really lost help me

hello guys I really need help. I am working on mindcontrolled wheel chair project.Using neurosky mindwave device.I have developed blink strength code on matlab which is working fine and giving proper blink strength values.Now in arduino i am talking live values from matlab and sending them.here i am simply using 4 leds which indicate direction.Now this is just testing code i made.But problem i am facing is that implementation of while loop and if statement is not working properly.when i blink and my blink strength value is between 60 and 100 it should go in while loop.But then it does not ask me for if statement value it directly initiate the program after it.Help me how i can fix it

int blinkstrength;
int count=0;

const int forward_led=2;
const int stop_led=3;
const int left_led=4;
const int right_led=5;




void setup() {
  Serial.begin(9600);
  pinMode(forward_led,OUTPUT);
  pinMode(stop_led,OUTPUT);
  pinMode(left_led,OUTPUT);
  pinMode(right_led,OUTPUT);
  }

void loop() {
  if(Serial.available()>0)
  {
    blinkstrength=Serial.read();
    count = 0;
   
      while (blinkstrength > 60 && blinkstrength < 100  )
      {
       
        if (blinkstrength > 60 && blinkstrength < 100 ){
       digitalWrite(forward_led, HIGH);
       delay (500);
       digitalWrite(stop_led,HIGH);
       delay (500);
       digitalWrite(left_led ,HIGH);
       delay (500);
       digitalWrite(right_led,HIGH);
     
    count =count + 1;
   
    break;}
    

  
      }
//  } 
  
  if ( count == 1)
    {
      digitalWrite(forward_led, LOW);
      
    digitalWrite(stop_led,LOW);
   
    digitalWrite(left_led ,LOW);
   
    digitalWrite(right_led,HIGH);
    
    }


  }
}
    while (blinkstrength > 60 && blinkstrength < 100  )
    {
      if (blinkstrength > 60 && blinkstrength < 100 )
      {
        digitalWrite(forward_led, HIGH);
        delay (500);
        digitalWrite(stop_led, HIGH);
        delay (500);
        digitalWrite(left_led , HIGH);
        delay (500);
        digitalWrite(right_led, HIGH);
        count = count + 1;
        break;
      }
    }

Once the program enters this while loop how will it ever exit because the value of blinkstrength never changes within it ?

What is the purpose if the if statement ? You already know that blinkstrength is in the target range because of the while statement. What do you think that the break; statement breaks out of ?

Thats the thing it breaks out of while statement.You see after that there is if statement with count it executes that but my main goal is that when i blink once and my blink strength range is between 60 to 100 it enters while loop and when i blink again and its range is between 60 to 100 it should execute that if statement inside while loop.But what really happens when i blink once and blink strength range is between 60 to 100 the whole statement runs .Do u get my point ?

What is the purpose of the if statement ? It is always going to return true because it is nested in the while loop and has the same condition

according to my logic the purpose of if statement is that once it enters in while loop it should again ask for blinkstrength value and if i again blink it should consider that value

The duplicate topic in Robotics has been removed

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

according to my logic the purpose of if statement is that once it enters in while loop it should again ask for blinkstrength value

How will it ask for the value again once it is stuck in he while loop ? Incidentally, without a prompt how would the user know that a response was required ?

Forget about matlab for now and change your loop to this:

void loop() {
  if (Serial.available() > 0)
  {
    blinkstrength = Serial.read();
    Serial.println(blinkstrength);

  }
}

Type 60 into the serial monitor and you'll probably be surprised by what you see. Serial.read() reads bytes, not integers.

Now change the loop to this:

void loop() {
  if (Serial.available() > 0)
  {
    blinkstrength = Serial.parseInt();
    Serial.println(blinkstrength);

  }
}

Type 60 into the serial monitor and you will see 60 printed. This will likely be too slow for wheelchair response but you can deal with that by either setting a timeout or processing the serial data yourself.

Once you are properly reading integers remove the while loop in loop(), leaving just the if statement after reading the blink strength.

  • You never read the serial input again once you enter the while loop
  • Since the if statement checks the same condition it writes HIGH to the LEDs and then exits the while loop
  • Given the above conditions count will be 1 when it exits the while loop and the LOW will be written to the first 3 LEDs.

You should dispense with the while loop and let loop() do the looping. The following should be closer to what you want. It checks for serial input each time loop() is executed. If there is no serial input or blinkstrength is not between 60 and 100 then it does nothing. I also formatted the code to be readable.

int blinkstrength;
int count = 0;

const int forward_led = 2;
const int stop_led = 3;
const int left_led = 4;
const int right_led = 5;

void setup() 
{
  Serial.begin(9600);
  pinMode(forward_led, OUTPUT);
  pinMode(stop_led, OUTPUT);
  pinMode(left_led, OUTPUT);
  pinMode(right_led, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0)
  {
    blinkstrength = Serial.read();

    if (blinkstrength > 60 && blinkstrength < 100 ) 
    {
      digitalWrite(forward_led, HIGH);
      delay (500);
      digitalWrite(stop_led, HIGH);
      delay (500);
      digitalWrite(left_led, HIGH);
      delay (500);
      digitalWrite(right_led, HIGH);
      digitalWrite(forward_led, LOW);
      digitalWrite(stop_led, LOW);
      digitalWrite(left_led, LOW);
      digitalWrite(right_led, HIGH);
    }
  }
}

Good point. If OP intended to read a byte then the value is range 0 to 255.

OP states values are being sent from matlab to "here". If by "here" OP means over the serial link then the intent may be to read the raw data.

my neurosky mindwave devices is connected with matlab i am taking blink values from matlab and values range from 0 to 255Ii converted it from raw data to digital.BUT the problem i face now i need to make commands simple for user i also made this code which work perfectly fine .But problem is that it is hard for user to keep track of blinkstrength. so what i intend to do that is if user blink twice the wheelchair move forward if blink once it stops and so on. For that reason i was trying to use while loop to do that to record how many blinks user did

int Blinkstrenght;
const int forward = 2;
const int stopp    = 3;
const int left    = 4;
const int right   = 5;


void setup()
{
Serial.begin(9600);
pinMode(forward, OUTPUT);
pinMode(stopp, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);

}

void loop()
{
  if(Serial.available()>0)
  {
    Blinkstrenght=Serial.read();

    
    
     
    if (Blinkstrenght > 55 && Blinkstrenght < 70 )         
    { 
    digitalWrite(forward, HIGH);
    digitalWrite(stopp,LOW);
    digitalWrite(left ,LOW);
    digitalWrite(right,LOW);
    
    }
    
    if ( Blinkstrenght > 70 && Blinkstrenght < 100 )
    {
      
      digitalWrite (forward,LOW);
      digitalWrite (stopp,HIGH);
      digitalWrite(left ,LOW);
    digitalWrite(right,LOW);
    }
    if ( Blinkstrenght > 100 && Blinkstrenght < 155 )
    {
      
      digitalWrite (forward,LOW);
      digitalWrite (stopp,LOW);
      digitalWrite(left ,HIGH);
    digitalWrite(right,LOW);
    }
    if ( Blinkstrenght > 155 && Blinkstrenght < 240 )
    {
      
      digitalWrite (forward,LOW);
      digitalWrite (stopp,LOW);
      digitalWrite(left ,LOW);
    digitalWrite(right,HIGH);
    }
    
  }
}

right now i am just using leds for testing and giving them directions

Lets say one blink is stop, two is turn left, three is turn right. The only ways I can think of determining whether three blinks means to stop and turn left or just turn right is to read their mind or put a time restraint on how long one blink sequence can be. So blink twice in one second, say, means turn left. To stop and turn right blink once, wait a second then blink three times. You would have to adjust the timeout for a blink sequence by trial and error.

int blinkstrength;
int count = 0;

const int forward_led = 2;
const int stop_led = 3;
const int left_led = 4;
const int right_led = 5;


int blinkCount = 0;                           // # of blinks in sequence
const unsigned long BLINK_TIMEOUT = 1000UL;   // length of timeout in ms
unsigned long firstBlink = 0UL;               // mark time of first blink in sequence

void setup() {
  Serial.begin(9600);
  pinMode(forward_led, OUTPUT);
  pinMode(stop_led, OUTPUT);
  pinMode(left_led, OUTPUT);
  pinMode(right_led, OUTPUT);
}

void loop() {

  // if a blink was registered
  if (Serial.available() > 0)
  {
    // read byte and increment count
    Serial.read();
    blinkCount++;

    // if starting a new sequence, record time
    if ( firstBlink == 0UL) {
      firstBlink = millis();
    } // if

  } // if

  // if we have a blink sequence and timeout has passed
  if ( blinkCount && ((millis() - firstBlink) > BLINK_TIMEOUT) ) {

    Serial.print("blink count:  ");
    Serial.println(blinkCount);

    // reset count and sequence start time
    blinkCount = 0;
    firstBlink = 0UL;

  } // if



}

thanks alot sir i understand what u mean.I have developed another program which is working just 'ok' but not good I will also try this one once again thanks this forum helped me alot.