So, I started on my first little project without a tutorial. Or actually, with one, but one that I have adapted to do a little more.
I used the standard oomlout tutorial that came with my Arduino kit, but expanded it to not only show the temperature in the serial monitor, but also by using 6 LED's (2 blue, 2 green, 2 red) that should go on when specific values are met or surpassed.
Should be pretty easy, I thought. But all the LED's turn on. I got the temperature reading in the serial monitor displaying a value of a bit under 22 degrees, which should mean the two blue LED's and 1 green LED should light up.
Seeing the LED's actually light up and the temp. reading is correct, my hardware should be good.
Below is the code I made/adapted. Any help and explanation why it isn't working would be highly appreciated. I'm learning pretty fast (the last time I did any programming is over 10 years ago but I'm picking it up quite well) so I'll be only asking this once :)
Thanks in advance!
/* Borrowed most of the code form the ARDX experimenters kit tutorials by oomlout
* editted for making a visual temperature reading with 6 LEDS
*/
//TMP36 Pin Variables
int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade
//(500 mV offset) to make negative temperatures an option
int blue1 = 7; //LED for cold
int blue2 = 6; //LED for less cold
int green1 = 5; //LED for medium cold
int green2 = 4; //LED for medium warm
int red1 = 3; //LED for warm
int red2 = 2; //LED for really warm
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(9600); //Start the serial connection with the copmuter
//to view the result open the serial monitor
//last button beneath the file bar (looks like a box with an antenae)
pinMode(blue1, OUTPUT); //setting all the pins as output
pinMode(blue2, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(green2, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(red2, OUTPUT);
}
void loop() // run over and over again
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
float temperature1 = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
if (temperature1 >= 10.00) //using the code below to create the visual thermometer
digitalWrite(blue1, HIGH);
if (temperature1 >= 15.00)
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
if (temperature1 >= 20.00)
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
digitalWrite(green1, HIGH);
if (temperature1 >= 25.00)
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
digitalWrite(green1, HIGH);
digitalWrite(green2, HIGH);
if (temperature1 >= 27.00)
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
digitalWrite(green1, HIGH);
digitalWrite(green2, HIGH);
digitalWrite(red1, HIGH);
if (temperature1 >= 30.00)
digitalWrite(blue1, HIGH);
digitalWrite(blue2, HIGH);
digitalWrite(green1, HIGH);
digitalWrite(green2, HIGH);
digitalWrite(red1, HIGH);
digitalWrite(red2, HIGH);
Serial.println(temperature1); //printing the result
delay(1000); //waiting a second
}
/*
* getVoltage() - returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1024 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}