combining a blinking LED with a static one

I want to make an amber LED blind while a red one is high dependant on the temp. I know this is a pretty easy one for some of you but could do with a bit of advice

else if( voltage <= 320) //voltage less that 32 degrees Celsius
{
digitalWrite(GREEN_ALARM); //turns of 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(1000); //delays for a second
digitalWrite(YELLOW_ALARM, LOW); //turns of yellow alarm
delay(1000); //delays for a second
}

would this work? I will include the full skrtch later on once its hopefully finished if that would make it easier for peeps to understand what I am doing :slight_smile:

would this work?

No.

      digitalWrite(GREEN_ALARM); //turns of green alarm

The digitalWrite() function needs two arguments - the pin number and the state. Writing LOW to the pin will turn it off, not of.

Other than that, the only problem is the long delay()s in the code.

else if( voltage <= 320) //voltage less that 32 degrees Celsius

You have a strange unit of measurement, there. Are you referring to temperature or voltage? They are not the same thing. Not even close.

Why is your amber LED pin named YELLOW_ALARM? Alarms make noise. If your amber/yellow LED is making noise, you have a serious wiring problem.

See the blink without delay example. IMO delay() is something that should never have seen light of day, its stops everything whilst its counting down.

Thank you paul. I want it the green alarm off not of sorry.
I can reduce the timing on the blink. I am sorry about the vague notes down the side, they are just so I can follow it without having to refer to my notes. I have done the analog to digital conversion earlier in the code. I am using a TMP 35 sensor for my input and 32 degrees = 320mV from the sensor. Sorry for being unclear. Let me finish my sketch and you will see what I am trying to do.
I was very apprehensive about using micro's because i knew nothing of the programming but it's fairt to say I am really getting into it now and am suprised at how much I am enjoying it :slight_smile:

Right here we go peeps. Here's my full sketch. This is my first proper go at my own skethch so please be gentle with me and any help will be greatly appreciated.
I am using a TMP 35 as the temp sensor and a piezo buzzer. The plan it to turn on and off LED's and make the amber one blink at a certain level. I am also going to make the buzzer go on and off at a specific level and then stay on with no other LED's lit at the last level.

//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 = 9;

yellow pin connection
int YELLOW_ALARM = 8;

//green pin connection
int GREEN_ALARM = 9;

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

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

What do people think? And remember I just learning this stuff so be gentle :slight_smile:
edit: I know theres some syntax errors and stuff like missing as \i have just resolved them when compiling but in general do peeps think this code will work? I have uploaded it into my arduino uno but I am waiting for my 9v dc power supply to arrive so can't yet try ity :frowning:

//converts to millivolts
    voltage = voltage * 1000;  
//temp less than 30 degrees Celsius (30mV)
    if( voltage <= 300 )
    {

If 300mV represents 30 degrees, why not do two things. First, make the comment correct. Second, create a new variable, tempC that is set to the new voltage divided by 10.

float tempC = voltage/10.0; // 10mV = 1 degree Celsius

Then, the if test can be:

if(tempC < 30.0)

This way, the number being compared to is in the same units as the comment. The cost will be minimal.

Also, when you paste code, do two things. Use the # button to put tags around the code, so it appears in the nice gray box with the scroll bars. The second, even more important, thing is to verify (compile) your code first.

Int BUZZ_PWM_ALARM = 4;

This will generate a syntax error.

Count the number of { and }. They don't match.

I know theres some syntax errors and stuff like missing as \i have just resolved them when compiling but in general do peeps think this code will work? I have uploaded it into my arduino uno

With the syntax errors? I don't think so...

Cheers Paul. I noticed the syntax error when compiling and have now resolved it. I did not know about putting the code in box as I am really am like a fresh fish with this and learning all the time. Thanks for your help and advice, I really appreciate it