My Arduino seems to be stuck inside of an if statement??

I'm so dang lost.. :~ I thought for sure this should work??

I am new to all of this and I am having problems with my Arduino Uno staying within one if statement instead of going through the entire loop again. Right now I have if statements inside of if statements as diagrammed below

void setup(){

int ...

}

void loop(){

Read temp

if (above certain temp)
{

display the error
if(button==LOW){
do nothing
}

if(button==HIGH){
fix error
}

}

if (below certain temp){

display the error
if(button==LOW){
do nothing
}

if(button==HIGH){
fix error
}
}

if (low temp<reading<high temp){
do nothing
}
}

now when I have this all written out it will stay within just one if statement instead of reading the temperature again.. It will determine the error but it wont go back to the top of the loop like I need it to..

Is there a command or another way of going about writing this code that will make it go through all of these steps effectively?

thanks a whole lot!! :slight_smile:

if this doesnt make sense, please tell me and I wiil try to explain another way!

if this doesnt make sense, please tell me and I wiil try to explain another way!

Not a lot of sense.

it will stay within just one if statement instead of reading the temperature again

Do you mean the "Read Temp" function at the beginning of the loop does not get called?
Try to use Serial.print for debugging, it will clarify the execution path for you.

I am having problems with my Arduino Uno staying within one if statement instead of going through the entire loop again

That doesn't make sense to me. Are you trying to display an error, only once?

The problem I am having is that it reads the temperature right at first and it goes to an if statement based on that reading. Then once it is inside of that if statement, and has executed either of the if statements inside of it, the arduino will not return to the top of the program and start over (beginning at the "Read temp" commands) like I assumed it would, instead it constantly loops inside of the initial if statement.

Is there something wrong with the way I'm writing my if statements?

I'm really trying my best to explain.. If I need to I will copy my entire code, but only if youre interested. its pretty long (well for me at least) haha! thanks for the replies!!

paden_sparks:
Is there something wrong with the way I'm writing my if statements?

Unless there is a problem with your actual code, on each iteration of loop(), the if-statement evaluates true and gets executed. This is leading you to the false conclusion that the code is "stuck" in the if-statement, which it isn't. Of course, this is just a guess because you didn't include your actual code...

paden_sparks:
I'm really trying my best to explain.. If I need to I will copy my entire code

Actual code is generally preferred.

I understand that this is new to you and you are trying to understand. However, an 'if' can not loop. From the code that you have listed there is no way that you would not reach the end of the 'loop' function and begin again that the top of the loop function. In that case, the "Read temp" will occur again. Now it is quite likely that the temp has not changed between the loop and therefore you will probably get the same error message again, and again, and again. But the read probably took place each time.

I hope this helps.

You leave an if statement by "falling off the end". Here's a quick and dirty re-write of what I think you're trying to do:

#define BUTTON ?
#define SENSOR ?
#define MAXT 74
#define MINT 70

int temperature;                       /* Temperature reading        */

void fix_hi_temp(int t)                /* Correct over-temperature   */
{
}

void fix_lo_temp(int t)                /* Correct under-temperature  */
{
}

void setup()
{  Serial.begin(115200);               /* Connect monitor            */
   pinMode(BUTTON,INPUT);
}

void loop()
{  temperature = analogRead(SENSOR);   /* Read temp                  */
   if (temperature > MAXT)
   {  Serial.println("Temp too high"); /* Display the error          */
      if (digitalRead(BUTTON) == HIGH) fix_hi_temp();
   }
   else if (temperature < MINT)
   {  Serial.println("Temp too low");  /* Display the error          */
      if (digitalRead(BUTTON) == HIGH) fix_lo_temp();
   }
}
void setup()
{
  
  Serial.begin(9600);            //Baud Rate//
  Serial.print(17,BYTE);         //Backlight For LCD//
  pinMode(1,OUTPUT);             //LCD Screen//            
  pinMode(3, OUTPUT);            //Conductivity Sensor// 
  pinMode(8,OUTPUT);             //Solenoid Valve//
  pinMode(9,OUTPUT);             //Solenoid Valve/
  pinMode(11,OUTPUT);            //  top middle  //          BLUE = everything in check  (between UCL and LCL on both heat and salinity)
  pinMode(6,OUTPUT);             //  bottom middle  //       small green = deadtime
  pinMode(12,OUTPUT);            //  top left  //            green = above salt Upper Control Limit
  pinMode(10,OUTPUT);            //  bottom left  //         yellow = Below salt Lower Control Limit
  pinMode(7,OUTPUT);             //  top right  //           green = above temp UCL
  pinMode(5, OUTPUT);            //  bottom right  //        yellow = below temp LCL
  pinMode(0, INPUT);             //  Momentary Switch //
  Serial.print(12, BYTE);        //  Clear Screen//        
  Serial.print(129,BYTE);
  Serial.print("LCL");
  Serial.print(136,BYTE);
  Serial.print("SP");
  Serial.print(143,BYTE);
  Serial.print("UCL");
  Serial.print(188,BYTE);
  Serial.print("SALTY");
  Serial.print(194,BYTE);
  Serial.print("CURRENT");
  Serial.print(203,BYTE);
  Serial.print("DI");


  
}


void loop()
{
 double P_SETPOINT = .0008;
 int A_SETPOINT;
 int deadtime=8000;
     A_SETPOINT = 972.07*pow(.0008,.0926);     
     int UCL = A_SETPOINT+6;
     double P_UCL = 3*pow(10,-32)*pow(UCL,10.503)+.01;
     int LCL = A_SETPOINT-6;
     double P_LCL = 3*pow(10,-32)*pow(LCL,10.503)+.01;
          


 digitalWrite(3,HIGH);
 delay(500);
 int Analog;
 Analog = analogRead(0);
digitalWrite(3,LOW);  //Conductivity Sensor

  double Sal;
Sal = 3*pow(10,-32)*pow(Analog,10.503);
Serial.print(175,BYTE);
Serial.print(Sal*100,3);



 if (Analog > UCL)
 {

  digitalWrite(12,HIGH);
  digitalWrite(6,HIGH);
  delay(deadtime);
  digitalWrite(6,LOW);
  delay(100);
  
 digitalWrite(3,HIGH);
 delay(500);
 int Analog;
 Analog = analogRead(0);
digitalWrite(3,LOW);  //Conductivity Sensor
  
  Serial.print(175,BYTE);
  Serial.print(Sal*100,3);
  Serial.print("    ");
 
  double Sal;
  double topen;
  double C2;
  double Madd;
  Sal = 3*pow(10,-32)*pow(Analog,10.503);
  C2 = Sal-((Sal-(P_SETPOINT))*(.8));
  Madd = (84.6*(Sal-C2))/(.85*Sal);              
  topen = (Madd/7.528)*1000;
  int Button = digitalRead(0);
  
  if(Button==LOW){
  Serial.print(17,BYTE);
  delay(300);
  Serial.print(18,BYTE);
  delay(300);
  Serial.print(17,BYTE);
  delay(300);
  Serial.print(18,BYTE);
  delay(300);
 
  }
  
  if (Button==HIGH) {
  
   digitalWrite(8,HIGH);
      delay(topen);
   digitalWrite(8,LOW);                
      delay(50);
  }
 
 digitalWrite(6,HIGH);
 delay(deadtime);
 digitalWrite(6,LOW);
 delay(100);
 digitalWrite(12,LOW);
 }


     if (Analog < LCL)
 {
 
  digitalWrite(10,HIGH);
  digitalWrite(6,HIGH);
  delay(deadtime);
  digitalWrite(6,LOW);
  delay(100);

 digitalWrite(3,HIGH);
 delay(500);
 int Analog;
 Analog = analogRead(0);
digitalWrite(3,LOW);       //Conductivity Sensor
   
   double topen;
   double Sal;
   double C2;
   double Madd;
   Sal = 3*pow(10,-32)*pow(Analog,10.503);
   C2 = Sal+((P_SETPOINT-Sal)*.8);                    
   Madd = (84.6*(C2-Sal))/(.85*(.0015-Sal));
   topen = (Madd/7.528)*1000;
   int Button=digitalRead(0);
  
  if(Button==LOW){
    Serial.print(18,BYTE);
    delay(300);
    Serial.print(17,BYTE);
    delay(300);

  }
  if (Button==HIGH){  
  Serial.print(17,BYTE);
    delay(300);
   digitalWrite(9,HIGH);
      delay(topen);                                    
   digitalWrite(9,LOW);
      delay(100);
  }
  
   digitalWrite(6,HIGH);
      delay(deadtime);
   digitalWrite(6,LOW);          
      delay(100);
 }

 if (UCL>Analog>LCL)
 {
 digitalWrite(8,LOW);
 digitalWrite(9,LOW);
 digitalWrite(11,HIGH);
 }
     
}

Thanks guys

Please read this http://arduino.cc/forum/index.php/topic,97455.0.html

Follow the instructions on how to post code in point 6. It will stop smileys appearing.

Help us (and yourself) by getting into the habit of giving the pins meaningful names.

Oh, and please DON'T SHOUT.

The purpose was not for you to understand what exactly is going on with my program as far as its function.. The point was to see if the syntax of my coding is correct.. I just feel like giving up at this point.. I mean does ANY of it make sense?

the point was to see if the syntax of my coding is correct

This is what a compiler does.

paden_sparks:
The purpose was not for you to understand what exactly is going on with my program as far as its function.. The point was to see if the syntax of my coding is correct.. I just feel like giving up at this point.. I mean does ANY of it make sense?

Could you at least click the "Modify" button on that post and put your code in [code ] [/code ] tags?

paden_sparks ,

What don't see -

Your "sketch" is run from a function called 'main' outside of your control. 'main' looks (roughly) like this -

void main()
{
setup();

for ( ;; )
{
loop();
}
}

Where 'setup' and 'loop' are provided by you.

You implement whatever is needed in order to setup your "sketch". 'setup' is executed but ONCE.

Following the automatic return from 'setup', which happened upon running out of code to execute in 'setup', execution continues following the code that called it in 'main'.

At this point the 'for ( ;; )' loop is entered. Given the specification 'for ( ;; )' will loop forever executing the routine 'loop'. Upon reaching the bottom of the code in YOUR 'loop' function the 'for ( ;; )' in 'main' will immediately execute your provided 'loop' routine.

This basically continues till the power is shutoff.

Not that you all cannot already read code.. I'll explain what I am trying to accomplish.

I have been challenged with controlling a certain salinity in a small "fish tank".
I have established a UCL and an LCL (Upper Control Limit & Lower Control Limit) as well as a Setpoint (basically exactly what salinity I want my fish tank to be at)

I want my system to read the salinity (My salinity/conductivity sensor is connected to pin3) and then place it inside of an if statement based on its readings.

Let's assume that it read above the UCL
Arduino recognizes reading and jumps to "if(Analog>UCL)" statement

I WANT it to turn on the "top left" (pin12) LED, wait for me to press a button (connected to pin0), and then dump in the correct amount of fresh water based on its calculaiton of error.
Once it dumps this water in, it waits a certain amount of time and turns on an LED that indicates it is in this "deadtime".

Now that it has done all of this, it SHOULD (I would assume) jump back up to the top of the void loop() and read the salinity again and place it in the necessary if statement and repeat the process..

Unfortunately, it seems that it will not go back up to the top of the void loop() again.. Instead it just repeats the same process over and over again inside of the UCL..
How do I know?? I have the LED (pin12) turn off at the end of the "if(Analog>UCL)" statement. The problem is it just stays on all the time and never goes off! I would think that it should be off for my "delay(500)" in the first part of my loop, right?? And also, when I press the reset button, it will sometimes jump to the "if (Analog<LCL)" statement because of all of the corrections made during the initial process..

I was thinking maybe it was because I have Two "if" statements inside of another "if" statement. I was just going to see if maybe there was somehting I could put like "break" (I've read up on this command but dont really understand what it does) or something similar would kick it out of that statement..

I like this idea alot.. I'm going to try a crack at this instead of the complicated way of writing like mine is.
I just feel like its easy to get lost in my code..
Isnt this format called "Subroutines"?

Why do you feel it necessary to recalculate a bunch of constants in loop()?

 double P_SETPOINT = .0008;
 int A_SETPOINT;
 int deadtime=8000;
     A_SETPOINT = 972.07*pow(.0008,.0926);     
     int UCL = A_SETPOINT+6;
     double P_UCL = 3*pow(10,-32)*pow(UCL,10.503)+.01;
     int LCL = A_SETPOINT-6;
     double P_LCL = 3*pow(10,-32)*pow(LCL,10.503)+.01;

These are never going to take on different values, are they?

Make them global, and compute the values in setup().

 Analog = analogRead(0);

And Analog contains? Lousy name, by the way. There is something attached to this pin that should give a clue as to the correct name for the value. I'll bet that the clue does not lead to the name Analog.

 int Analog;
 Analog = analogRead(0);
digitalWrite(3,LOW);  //Conductivity Sensor

Lovely. Another variable of the same name in the if block. This leads to baldness.

   double Sal;

Lovely. Another Sal. Good to have two Sals around. Easy to keep them straight. Sal is the one with the blond hair.

Back to the drawing board...

What do you think this does?

	if ( UCL > Analog > LCL )

lloyddean:
What do you think this does?

	if ( UCL > Analog > LCL )

This is a setup for future additions.. I will have to eventually control the temperature after the salinity is corrected..

"Analog" is the variable I am using indicating the Analog Reading I am getting from my Conductivity Sensor (digitalpin 3, and analogpin 0)

@PaulS

I will fix that..

but before I had those declared more than once, it would give me zero. So placed them inside of the if statements right before I was going to recall it, and it actually seemed to work..