Newbie could really do some help with my code

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 :slight_smile:

//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);
}

when I verified it and uploaded it via the IDE there seemed to be no problem

Not clear what you're asking if there's no problem.

//setting the reference voltage to the default setting

It's the default; there's no need for you to set it.

When posting code, please use the "Code" # icon on the editor's toolbar.

Cheers AWOL. I think the code in theory is ok, thats why it verified OK. But it was more to do with the fact that I could not get my design working and wanted to know if I had made any glaring mistakes. I come from a semi conductor background as was only introduced to micro's this year while studying so they are totally new to me. But I do find them interesting and am trying to implement them into two projects for work at the moment. With being new to them I am trying to walk before I run so to speak. thanks for your help, I will remove the reference and see if it makes a difference :slight_smile:

But it was more to do with the fact that I could not get my design working

You didn't tell us that.

You haven't said what "working" means to you in this instance, and how what you observed from your output differs from that definition of "working".

Without that basic information, it isn't easy to offer more help.

//pin for which tmp 35 sensor inputs into the arduino uno
 int TMP_35_IN_A3 = 3;

Why is the pin number part of the name of the variable, in this instance only?

What happens if you need to change the pin to pin 5, for instance? Will you change the variable name, too? In which case, why bother with a variable? If you don't change the variable name, doesn't

//pin for which tmp 35 sensor inputs into the arduino uno
 int TMP_35_IN_A3 =53;

look a little strange?

Ok I will try to give a fuller explanation. I was in work on nights last night so head is still a bit fuzzy.
I uploaded the code and connected my circuit but it did not work. I measured the 5v output from the uno and it measured 3.9V. I was also getting a voltage from the input leg of the TMP sensor so I presumed everything into the micro was oK. The problem was that I was not getting anything on the expected output pin. Because of htis I thought it was the code that was the problem so I reset the Uno and re-compiled the code. then I got absolutely nothing and the TX light on the Uno was illuminated. I dod not know what this TX is for. I removed the USB and put it back in and the TX light is no longer on but I am not getting any voltage at all anywhere now :frowning:

What do you get on your serial console? Does the debug info (analog and voltage) print out? If so, what values do you get?