Can someone tell me what I have done wrong with this? I added two additional relays and pushbuttons to the original code and now I get an error message stating that 'readButtonPin5' was not declared in this scope. I'm at a loss.
Please help. Here's the code:
/* Pushbutton controlled relays. Modified by Jared Elliott.
* Control 4 relays via arduino using pushbuttons.
* There is a delay after pressing the pushbutton before it turns off.
* The delays can be adjusted to suit your needs.
* Additional relays can be added by copying the
* lines of code and adding the required setup code.
*
* This code was originally by Samir Tawfik on YouTube.
*/
#define RELAY_ON 0 // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1 // and off when HIGH (1). These #defines just help keep track.
const int RelayPin1 = 8; // Pin numbers for the relays.
const int RelayPin2 = 9; // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
const int RelayPin5 = A0;
const int RelayPin6 = A1;
const int ButtonPin1 = 2; // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
const int ButtonPin5 = 6;
const int ButtonPin6 = 7;
const int Duration1 = 1000; // Number of milliseconds that the relays
const int Duration2 = 2000; // run after the buttons are pressed.
const int Duration3 = 3000;
const int Duration4 = 4000;
const int Duration5 = 5000;
const int Duration6 = 6000;
// variables
byte State1 = RELAY_OFF; // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF; // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
byte State5 = RELAY_OFF; // Uncomment to use for an additional relay.
byte State6 = RELAY_OFF;
unsigned long currentMillis = 0; // Stores the value of millis() in each iteration of loop().
unsigned long TimerMillis1 = 0; // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
unsigned long TimerMillis5 = 0; // Uncomment to use for an additional relay.
unsigned long TimerMillis6 = 0;
void setup()
{
// --Debug--
Serial.begin(9600);
Serial.println("Starting Relay Controller.ino");
digitalWrite(RelayPin1, RELAY_OFF); // Relays are active-LOW. Initialize relay pins
digitalWrite(RelayPin2, RELAY_OFF); // high so the relays are inactive at startup/reset.
digitalWrite(RelayPin3, RELAY_OFF);
digitalWrite(RelayPin4, RELAY_OFF);
digitalWrite(RelayPin5, RELAY_OFF); // Uncomment for an additional relay.
digitalWrite(RelayPin6, RELAY_OFF);
pinMode(RelayPin1, OUTPUT); // Then set the pins as outputs.
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(RelayPin5, OUTPUT); // Uncomment for an additional relay.
pinMode(RelayPin6, OUTPUT);
pinMode(ButtonPin1, INPUT_PULLUP); // Set the float sensor pins as inputs
pinMode(ButtonPin2, INPUT_PULLUP); // and use the internal pullup resistors.
pinMode(ButtonPin3, INPUT_PULLUP);
pinMode(ButtonPin4, INPUT_PULLUP);
pinMode(ButtonPin5, INPUT_PULLUP);
pinMode(ButtonPin6, INPUT_PULLUP);
}
void loop()
{
// Get the current clock count
currentMillis = millis() + Duration1; // We add the countdown timer duration to
// the clock to prevent the relay from
// running at boot time, whele the clock
// counter is still below the timer value.
// Call the functions that do the work
readButtonPin1(); // Check each button and decide whether
readButtonPin2(); // to start the relay and countdown timer.
readButtonPin3();
readButtonPin4();
readButtonPin5();
readButtonPin6();
triggerRelay(); // Actually toggles the relay pins based on
// the data from the above functions.
}
// ---Worker functions---
void readButtonPin1() // Repeat comments for each 'readButtonPin' iteration.
{
if (digitalRead(ButtonPin1) == LOW) // If the button is tripped (pulled low)...
{
State1 = RELAY_ON; // ...then trigger the relay pin
TimerMillis1 = currentMillis; // and set the relay countdown timer to the current clock time.
}
if (currentMillis - TimerMillis1 >= Duration1) //If the countdown timer has expired...
{
State1 = RELAY_OFF; // ...then turn off the relay pin
TimerMillis1 = 0; // and reset the timer to 0 for the next trigger.
}
}
void readButtonPin2()
{
if (digitalRead(ButtonPin2) == LOW)
{
State2 = RELAY_ON;
TimerMillis2 = currentMillis;
}
if (currentMillis - TimerMillis2 >= Duration2)
{
State2 = RELAY_OFF;
TimerMillis2 = 0;
}
}
void readButtonPin3()
{
if (digitalRead(ButtonPin3) == LOW)
{
State3 = RELAY_ON;
TimerMillis3= currentMillis;
}
if (currentMillis - TimerMillis3 >= Duration3)
{
State3 = RELAY_OFF;
TimerMillis3 = 0;
}
}
void readButtonPin4()
{
if (digitalRead(ButtonPin4) == LOW)
{
State4 = RELAY_ON;
TimerMillis4 = currentMillis;
}
if (currentMillis - TimerMillis4 >= Duration4)
{
State4 = RELAY_OFF;
TimerMillis4 = 0;
}
void readButtonPin5()
{
if (digitalRead(ButtonPin5) == LOW)
{
State5 = RELAY_ON;
TimerMillis5 = currentMillis;
}
if (currentMillis - TimerMillis5 >= Duration5)
{
State5 = RELAY_OFF;
TimerMillis5 = 0;
}
void readButtonPin6()
{
if (digitalRead(ButtonPin6) == LOW)
{
State6 = RELAY_ON;
TimerMillis6 = currentMillis;
}
if (currentMillis - TimerMillis6 >= Duration6)
{
State6 = RELAY_OFF;
TimerMillis6 = 0;
}
}
void triggerRelay()
{
digitalWrite(RelayPin1, State1); // Toggle the relay pins on and off based on
digitalWrite(RelayPin2, State2); // the State from the readButton functions.
digitalWrite(RelayPin3, State3);
digitalWrite(RelayPin4, State4);
digitalWrite(RelayPin5, Stete5);
digitalWrite(RelayPin5, Stete6);
}
You missed a closing bracket. Auto format the sketch and it will leap out at you.
aarg:
You missed a closing bracket. Auto format the sketch and it will leap out at you.
I did autoformat and I guess I'm just as blind as I'm ignorant about what I did wrong. Could you possibly show me what bracket I missed?
Thanks for replying.
readButtonPin5() has no closing bracket. Not sure why you can't see that all the following code is indented too far.
Edit - It's worse, you have missing closing brackets all over the place. Better do a complete cleanup.
Sorry, it's not our job to do that.
You are talking about the '{' brackets right? So this code snippet would be wrong? There is a bracket at the first then one at the end. Should there be brackets at every indent?
{
// Get the current clock count
currentMillis = millis() + Duration1; // We add the countdown timer duration to
// the clock to prevent the relay from
// running at boot time, whele the clock
// counter is still below the timer value.
// Call the functions that do the work
readButtonPin1(); // Check each button and decide whether
readButtonPin2(); // to start the relay and countdown timer.
readButtonPin3();
readButtonPin4();
readButtonPin5();
readButtonPin6();
triggerRelay(); // Actually toggles the relay pins based on
// the data from the above functions.
}
This code compiles just fine. But when I add 'const int ButtonPin5 = 6;' and 'readButtonPin5();' I get the error. Without the two lines the code compiles as is, indented stuff and all.
I understand wanting others to figure out things on their own and I assure you, I have been brainstorming this for a good while. But come on, surely there is someone who can show me what I am doing wrong. I'm not asking for you to write this thing for me.
/* Pushbutton controlled relays. Modified by Jared Elliott.
* Control 4 relays via arduino using pushbuttons.
* There is a delay after pressing the pushbutton before it turns off.
* The delays can be adjusted to suit your needs.
* Additional relays can be added by copying the
* lines of code and adding the required setup code.
*
* This code was originally by Samir Tawfik on YouTube.
*/
#define RELAY_ON 0 // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1 // and off when HIGH (1). These #defines just help keep track.
const int RelayPin1 = 8; // Pin numbers for the relays.
const int RelayPin2 = 9; // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
const int ButtonPin1 = 2; // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
const int Duration1 = 1000; // Number of milliseconds that the relays
const int Duration2 = 2000; // run after the buttons are pressed.
const int Duration3 = 3000;
const int Duration4 = 4000;
// variables
byte State1 = RELAY_OFF; // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF; // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
unsigned long currentMillis = 0; // Stores the value of millis() in each iteration of loop().
unsigned long TimerMillis1 = 0; // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
void setup()
{
// --Debug--
Serial.begin(9600);
Serial.println("Starting Relay Controller.ino");
digitalWrite(RelayPin1, RELAY_OFF); // Relays are active-LOW. Initialize relay pins
digitalWrite(RelayPin2, RELAY_OFF); // high so the relays are inactive at startup/reset.
digitalWrite(RelayPin3, RELAY_OFF);
digitalWrite(RelayPin4, RELAY_OFF);
pinMode(RelayPin1, OUTPUT); // Then set the pins as outputs.
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(ButtonPin1, INPUT_PULLUP); // Set the float sensor pins as inputs
pinMode(ButtonPin2, INPUT_PULLUP); // and use the internal pullup resistors.
pinMode(ButtonPin3, INPUT_PULLUP);
pinMode(ButtonPin4, INPUT_PULLUP);
}
void loop()
{
// Get the current clock count
currentMillis = millis() + Duration1; // We add the countdown timer duration to
// the clock to prevent the relay from
// running at boot time, whele the clock
// counter is still below the timer value.
// Call the functions that do the work
readButtonPin1(); // Check each button and decide whether
readButtonPin2(); // to start the relay and countdown timer.
readButtonPin3();
readButtonPin4();
triggerRelay(); // Actually toggles the relay pins based on
// the data from the above functions.
}
// ---Worker functions---
void readButtonPin1() // Repeat comments for each 'readButtonPin' iteration.
{
if (digitalRead(ButtonPin1) == LOW) // If the button is tripped (pulled low)...
{
State1 = RELAY_ON; // ...then trigger the relay pin
TimerMillis1 = currentMillis; // and set the relay countdown timer to the current clock time.
}
if (currentMillis - TimerMillis1 >= Duration1) //If the countdown timer has expired...
{
State1 = RELAY_OFF; // ...then turn off the relay pin
TimerMillis1 = 0; // and reset the timer to 0 for the next trigger.
}
}
void readButtonPin2()
{
if (digitalRead(ButtonPin2) == LOW)
{
State2 = RELAY_ON;
TimerMillis2 = currentMillis;
}
if (currentMillis - TimerMillis2 >= Duration2)
{
State2 = RELAY_OFF;
TimerMillis2 = 0;
}
}
void readButtonPin3()
{
if (digitalRead(ButtonPin3) == LOW)
{
State3 = RELAY_ON;
TimerMillis3= currentMillis;
}
if (currentMillis - TimerMillis3 >= Duration3)
{
State3 = RELAY_OFF;
TimerMillis3 = 0;
}
}
void readButtonPin4()
{
if (digitalRead(ButtonPin4) == LOW)
{
State4 = RELAY_ON;
TimerMillis4 = currentMillis;
}
if (currentMillis - TimerMillis4 >= Duration4)
{
State4 = RELAY_OFF;
TimerMillis4 = 0;
}
}
void triggerRelay()
{
digitalWrite(RelayPin1, State1); // Toggle the relay pins on and off based on
digitalWrite(RelayPin2, State2); // the State from the readButton functions.
digitalWrite(RelayPin3, State3);
digitalWrite(RelayPin4, State4);
}
The 'readButtonPin5' was not declared in this scope error is due to the function above it, readButtonPin4() not having a closing }. You might have zapped that during your copy/pastes, if it was there before. Fix that then the error moves to 6, becasue you don't have a } on 5 either.
It's good practice to comment your closing } so you know what they belong to. I added the lines arrowed below:
void readButtonPin4()
{
if (digitalRead(ButtonPin4) == LOW)
{
State4 = RELAY_ON;
TimerMillis4 = currentMillis;
}
if (currentMillis - TimerMillis4 >= Duration4)
{
State4 = RELAY_OFF;
TimerMillis4 = 0;
}
}// end of readButtonPin4 <<<<<<<<<<<<<<<<<<<
void readButtonPin5()
{
if (digitalRead(ButtonPin5) == LOW)
{
State5 = RELAY_ON;
TimerMillis5 = currentMillis;
}
if (currentMillis - TimerMillis5 >= Duration5)
{
State5 = RELAY_OFF;
TimerMillis5 = 0;
}
}// end of readButtonPin5 <<<<<<<<<<<<<<<<<<
If you hover cursor next to a bracket the IDE highlights its mate, if it can find one, with a small box.
You also have two instances of State spelled Stete.
kenwood120s:
The 'readButtonPin5' was not declared in this scope error is due to the function above it, readButtonPin4() not having a closing }. You might have zapped that during your copy/pastes, if it was there before. Fix that then the error moves to 6, becasue you don't have a } on 5 either.
It's good practice to comment your closing } so you know what they belong to. I added the lines arrowed below:
If you hover cursor next to a bracket the IDE highlights its mate, if it can find one, with a small box.
You also have two instances of State spelled Stete.
Thank you sir. I did find the spelling mistakes and the reason for that is probably because I had my font set too small. I was trying to fit more on the screen and couldn't see it well.
But due to your reply, I now understand what I did wrong. I have learned what to look for and some best practices for writing sketches. This error shouldn't happen again.
Thank you for helping!!!