Code Bug.

Hi all,

I have a small problem i cant seem to get around with my code:

Background:

I have an alarm pin which activates if parameters go out of limits. The alarm pin brings in a relay via a BD137. Now the circuit works fine, im just having trouble with the code. Here is the code:

 {
  if (tank_water_temp_f == 76) Serial.print("ALARM.....WATER TEMP LOW......");                                       // Outputs "Low Water Temp" alarm to PC
  if (tank_water_temp_f == 76) lcd.clear();                                                                          // Clears LCD screen ready for alarm message
  if (tank_water_temp_f == 76) lcd.print(" .....ALARM.....");                                                        // Prints 'ALARM' header on LCD
  if (tank_water_temp_f == 76) lcd.setCursor(17,1);                                                                  // Positions cursor on LCD for alarm message
  if (tank_water_temp_f == 76) lcd.print ("WATER TEMP LOW");                                                         // Prints 'Water Temp Low' message on LCD

  if (tank_water_temp_f == 76) digitalWrite (alarm_out_pin, HIGH);                                                   // Outputs alarm signal to alarm circuit
  if (tank_water_temp_f == 76) Serial.print ("WATER TEMP LOW");                                                      // Send serial message to pc to say WATER TEMP LOW
  
  else digitalWrite (alarm_out_pin, LOW);                                                                            // Resets the pin
 } 
 
 { 
  if (tank_water_temp_f > 84) Serial.print ("ALARM.....WATER TEMP HIGH.....");                                      // Outputs "High Water Temp" alarm to PC
  if (tank_water_temp_f > 84) lcd.clear();                                                                          // Clears LCD screen ready for alarm message
  if (tank_water_temp_f > 84) lcd.print(" .....ALARM.....");                                                        // Prints 'ALARM' header on LCD
  if (tank_water_temp_f > 84) lcd.setCursor(16,1);                                                                  // Positions cursor on LCD for alarm message
  if (tank_water_temp_f > 84) lcd.print ("WATER TEMP HIGH");                                                        // Prints 'Water Temp HIGH' message on LCD
 
  if (tank_water_temp_f > 84) digitalWrite (alarm_out_pin, HIGH);                                                     // Outputs alarm signal to alarm circuit
  if (tank_water_temp_f > 84) Serial.print ("WATER TEMP HIGH");                                                     // Send serial message to pc to say WATER TEMP HIGH
   
  else digitalWrite (alarm_out_pin, LOW);      
 }

Because of the " else digitalWrite (alarm_out_pin, LOW); " its making the relay turn on and off rapidly. But if i take them out the relay works fine but does not shut off when the alarm turns off.

Is there any other way i can write this code so that the relay comes on only when any of the alarms come on and then turns off when the alarm stops?

Any help or advice would be greatly appreciated.

VR.

you don’t really have a bug… you haven’t really written code in the “Arduino way” of doing things. There is an ONLINE version of Massimo Banzi’s book called “Getting Started with Arduino”. You really should have it handy and rethink some of your coding.

2 things you should look at:

Code Blocks and look at the section named “FLOW CONTROL”.

Your code blocks markers (brackets) are not in the right places.

You absolutely only have to test for a particular condition… one time. You keep testing it and there is no merit in that.

Example:

PSEUDO CODE I’m not re-writing all your code… just giving you ideas…

you want to say:

IF (expression)
{ // start block
statement
statement
statement
} // Close block

if (water_tank_temp_f > 84) {   // <--- OPEN A CODE BLOCK HERE with the bracket
   Serial.println("Alarm Condition";
   lcd.clear();
   lcd.print(".....ALARM.....");
   lcd.setCursor(16,1);
   lcd.print("WATER TEMP HIGH");
   PinState = HIGH;
   Serial.print("WATER TEMP HIGH");
   }      // close the IF block for a match  
else 
   {        // start a related block for "ELSE" or NOT MATCH
     PinState = LOW
}          // close  whole block for expression 



  // at the bottom... after all tests... set the pin to PinState

There is more than 1 way to write it so it works but you should should use this as means of learning how to write Arduino code

Thanks for the info.

Im very new to the Arduino way of programming and my mind is still in industrial PLC programming so its very difficult to try to learn the ‘Arduino Way’

Ill have a look at your code but what ive tried with it so far just comes up with errors when i compile it.

VR

I tried what you said like this:

 { 
  if (tank_water_temp_f > 84){ 
    Serial.print ("ALARM.....WATER TEMP HIGH.....");                                      // Outputs "High Water Temp" alarm to PC
    lcd.clear();                                                                          // Clears LCD screen ready for alarm message
    lcd.print(" .....ALARM.....");                                                        // Prints 'ALARM' header on LCD
    lcd.setCursor(16,1);                                                                  // Positions cursor on LCD for alarm message
    lcd.print ("WATER TEMP HIGH");                                                        // Prints 'Water Temp HIGH' message on LCD
    digitalWrite (alarm_out_pin, HIGH);                                                     // Outputs alarm signal to alarm circuit
    Serial.print ("WATER TEMP HIGH");    
  }    // Send serial message to pc to say WATER TEMP HIGH
  {
    else digitalWrite (alarm_out_pin, LOW);
  }
     
 }

but when i go to compile i get this:

In function ‘void loop()’:
error: ‘else’ without a previous 'if

Any ideas what im missing???

VR

You have a weird empty set of curly braces in there. Here’s the reference page for control structures that better explains the proper syntax: http://arduino.cc/en/Reference/Else

For starters, check notes in code below, look for <—

 {  <--- get rid of this
  if (tank_water_temp_f > 84){
    Serial.print ("ALARM.....WATER TEMP HIGH.....");   // Outputs "High Water Temp" alarm to PC
    lcd.clear();                                          // Clears LCD screen ready for alarm message
    lcd.print(" .....ALARM.....");                   // Prints 'ALARM' header on LCD
    lcd.setCursor(16,1);                               // Positions cursor on LCD for alarm message
    lcd.print ("WATER TEMP HIGH");             // Prints 'Water Temp HIGH' message on LCD
    digitalWrite (alarm_out_pin, HIGH);      // Outputs alarm signal to alarm circuit
    Serial.print ("WATER TEMP HIGH");       // Send serial message to pc to say WATER TEMP HIGH
  }
  {  <--- Misplaced curly brace, move it after ->else<- on next line
    else digitalWrite (alarm_out_pin, LOW);
  }

 }   <--- get rid of this

For clarification:

I did say I wasn’t trying to write “working” code. I said you needed to focus on Code Blocks (so I made the curlies near ELSE obvious) and the IF clause simplified… because it needed to be…

There is an ONLINE version of Massimo Banzi’s book called “Getting Started with Arduino”.

Wasn’t able to find it, but the print book is available for about $11.00 through Amazon.

The online programming references are quite good at giving samples so that syntax and structure are readily apparent and are available here: http://arduino.cc/en/Reference/HomePage

Arduino Programming Notebook PDF, print it up for reference: http://www.arduino.cc/playground/uploads/Main/arduino_notebook_v1-1.pdf

This new one is good too…

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1247637768

Thanks! I stuck a link on my blog for reference.