Hi all. I decided to use the arduino for a project in work which takes a temperature reading and gives me an alarm depending on the temp. I am not really too good with micro's or the code but am slowly learning. Could anyone take a look at my code and see if they can see any obvios mistakes as when I verified it and uploaded it via the IDE there seemed to be no problem. I am using a tmp 35 sensor and it is currently being powered via USB from my pc.
Here is my code and thanks in advance ![]()
//pin for which tmp 35 sensor inputs into the arduino uno
int TMP_35_IN_A3 = 3;
//now to declare integers of any other peripherals
//red pin connection
int RED_ALARM = 8;
//yellow pin connection
int YELLOW_ALARM = 9;
//green pin connection
int GREEN_ALARM =10 ;
//buzzer alarm connection
int BUZZ_PWM_ALARM = 4;
void setup()
{
Serial.begin(9600); //this is how the chip communicates with the PC
//set ALARMS pins as output
pinMode(RED_ALARM, OUTPUT);
pinMode(YELLOW_ALARM, OUTPUT);
pinMode(GREEN_ALARM, OUTPUT);
pinMode(BUZZ_PWM_ALARM, OUTPUT);
//setting the reference voltage to the default setting
analogReference(DEFAULT);
}
void loop()
{
long aRead = 0; //assumes voltage range from input
float voltage; //declares a floating number of the voltage
// uses a for statement to count up to 10 readings
// which will limit fluctuations when averaged out
for(int i=0; i < 10; i++)
aRead += analogRead(TMP_35_IN_A3); //reads the value from tmp sensor
aRead = aRead / 10; //now takes the average of 10 readings
//this performs the analog to digital conversion
voltage = (aRead * 5.0) / 1024.0;
//converts to millivolts
voltage = voltage * 1000;
//temp less than 30 degrees Celsius (30mV)
if( voltage <= 300 )
{
digitalWrite(GREEN_ALARM, HIGH); //turn on green alarm
digitalWrite(YELLOW_ALARM, LOW); //turn off yellow alarm
digitalWrite(RED_ALARM, LOW); //turn off red alarm
digitalWrite(BUZZ_PWM_ALARM, LOW); //turn off buzzer
}
else if( voltage <= 310 ) //voltage between 30 degrees and 31 degrees
//Celsius turn red and yellow on
{
digitalWrite(GREEN_ALARM, HIGH); //turn on green alarm
digitalWrite(YELLOW_ALARM, HIGH); //turn on yellow alarm
digitalWrite(RED_ALARM, LOW); //turn off red alarm
digitalWrite(BUZZ_PWM_ALARM, LOW); //turn off buzzer
}
else if( voltage <= 315) //voltage less that 31.5 degrees Celsius
{
digitalWrite(GREEN_ALARM, LOW); //turns off green alarm
digitalWrite(YELLOW_ALARM, HIGH); //turns on yellow alarm
digitalWrite(RED_ALARM, LOW); //turns off yellow alarm
digitalWrite(BUZZ_PWM_ALARM, LOW); //turns off buzzer
}
else if( voltage <= 320) //voltage less that 32 degrees Celsius
{
digitalWrite(GREEN_ALARM, LOW); //turns off green alarm
digitalWrite(BUZZ_PWM_ALARM, LOW); //turns off buzzer
digitalWrite(RED_ALARM, HIGH); //turns on red alarm
digitalWrite(YELLOW_ALARM, HIGH); //turns on yellow alarm
delay(200); //delays for 200mS
digitalWrite(YELLOW_ALARM, LOW); //turns off yellow alarm
delay(200); //delays for 200mS
}
else if( voltage <= 330) //voltage less that 33 degrees Celsius
{
digitalWrite(GREEN_ALARM, LOW); //turns off green alarm
digitalWrite(YELLOW_ALARM, LOW); //turns off yellow alarm
digitalWrite(RED_ALARM, HIGH); //turns on red alarm
analogWrite(BUZZ_PWM_ALARM, 128); //turns on buzzer
delay(500); //delays for 200mS
digitalWrite(BUZZ_PWM_ALARM, LOW); //turns off buzzer
delay(200); //delays for 200mS
}
else // anything above 33 degrees Celsius
{
digitalWrite(GREEN_ALARM, LOW); //turns off green alarm
digitalWrite(YELLOW_ALARM, LOW); //turns off yellow alarm
digitalWrite(RED_ALARM, LOW); //turns off red alarm
analogWrite(BUZZ_PWM_ALARM, 254); //turns on buzzer
}
//print debug info to serial console
Serial.print("Analog: ");
Serial.print(aRead);
Serial.print(" Voltage:");
Serial.println(voltage);
//short 1 sec delay between measurements
delay(1000);
}