Using the temp sensor as a button

So one of the easiest things to do with the temperature sensor is to use it as a button to input user choice. With Sparki, this is a little redundant, as you could simply use the light sensors. However, it’s a good way to learn how to use the readings provided by the sensor.

arcbotics sparki

You need to start by taking the temperature at the beginning of the program. And then you can use this value to determine if human contact is being made (or someone’s holding a lighter to your robot). Because our hands are warmer than room temp.

Then simply add an if/else in there to determine what to do with this information. I just edited this touch sensor program I made previously, to choose to play a game when the “button” is pressed, or to free roam when not.

This is not the most useful way to use a temperature sensor. But I’ll get to that next time 🙂

#include <Sparki.h> // include the sparki library
float tempC;
float tempA;
int reading;
int tempPin = 8;

void setup()
{
analogReference(INTERNAL);
reading = analogRead(tempPin);
tempA=reading/9.31-10;
sparki.clearLCD();
sparki.print(tempA);
sparki.updateLCD();
delay(1000);
}

void shakeGame()
{
sparki.clearLCD();
sparki.drawString(10,1,”Hello human”);
sparki.drawString(10,3,”shake me when you see the”);
sparki.drawString(10,5,”GREEN LIGHT”);
sparki.updateLCD();
delay(3000);
int score=0;
int counter1=0;
while (counter1<5)
{
randomSeed(analogRead(1));
int response1 = random(1,4);
int counter=0;
while(counter<response1)
{
sparki.RGB(100,0,0); // Make the LED maximum Red
delay(1000);
sparki.RGB(0,0,100); // Make the LED maximum Blue
delay(1000);
counter=counter+1;
}
sparki.RGB(0,100,0); // Make the LED maximum Green
delay(1000);

if(sparki.accelX() <0)
{
score=score+1;
sparki.clearLCD();
sparki.print(score);
sparki.updateLCD();
delay(1000);
}
counter1=counter1+1;
}
sparki.RGB(RGB_OFF);
sparki.clearLCD();
sparki.drawString(10,1,”Well done!”);
sparki.drawString(10,3,”You scored”);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.print(score);
sparki.updateLCD();
delay(1000);
}
void freeRoam(){
sparki.clearLCD();
sparki.drawString(10,1,”Free roam”);
sparki.updateLCD();
while(sparki.ping()>20)
{
sparki.moveForward(
);
}
sparki.moveStop();
sparki.moveRight(90);
delay(1000);
int R=sparki.ping();
sparki.ping();
delay(2000);
sparki.clearLCD();
sparki.println(R);
sparki.updateLCD();
delay(1000);
sparki.moveLeft(180);
delay(1000);
int L = sparki.ping();
sparki.ping();
delay(2000);
sparki.clearLCD();
sparki.println(L);
sparki.updateLCD();
delay(1000);
sparki.moveRight(90);

if((R>10) && (R>=L))
{
sparki.moveRight(90);

}
if ((L>10) && (L>=R))
{
sparki.moveLeft(90);

}
if ((R<=10) && (L <= 10))
{
sparki.moveRight(180);
sparki.RGB(0,0,220);

}
}
void loop()
{
reading = analogRead(tempPin);
tempA=reading/9.31-10;
sparki.clearLCD();
sparki.print(tempA);
sparki.updateLCD();
delay(3000);
sparki.clearLCD();
sparki.drawString(10,1,”Touch sensor to play”);
sparki.updateLCD();
delay(2000);
reading = analogRead(tempPin);
tempC=reading/9.31-10;
sparki.clearLCD();
sparki.print(tempC);
sparki.updateLCD();
delay(1000);
if (tempC>tempA)
{
shakeGame();
}
else
{
freeRoam();
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s