I am trying to do my first project after doing a couple examples. I am trying to read a temperature and if the temp equals 90 then a LED lights up if not a different one lights up. Can someone point me as to what I doing wrong in the program. I believe its how I use if and else but I have tried a million combos with no luck to getting it to run right. As of now it is display no LED's and its reading the what I think is real temp and then it displays 90 after then back to temp back to 90 and so on. Thanks for any tips to figuring this out.
//TMP36 Pin Variable
const int temperaturePin = 0;
const int ledPin[] = {
11, 9};
void setup()
{
pinMode(ledPin[11, 9], OUTPUT);
Serial.begin(9600);
}
void loop()
{
float temperature = getVoltage(temperaturePin);
temperature = (((temperature - .5) * 100)*1.8) + 32; // Converting from 10 mv per degree with 500 mv offset to degrees ((voltage - 500mv) times 100) also converting to F instead of C
Serial.println (temperature);
if(temperature = 90)
{
digitalWrite(ledPin[11], HIGH);
digitalWrite(ledPin[9], LOW);
Serial.println(temperature);
delay(1000);
}
else
{
digitalWrite(ledPin[11], LOW);
digitalWrite(ledPin[9], HIGH);
Serial.println(temperature);
delay(1000);
}
}
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 milivolts
}
I got that bit of code from the LED example. I tried to combine the temp and LED examples together. I guess I should go back to examples for awhile still.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
The circuit:
* LED connected from digital pin 13 to ground.
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
Created 1 June 2005
By David Cuartielles
http://arduino.cc/en/Tutorial/Blink
based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
// The setup() method runs once, when the sketch starts
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
Thanks for the tip I will try and start again on it.
Here is what I came up with to work for what I was trying to accomplish. Can someone point out any better ways I could have done this or anything along those lines. It does work just want to get ideas on how to improve the code.
//TMP36 Pin Variable
int temperaturePin = 0; // pin that the sensor is attached to
int ledPin1 = 6; //connects red LED
int ledPin2 = 7; // connects green LED
void setup() {
pinMode(ledPin1, OUTPUT); // makeing both pins output
pinMode(ledPin2, OUTPUT);
// initialize serial communications:
Serial.begin(57600);
}
void loop()
{
float temperature = getVoltage(temperaturePin);
temperature = (((temperature - .5) * 100)*1.8) + 32;
int lights = temperature; // getting temperature value to send to if statement for LED
if (lights <= 73) // if the analog value is high enough, turn on the LED:
{
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
else
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
// print the analog value:
Serial.println(temperature);
delay(2000);
}
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 milivolts
}