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;
}
}
}