I put a while statement inside my if statement and it's thrown "expected unqualified-id before'if'" at me. I'm not entirely sure what this means or how to fix it. The idea behind the code is to make a robot move towards the brightest light using LDRs, and I've tried to include a 40 unit buffer between LDRs before it begins to turn.
//LDR pins
const int leftLDR = A1;
const int rightLDR = A2;
//Motor settings
int LeftDirection = 2;
int LeftEnable = 3;
int RightDirection = 4;
int RightEnable = 5;
int speed = 150;
//Buffer for the LDR readings of 40
int threshholdBufferUpperCap = 80;
int threshholdBufferLowerCap = 40;
void setup() {
Serial.begin(9600); //Hardware baud rate
//Initilizing pin modes
pinMode(leftLDR, INPUT);
pinMode(rightLDR, INPUT);
pinMode(LeftDirection, OUTPUT);
pinMode(LeftEnable, OUTPUT);
pinMode(RightDirection, OUTPUT);
pinMode(RightEnable, OUTPUT);
}
void loop() {
//HIGH means the wheels move forwards, LOW means they move backwards.
digitalWrite(LeftDirection, HIGH);
analogWrite(LeftEnable, speed);
digitalWrite(RightDirection, HIGH);
analogWrite(RightEnable, speed);
//Take reading from LDRs
int ldrLeftThreshhold = analogRead(leftLDR);
int ldrRightThreshhold = analogRead(rightLDR);
//if statement so the robot turns towards a light if theres more light hitting the left LDR
if (ldrLeftThreshhold > threshholdBufferUpperCap){
while (ldrRightThreshhold < threshholdBufferLowerCap) {
}
}
//Stop the motors as a sudden direction change might damage the motor
int speed = 0;
delay(250);
digitalWrite(RightDirection, LOW); //Motors moving different directions to allow turning
delay(50);
speed = 150;
}
if (ldrRightThreshhold > threshholdBufferUpperCap){
while (ldrLeftThreshhold < threshholdBufferLowerCap) {
}
}
//Stop the motors as a sudden direction change might damage the motor
int speed = 0;
delay(250);
digitalWrite(LeftDirection, LOW); //Motors moving different directions to allow turning
delay(50);
speed = 150;
}
//Print to serial monitor a quantified measure for the amount of light hitting the LDRs
Serial.print("Left LDR threshhold - ");
Serial.println(ldrLeftThreshhold);
Serial.print("Right LDR threshhold - ");
Serial.println(ldrRightThreshhold);
delay(1500);
}
Aucun commentaire:
Enregistrer un commentaire