GLCD while statement

i have made a little light seeking robot.
but now i want to add a little GLCD to say wheter it is going forward, left or right.

i used a while statement but it isn’t really working.

this is my code:

#include <glcd.h>
#include <glcd_Buildinfo.h>
#include <glcd_Config.h>



 #include <fonts\Arial14.h>  
  #include "fonts\SystemFont5x7.h"  

const int RightSensor = 1; 
const int LeftSensor = 0; 
const int value = 200;
const int cas = 0;
#define PwmPinMotorA 10
#define PwmPinMotorB 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13



int SensorLeft; 
int SensorRight;
int SensorDifference;

const int  buttonPin = 3;   

// Variables will change:

int buttonPushCounter = 0;  
int buttonState = 0;         
int lastButtonState = 0;     
int stat;
void setup() {
  GLCD.Init(NON_INVERTED);   
  GLCD.ClearScreen();  
  GLCD.SelectFont(System5x7); 
  
  
  pinMode(buttonPin, INPUT);
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
pinMode(LeftSensor, INPUT); 
pinMode(RightSensor, INPUT); 
digitalWrite(1, HIGH); 
digitalWrite(2, HIGH); 
Serial.begin(9600); 
Serial.println(" \nBeginning Light Seeking Behavior"); 
}


void loop() {
  
  // i have also tried to put the code that now in void lcd is here but that did not work either

  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {

      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      Serial.println("off"); 
    }
  }

  lastButtonState = buttonState;


  if (buttonPushCounter % 2 == 0) {
  SensorLeft = 1023 - analogRead(LeftSensor); 
delay(1);
SensorRight = 1023 - analogRead(RightSensor); 
delay(1);
SensorDifference = abs(SensorLeft - SensorRight); 



Serial.print("Left Sensor = "); 
Serial.print(SensorLeft); 
Serial.print("\t");
Serial.print("Right Sensor = "); 
Serial.print(SensorRight);
Serial.print("\t"); 




if (SensorLeft > SensorRight && SensorDifference > 30) {
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, cas);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, value);
Serial.println("Left");
}

if (SensorLeft < SensorRight && SensorDifference > 30) { 
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, cas);
Serial.println("Right"); 
}

else if (SensorDifference < 30) { 
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, value);
Serial.println("Forward"); 
}
Serial.print("\n");
}

   else {
   digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, cas);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, cas); }}

void lcd() {
  while(SensorLeft > SensorRight && SensorDifference > 30) {
    GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Turn Left");  
  }
  
    while(SensorLeft < SensorRight && SensorDifference > 30) {
     GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Turn Right");  
  }
  
      while(SensorDifference < 30) {
    GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Forward!");  
  }

}

i used a while statement but it isn't really working.

Is pretty useless, in terms of being able to help you.

I expect to see XXX on the LCD, but I get SuRoijgft, instead

is a lot more useful.

In the while statement, the condition never changes, so you either have an infinite loop or you never execute the code. While is NOT what you want to be using.

thank you very much for the quick answer.

what would you suggest instead of a while statement, a if statement doesn't work either.

i don't really under stand why you are getting that output.

a if statement doesn't work either.

The if statement and the while statement both do something. Clearly they do not do what you expect or want them to do. But, you have not told us what you want them to do, or what they actually do, so it is hard for us to tell you how to change them to make them do what you want.

excuse me for not have told you.

but i have already found the problem, i have asked the question to early i should have thought about it longer :sweat_smile:

this is my code

#include <glcd.h>
#include <glcd_Buildinfo.h>
#include <glcd_Config.h>



 #include <fonts\Arial14.h>  
  #include "fonts\SystemFont5x7.h"  

const int RightSensor = 1; 
const int LeftSensor = 0; 
const int value = 200;
const int cas = 0;
#define PwmPinMotorA 10
#define PwmPinMotorB 11
#define DirectionPinMotorA 12
#define DirectionPinMotorB 13



int SensorLeft; 
int SensorRight;
int SensorDifference;

const int  buttonPin = 3;   

// Variables will change:

int buttonPushCounter = 0;  
int buttonState = 0;         
int lastButtonState = 0;     
int stat;
void setup() {
  GLCD.Init(NON_INVERTED);   
  GLCD.ClearScreen();  
  GLCD.SelectFont(System5x7); 
  
  
  pinMode(buttonPin, INPUT);
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
pinMode(LeftSensor, INPUT); 
pinMode(RightSensor, INPUT); 
digitalWrite(1, HIGH); 
digitalWrite(2, HIGH); 
Serial.begin(9600); 
Serial.println(" \nBeginning Light Seeking Behavior"); 
}


void loop() {
  

    if(SensorLeft > SensorRight && SensorDifference > 30) {
    GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Turn Left");  
  }
  
    if(SensorLeft < SensorRight && SensorDifference > 30) {
     GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Turn Right");  
  }
  
      if(SensorDifference < 30) {
    GLCD.ClearScreen();
    GLCD.CursorTo(13,2);  
    GLCD.print("Forward!");  
  }

  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {

      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      Serial.println("off"); 
    }
  }

  lastButtonState = buttonState;


  if (buttonPushCounter % 2 == 0) {
  SensorLeft = 1023 - analogRead(LeftSensor); 
delay(1);
SensorRight = 1023 - analogRead(RightSensor); 
delay(1);
SensorDifference = abs(SensorLeft - SensorRight); 



Serial.print("Left Sensor = "); 
Serial.print(SensorLeft); 
Serial.print("\t");
Serial.print("Right Sensor = "); 
Serial.print(SensorRight);
Serial.print("\t"); 




if (SensorLeft > SensorRight && SensorDifference > 30) {
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, cas);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, value);
Serial.println("Left");
}

if (SensorLeft < SensorRight && SensorDifference > 30) { 
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, cas);
Serial.println("Right"); 
}

else if (SensorDifference < 30) { 
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, value);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, value);
Serial.println("Forward"); 
}
Serial.print("\n");
}

   else {
   digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, cas);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, cas); }}