Another break; out of loop problem. Help

Hello on and all.

I have a problem with my code. All in my code is working, but not this part here.
I can jump in to the while loop and run the code with no problem. But the break when runState is HIGH do not work.
I have test the runState and its is working, but not when I use it in this while loop.

I use a while(1) because when I recive a HIGH on a pin, I witch to stop my robot and only run again when i recive a HIGH on another PIN.

void charging(void)
{
while(1)
{
digitalWrite(leftDIR, LOW);
digitalWrite(rightDIR, LOW);
analogWrite(leftPWM, 0);
analogWrite(rightPWM, 0);
digitalWrite(cutterDIR, LOW);
analogWrite(cutterPWM, 0);
if (runState == HIGH){
break;
}
}
}

Thanks in advance
//Mads

difficult to help without the complete code

runState does not change in the while loop so if it is LOW when the loop is entered it will remain LOW and the loop will not exit. You could be changing runState in an interrupt, but how would we know ?

Her is my complete code. All work, but not my while(1) loop. break do not work.

//Setting up the Hardware pins
// First the line following Coil sensor
int coilRight = 0; //Left coil sensor is on pin A0
int coilLeft = 1; //Right coil sensor is on pin A1

//setup Current sensor
//int current = A2; //Current sensor is on pin A2

//Inputs
int runMower = 12; //To start the mower
int chargingMower = 6; //Standby the mower for charging
int bumpSensor = 7; //Stop the mower if it hits something

//Setting up the Motor Controllers
int leftDIR = 4;
int rightDIR = 2;
int cutterDIR = 8;
int leftPWM = 11;
int rightPWM = 3;
int cutterPWM = 9;
const char bothSpeed = 80; //Sets how fast the motors will spin (0 to 254)

//Here we set up variable that will hold the ADC value representing the line sensor values
int leftSees = 0; //A0 ADC value (0 to 1023)
int rightSees = 0; //A1 ADC value (0 to 1023)
//int currentSees = 0; //A2 ADC value (0 to 1023)

//bumpState, runMower and charging variable that will change
int bumpState = 0;
int runState = 0;
int chargingState = 0;

void setup()
{

//Make sure to set all of our control signal pins as input and output
pinMode(leftDIR, OUTPUT);
pinMode(rightDIR, OUTPUT);
pinMode(cutterDIR, OUTPUT);
pinMode(bumpSensor, INPUT);
pinMode(runMower, INPUT);
pinMode(chargingMower, INPUT);
TCCR2B = TCCR2B & 0b11111000 | 0x01; // Set PWM Freq. to 31.250Khz (its nesserey to do so else the guide sensor will fail)

}

void loop()
{
//Read the state of the charging value:
chargingState = digitalRead(chargingMower);

//Delay a little bit
delay(5);

//Read the state of the runMower value:
runState = digitalRead(runMower);

//Delay a little bit
delay(5);

//Read the state of the bumpSensor value:
bumpState = digitalRead(bumpSensor);

//Delay a little bit
delay(5);

//Start by reading the left sensor on A0
int leftCoil = analogRead(coilLeft);

//Delay a little bit
delay(5);

//Read the right sensor connected A1
int rightCoil = analogRead(coilRight);

//Next, we run the motors based on the sensor reading

//If both sensors see black (ADC value greater than 800), then back up
if ((leftCoil >= 800)&&(rightCoil >= 800)) reverse();

//Otherwise, if only the left sensor sees black, then turn off the left motor
//so the robot veer to the left
else if ((leftCoil >= 800)&&(rightCoil < 800)) turnLeft();

//Otherwise, if only the right sensor sees black, then turn off the right motor
//so the robot veer to the right
else if ((leftCoil < 800)&&(rightCoil >= 800)) turnRight();

//If mower hit something then standby
else if (bumpState == HIGH) standby();

//Stop mower when in charging posison
else if (chargingState == HIGH) charging();

//Otherwise, move forward
else forward();

}

//Turn right by turning off the right motor
//i.e disable the PWM to that wheel
void turnRight(void)
{
digitalWrite(leftDIR, HIGH);
digitalWrite(rightDIR, HIGH);
analogWrite(leftPWM, bothSpeed);
analogWrite(rightPWM, 0);
digitalWrite(cutterDIR, HIGH);
analogWrite(cutterPWM, 60);
}

//Turn left by turning off the left motor
//i.e disable the PWM to that wheel
void turnLeft(void)
{
digitalWrite(leftDIR, HIGH);
digitalWrite(rightDIR, HIGH);
analogWrite(leftPWM, 0);
analogWrite(rightPWM, bothSpeed);
digitalWrite(cutterDIR, HIGH);
analogWrite(cutterPWM, 60);
}

//Move forward by enabling both wheels
void forward(void)
{
digitalWrite(leftDIR, HIGH);
digitalWrite(rightDIR, HIGH);
analogWrite(leftPWM, bothSpeed);
analogWrite(rightPWM, bothSpeed);
digitalWrite(cutterDIR, HIGH);
analogWrite(cutterPWM, 60);
}

//Reverse by enabling both wheels
void reverse(void)
{
digitalWrite(leftDIR, LOW);
digitalWrite(rightDIR, LOW);
analogWrite(leftPWM, bothSpeed);
analogWrite(rightPWM, bothSpeed);
digitalWrite(cutterDIR, LOW);
analogWrite(cutterPWM, 0);
}

//Standby the mower
void standby(void)
{
digitalWrite(leftDIR, LOW);
digitalWrite(rightDIR, LOW);
analogWrite(leftPWM, 0);
analogWrite(rightPWM, 0);
digitalWrite(cutterDIR, LOW);
analogWrite(cutterPWM, 0);
}

//Standby the mower for charging
void charging(void)
{
while(1)
{
digitalWrite(leftDIR, LOW);
digitalWrite(rightDIR, LOW);
analogWrite(leftPWM, 0);
analogWrite(rightPWM, 0);
digitalWrite(cutterDIR, LOW);
analogWrite(cutterPWM, 0);
if (runState == HIGH){
break;
}
}
}

but not my while(1) loop. break do not work.

Unless you read runState in an interrupt( or it was HIGH to start with), how could it possibly?

As I said before, runState does not change in the while loop so if it is LOW when the loop is entered it will remain LOW and the loop will not exit. In order to exit the while loop you need to read an input inside it and exit if the input changes.

Perhaps the problem would be clearer if the while loop looked like this

 while(runState == LOW)
  {
    digitalWrite(leftDIR, LOW);
    digitalWrite(rightDIR, LOW);
    analogWrite(leftPWM, 0);
    analogWrite(rightPWM, 0);
    digitalWrite(cutterDIR, LOW);
    analogWrite(cutterPWM, 0);
  }

Now can you see that the while loop will never end because runState will never become HIGH ?

Okay thanks for the answer, but no its sill don´t work... I have no Idea why.

Okay thanks for the answer, but no its sill don´t work... I have no Idea why.

And without seeing your code, neither do we.

Okay sorry, I get it to work now. I while(digitalRead(runMower) == LOW)
So the code is.

while(digitalRead(runMower) == LOW)
{
digitalWrite(leftDIR, LOW);
digitalWrite(rightDIR, LOW);
analogWrite(leftPWM, 0);
analogWrite(rightPWM, 0);
digitalWrite(cutterDIR, LOW);
analogWrite(cutterPWM, 0);
}

Thanks UKHeliBob, for point me in the right direction.

Thants to all and have a have a nice day. Im happy now.
Mads