Problem with my if statment !!

‏I have a problem in my code flow for the program, where I used nested if to make a multi condition , but my problem is that if Nested run only the first two conditions, but the rest do not work only if he achieved the first condition and the second with the third , So what i can do here ??!

If((Avg_Raw >= 201) && (Avg_Raw <= 600))
{
If(c == 0)
{
Serial.print(“order 1”);
c=0;
goto getC;

If((attention >= 65) && (attention <= 100))
{
Serial.print(“move”);
}
}

getC:
c=c
}

"Sorry for my weak english

Your code presented correctly:

if((Avg_Raw >= 201) && (Avg_Raw <= 600)) {
  if(c == 0){
    Serial.print("order 1");
    c=0;
    goto getC;
    if((attention >= 65) && (attention <= 100)) {
      Serial.print("move");
    }
  }
  getC:
  c=c
}

Indenting your code is a must to make sure that your logic is correct. With this indentation, take a look at how the ifs are placed.

Jacques

All ifs are correct
But the problem still the same !!

But I noticed something I mentioned at the beginning, when the value of (Attention) appears at the same time with the value of (Avg_Raw), then I get the value required

And this is the real problem , I want get required value without need to get two value in the same time !!

itsreco7:
And this is the real problem , I want get required value without need to get two value in the same time !!

It sounds like the problem is in the code you did not post.

Post the complete program and make sure to use the AutoFormat tool to indent it properly as shown by @jbellavance.

And also use the code button </> to make it display as in Reply #1

…R

getC:
  c=c
}

Too many labels and pointless assignments, too few semicolons

#define LED 13
#define EBL 12
#define BAUDRATE 57600
#define DEBUGOUTPUT 0
#define EEG_AVG  60

#define R_Mot_F 2
#define R_Mot_B 3
#define L_Mot_F 4
#define L_Mot_B 5
#define RML 6
#define LML 7

#define echo 9
#define trig 10

// checksum variables
byte generatedChecksum = 0;
byte checksum = 0;
int payloadLength = 0;
unsigned long payloadDataS[5]  = {0};
unsigned long payloadDataB[64] = {0};
unsigned long Avg_Raw,Temp,Temp_Avg;
unsigned int  Raw_data,Raw_EEG,On_Flag=0,Off_Flag=1;
byte poorQuality = 0;
byte attention = 0;
byte meditation = 0;
int x,c,j,n=0;

//ultrasonic variable
long duration;
int distance;

//milli() variable
unsigned long previousMillis = 0;
unsigned long interval = 3000;

// system variables
boolean bigPacket = false;
//-------------------------------------------------------

void Wheelchair_Forword()
 {  
   digitalWrite(R_Mot_F, HIGH);  
   digitalWrite(R_Mot_B, LOW);
   digitalWrite(L_Mot_F, HIGH);  
   digitalWrite(L_Mot_B, LOW);
   digitalWrite(RML, HIGH);  
   digitalWrite(LML, HIGH);
   Serial.print("Move"); 
   Serial.print("\n");
   Serial.print("\n"); 
 }
 
 void Wheelchair_Backword()
 {  
   digitalWrite(R_Mot_F, LOW);  
   digitalWrite(R_Mot_B, HIGH);
   digitalWrite(L_Mot_F, LOW);  
   digitalWrite(L_Mot_B, HIGH);
   digitalWrite(RML, HIGH);  
   digitalWrite(LML, HIGH);
   Serial.print("Go Back"); 
   Serial.print("\n");
   Serial.print("\n"); 
 }
 
 void Wheelchair_Stop()
 {
   digitalWrite(R_Mot_F, LOW);  
   digitalWrite(R_Mot_B, LOW);
   digitalWrite(L_Mot_F, LOW);  
   digitalWrite(L_Mot_B, LOW);
   digitalWrite(RML, LOW);  
   digitalWrite(LML, LOW);
   Serial.print("Stop");              
   Serial.print("\n");
   Serial.print("\n");
 }
 
 void Wheelchair_Go_R()
 {  
   digitalWrite(R_Mot_F, LOW);  
   digitalWrite(R_Mot_B, LOW);
   digitalWrite(L_Mot_F, HIGH);  
   digitalWrite(L_Mot_B, LOW);
   digitalWrite(RML, LOW);  
   digitalWrite(LML, HIGH); 
   Serial.print("Go Right");               
   Serial.print("\n");
   Serial.print("\n");
 }
 
 void Wheelchair_Go_L()
 {  
   digitalWrite(R_Mot_F, HIGH);  
   digitalWrite(R_Mot_B, LOW);
   digitalWrite(L_Mot_F, LOW);  
   digitalWrite(L_Mot_B, LOW);
   digitalWrite(RML, HIGH);  
   digitalWrite(LML, LOW);  
   Serial.print("Go Left");
   Serial.print("\n");
   Serial.print("\n");
 }
//--------------------------------------------------------------

// Microprocessor Setup //

void setup()
{
pinMode(R_Mot_F, OUTPUT);   
  pinMode(R_Mot_B, OUTPUT);
  pinMode(L_Mot_F, OUTPUT);   
  pinMode(L_Mot_B, OUTPUT);
  pinMode(RML, OUTPUT);   
  pinMode(LML, OUTPUT);

  pinMode(LED, OUTPUT);
  pinMode(EBL, OUTPUT);

Serial.begin(BAUDRATE);
}

// Read data from Serial UART //

byte ReadOneByte()
{
  int ByteRead;
  while(!Serial.available());
  ByteRead = Serial.read();

#if DEBUGOUTPUT
  Serial.print((char)ByteRead); // echo the same byte out the USB serial (for debug purposes)
#endif
  return ByteRead;
}


//MAIN LOOP//

void loop()
{
// Look for sync bytes
  if(ReadOneByte() == 170)
  {
    if(ReadOneByte() == 170)
    {
      payloadLength = ReadOneByte();
      if(payloadLength == 32)
      {
        Big_Packet(payloadLength);
        return;
      }
      else if(payloadLength == 4)   // Small Packet
      { 
        Raw_EEG = Small_Packet(payloadLength);
        if (j<512)
         {
           Temp += Raw_EEG;
           j++; 
          }
           else
          {
           ONESEC_RAW_VAL(Temp);
           j=0;
           Temp=0;
        }
      }
    }
  }
}

void Big_Packet(byte data)
{
      generatedChecksum = 0;
      for(int i = 0; i < payloadLength; i++)
    {
      payloadDataB[i] = ReadOneByte();                   //Read payload into memory
      generatedChecksum += payloadDataB[i];
    }
    
      checksum = ReadOneByte();                         //Read checksum byte from stream
      generatedChecksum = 255 - generatedChecksum;      //Take one's compliment of generated checksum
  
  if(checksum == generatedChecksum)
  {
    poorQuality = 200;
    attention = payloadDataB[29];
    meditation = 0;
    
    for(int i = 0; i < payloadLength; i++)
   {                                                   //Parse the payload
    switch (payloadDataB[i])
    {
      case 2:
        i++;
        poorQuality = payloadDataB[i];
        bigPacket = true;
        break;
      case 4:
        i++;
        attention = payloadDataB[i]; 
        break;
        case 5:
        i++;
        meditation = payloadDataB[i];
        break;
      case 0x80:
        i = i + 3;
        break;
      case 0x83:
        i = i + 25;
        break;
      default:
        break;
      } // switch
    } // for loop
  
#if !DEBUGOUTPUT

if(bigPacket)
      {
        if(poorQuality == 0)
           digitalWrite(LED, HIGH);
        else
           digitalWrite(LED, LOW);
           
           Serial.print("PoorQuality: ");
           Serial.print(poorQuality, DEC);
           Serial.print("\n");
           Serial.print("Attention: ");
           Serial.print(attention, DEC);
           Serial.print(" | ");
           Serial.print("meditation: ");
           Serial.print(meditation, DEC);
           Serial.print(" | ");
           Serial.print("Rawdata(EYEBLINK): ");
           Serial.print(Avg_Raw,DEC);
           Serial.print("\n");
           Serial.print("\n");

if((Avg_Raw>=201) && (Avg_Raw<=600)) {
            if(c==0) {
              
             Wheelchair_Stop();
             Serial.print("order 1");
             Serial.print("\n");
             c=1; 
             goto GetC;
           if((Avg_Raw>=103) && (Avg_Raw<=200))
           {
           digitalWrite(EBL, HIGH); 
           {
           if(x==0)
           {
           Wheelchair_Go_R(); 
           x=1;
           goto GetX;
            }
           else if(x==1)
           {
           Wheelchair_Stop();
           x=2;
           goto GetX;
           }
           else if(x==2)
           {
           Wheelchair_Go_L();
           x=3;
           goto GetX;
           }
           else if(x==3)
           {
           Wheelchair_Stop();
           x=0;
           goto GetX;
           }
           }
            GetX:
             x=x;     
           }
             }
             else if(c==1) {
    Wheelchair_Stop();
               digitalWrite(EBL, LOW);
               Serial.print("order 2 ");
               Serial.print("\n");
               c=0;  
               goto GetC;
               
           if((attention>=65) && (attention<=100))
           {
             Wheelchair_Forword();  
           }                         
       }             
        GetC:
        c=c;
           }
   }   
           
 #endif
    bigPacket = false;
  }
}


unsigned long Small_Packet (byte data)
{
  generatedChecksum = 0;
  for(int i = 0; i < data; i++) 
  {  
    payloadDataS[i]     = ReadOneByte();      //Read payload into memory
    generatedChecksum  += payloadDataS[i] ;
  }
  generatedChecksum = 255 - generatedChecksum;
  checksum  = ReadOneByte();
  if(checksum == generatedChecksum)         // Varify Checksum
  {  
      Raw_data  = ((payloadDataS[2] << 8)| payloadDataS[3]);
      if(Raw_data & 0xF000)
      { 
        Raw_data = (((~Raw_data) & 0xFFF)+1); 
      }
      else
      { 
        Raw_data = (Raw_data & 0xFFF); 
      }
      
      return Raw_data;
  }
} 

void ONESEC_RAW_VAL(unsigned long data)
{
  Avg_Raw = data/512;
  if (On_Flag==0 && Off_Flag==1)
  {
    if (n<4)
    {
      Temp_Avg += Avg_Raw;
      n++;
    }
    else
    {
      Temp_Avg = Temp_Avg/4;
      if (Temp_Avg<EEG_AVG) 
      {
        On_Flag=1;Off_Flag=0;
      }
      n=0;Temp_Avg=0;
    }  
  }              
}

Don't use 'goto' ! Looks like you don't need it either. And if you feel you need it, you're doing something wrong.

sterretje:
Don't use 'goto' ! Looks like you don't need it either. And if you feel you need it, you're doing something wrong.

So what is the alternative? ?

What is the intent behind using goto on:

         if((Avg_Raw>=103) && (Avg_Raw<=200)) {
           digitalWrite(EBL, HIGH); 
           if(x==0) {
             Wheelchair_Go_R(); 
             x=1;
             goto GetX;
           }
           else if(x==1) {
              Wheelchair_Stop();
              x=2;
              goto GetX;
            }
           else if(x==2) {
             Wheelchair_Go_L();
             x=3;
             goto GetX;
           }
           else if(x==3) {
             Wheelchair_Stop();
             x=0;
             goto GetX;
           }
           GetX:
           x=x;     
        }

Seems to me that you just want to skip the other ifs…

What you want to implement here is a state machine.

Use a switch statement. No more need for goto.

Take a look here in the Arduino reference

The alternative is if / else if or switch. As you already have if / else if, you will eventually end at x=x anyway, regardless of the value of x.

Good point
I use switch and it work fine…but the main problem still ON

Can I Separate conditions from each other. So that the main condition is divided into two parts

Part 1 (Avg_Raw>=103 && Avg_Raw<=200) its own condition,

and the second part ( attentions) its own condition

Again, sorry for my weak English

       On_Flag = 1;
        Off_Flag = 0;

Why 2 variables, one the opposite of the other ?

        On_Flag = 1;
        Off_Flag = 0;

Should they be set the opposite way somewhere ?

Can I Separate conditions from each other. So that the main condition is divided into two parts

Part 1 (Avg_Raw>=103 && Avg_Raw<=200) its own condition,

and the second part ( attentions) its own condition

Yes you can.

Will it solve your problem? I don’t know.

What I see is:

if((Avg_Raw>=201) && (Avg_Raw<=600)) {
  if (c==0) {Do the state machine}
  else if (c==1) {
    Wheelchair_Stop();
    if((attention>=65) && (attention<=100) {Wheelchair_Forword();}
  }
}

Except that you have this goto just before the (if((attention…) that prevents the if statement to ever get reached.

jbellavance:
Yes you can.

Will it solve your problem? I don’t know.

What I see is:

if((Avg_Raw>=201) && (Avg_Raw<=600)) {

if (c==0) {Do the state machine}
  else if (c==1) {
    Wheelchair_Stop();
    if((attention>=65) && (attention<=100) {Wheelchair_Forword();}
  }
}


Except that you have this goto just before the (if((attention...) that prevents the if statement to ever get reached.

Nop the problem still the same

Ok, on the other hand , can any one make a code to do this function:

*Raw_value for make condetion, and its 2 condition:

1- *attention for go forward
2- *eyeblink for go left

When i get raw_value once work on condition (1)
When i get the raw_value twice work on condition (2)

What changes did you make to your code?

Please post the most recent code.

if((Avg_Raw>=201) && (Avg_Raw<=600))
{
switch (c)
{
Case 0: 
Serial.print("order 1"); 
Serial print ("\n"); 
if (attention> 65)
{
Wheelchair_Forward();
}
C=1;
break ;
Case 1: 
Serial print ("order 2"); 
Serial print ("\n"); 
Eye Blink ();
C=0;
 break; 
default:
break;
}
}

Like u see
I make two part to make different function… But it doesn’t work like I won’t!!
For example
The wheelchair can’t move forward if I didn’t make the avg_raw and the atention value in the same time

Sooooooo … Any ideas??

BTW i forget to mention something... My code project is for make brain control a wheelchair by using mindwave headset

switch (c)
C=1;

c and C are different variables.

You have not posted your whole program so it is impossible to tell whether that is significant.

switch (c)
{
Case 0:

Before you post code, do two things.

First, make sure it ACTUALLY compiles. This crap won’t.
Second,
Use
Tools
+
Auto
Format
so
your
code
doesn’t
look
like
shit.