Rocket Deep Space HEadlights Bug Hunt! Test your Troubleshooting.

This bug is mysterious and sly - it is tough to see exactly why the code is acting strangely, even after it’s uploaded to Code Rocket. But hunting down and fixing this bug will give you a better grip on the concept from the project- variables that change throughout a program and how they need to be ‘managed’.

/*Buggy: Press button 12 to 'lock' the headlights of the spaceship on. Press it again to turn them off*/ int headlights = 0; //Variable for the status of the headlights. If the variable = 1, the headlights are on. Otherwise, they're off. void setup(){ pinMode(12,INPUT_PULLUP); //Button 12 pinMode(3,OUTPUT); //LED 3 pinMode(4,OUTPUT); //LED 4 } void loop(){ if(digitalRead(12)==LOW){ //if the button is pressed... headlights++; //...increase the value of 'headlights' by one delay(250); // Wait 250 milliseconds before counting another press } if(headlights == 1){ //If the headlights variable = 1, turn on lights 3 and 4 digitalWrite(3,HIGH); digitalWrite(4,HIGH); } else{ //If headlights variable is anything other than 1, turn off the headlights digitalWrite(3,LOW); digitalWrite(4,LOW); headlights = 1; //resetting the headlights variable to 0 ensures the variable doesn't increase to 2,3,4 and higher. } } // (c) 2018 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 



Need a hint? Click here.