Hi,
For some reason adding certain lines of display code will randomly crash the code. There are no compiling errors, the program takes up only 59% of the program storage and even uploads fine. But then the Arduino doesn't do anything.
This works:
void loop(){
display.clearDisplay();
display.setCursor(0, 0);
display.println("10 seconds until\nlight scanning");
display.println("Place me on a surface...");
display.display();
delay(10000);
display.clearDisplay();
display.setCursor(0, 0);
display.println("Setting light\nthreshold...");
display.display();
light_Threshold = 0;
getDistance();
while (cm > 5){
getDistance();
getLight();
if (light_Value > light_Threshold){ // New brightest spots are set as the threshold
light_Threshold = light_Value;
}
analogWrite(motorR_F_Pin, speed_Value); // Turn the robot
analogWrite(motorL_B_Pin, speed_Value);
delay(200);
digitalWrite(motorR_F_Pin, LOW);
digitalWrite(motorL_B_Pin, LOW);
delay(200);
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Light threshold\nset at: ");
display.println(light_Threshold);
display.println("\nTime to go to the\nlight!");
display.display();
delay(4000);
getLight();
while (light_Value < (light_Threshold-10)){ // Turn the robot to face the brightest light
digitalWrite(motorR_F_Pin, HIGH);
digitalWrite(motorL_B_Pin, HIGH);
delay(200);
digitalWrite(motorR_F_Pin, LOW);
digitalWrite(motorL_B_Pin, LOW);
delay(200);
getLight();
}
int found = 0; // When this = 1 the robot is in front of the brightest light
while (found != 1){
getLight();
getDistance();
display.clearDisplay();
display.setCursor(0, 0);
display.display();
if (ldrR_Value > ldrL_Value && light_Value <= light_Threshold){ // Light is on the right of robot
}
}
}
But as soon as I add one more print line, the code crashes and does nothing:
void loop(){
display.clearDisplay();
display.setCursor(0, 0);
display.println("10 seconds until\nlight scanning");
display.println("Place me on a surface...");
display.display();
delay(10000);
display.clearDisplay();
display.setCursor(0, 0);
display.println("Setting light\nthreshold...");
display.display();
light_Threshold = 0;
getDistance();
while (cm > 5){
getDistance();
getLight();
if (light_Value > light_Threshold){ // New brightest spots are set as the threshold
light_Threshold = light_Value;
}
analogWrite(motorR_F_Pin, speed_Value); // Turn the robot
analogWrite(motorL_B_Pin, speed_Value);
delay(200);
digitalWrite(motorR_F_Pin, LOW);
digitalWrite(motorL_B_Pin, LOW);
delay(200);
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Light threshold\nset at: ");
display.println(light_Threshold);
display.println("\nTime to go to the\nlight!");
display.display();
delay(4000);
getLight();
while (light_Value < (light_Threshold-10)){ // Turn the robot to face the brightest light
digitalWrite(motorR_F_Pin, HIGH);
digitalWrite(motorL_B_Pin, HIGH);
delay(200);
digitalWrite(motorR_F_Pin, LOW);
digitalWrite(motorL_B_Pin, LOW);
delay(200);
getLight();
}
int found = 0; // When this = 1 the robot is in front of the brightest light
while (found != 1){
getLight();
getDistance();
display.clearDisplay();
display.setCursor(0, 0);
display.print("Centre light value: "); // This is the line that breaks it all
display.display();
if (ldrR_Value > ldrL_Value && light_Value <= light_Threshold){ // Light is on the right of robot
}
}
}
This isn't the whole code but I can post the entire code if that's needed. Can anyone tell me what's going on?
Thank you for your help!