Hi, im trying to make a simple push button on/off motor. so when i push 1 button the motor and a green led turn on, when i push the other button the motor and green led turn of, and a red led turns on. im using just a small dc motor. the error i keep getting is that the "motorPin was not declared in this scope" please help i have been trying to figure this out and nothing i've done has fixed it. here's the code.
int buttonPin1 = 2; //Start button
int buttonPin2 = 3; //Stop button
int greenLedPin = 6;//green led start status int
int redLedPin = 7; //red led stop status int motorPin = 9; // the motor's pin
int buttonStatus1 = 0;
int buttonStatus2 = 0;
if (buttonStatus1 == HIGH && buttonStatus2 == LOW) { // if the start button is pressed (AND stop button not) digitalWrite(motorPin, HIGH);// turn the motor ON
digitalWrite(greenLedPin, HIGH); //turn the green led indicator ON
digitalWrite(redLedPin, LOW); //turn the red led indicator OFF }
if (buttonStatus1 == LOW && buttonStatus2 == HIGH) { // if stop button is pressed (AND the start off)
digitalWrite(motorPin, LOW); // turn the motor OFF
digitalWrite(greenLedPin, LOW); //turn the green led indicator OFF
digitalWrite(redLedPin, HIGH); //turn the red led indicator ON
}
}
Variables are "declared" then "defined".
This error is telling you that and more.
Variable "motorPin" was not declared is the part of the error message.
The second part - which may look superficial and /or misleading, is additionally telling you where the variable usage is and is missing.
Compiler errors are part of the troubleshooting process and it is up to programmer to use the error messages to identify the actual problem.
But asking for "more eyes" to find the problem is OK, however, when doing so you should use all the help compiler can give you - activate "verbose" compiler output , outputting ALL - errors and warnings messages, adding "line numbers " to you code, and , mainly - post all the compiler output - memory is cheap!.
No matter how this happened, you have just experienced ONE common challenge in programming - typo error - by having line of code accidentally misplaced AFTER the // comment.