Where do you look for help programming 17 buttons for the Mege 2560 where there are also 17 led and 14 relays. I can't find a solution for setting up the buttons, I get errors every time I try to program a button (was not declared in this scope) and (error: 'buttonState' was not declared in this scope
buttonState = digitalRead(buttonPin1)![]()
That error gets all buttons
Then I don't understand how the buttons should be defined, (pinMode(bottonPin1, INPUT)
buttons 1 to 17 don't work.
I just posted this. Perhaps it will be a good starting point for you
Welcome to the forum
Start with a single button
const byte buttonPin0 = 2; // using pin 2 for button0
Then in setup()
Serial.begin(115200); //set Serial port to 115200 baud
pinMode(buttonPin0, INPUT_PULLUP); //turn on pullup resistor for buttonPin0
Then in loop()
Serial.println(digitalRead(buttonPin0)); //print current state of buttonPin0
So, all together you have
const byte buttonPin0 = 2; // using pin 2 for button0
void setup()
{
Serial.begin(115200); //set Serial port to 115200 baud
pinMode(buttonPin0, INPUT_PULLUP); //turn on pullup resistor for buttonPin0
}
void loop()
{
Serial.println(digitalRead(buttonPin0)); //print current state of buttonPin0
}
Connect a pushbutton between pin 2 and GND. Upload the sketch. Open the Serial monitor. What happens when you press the button ?
Thank you for the welcome
Cannot use pullup as it has to replace an old switch setup where there is +5V output and pulldown resistor.
I haven't entered Serial into setup at all, so I have to start there.
I'm completely new to Arduino so just need to learn it, but have previously written in Basic(1980)
Thanks for the answer
Trying to type it in
What is it ? // Create button array pushButton buttons[NUM_BUTTONS] = …
Probably a lot easier if you copy and paste instead of typing it in...
What exactly is going to replace the old switch setup ? You cannot use an Arduino pin as a replacement for a switch. You could, however, connect an existing switch with an existing pulldown resistor to an Arduino pin and use just INPUT in pinMode()
After additional review of program, I have found several spelling mistakes which are also in the original post at the top (bottonPin1), after they have been corrected there is only (buttonState = digitalRead(button1Pin)
error, for all 1 - 17
error: 'buttonState' was not declared in this scope
buttonState = digitalRead(buttonPin17);
note: suggested alternative: 'buttonPin17'
buttonState = digitalRead(buttonPin17);
I don't quite understand what it means, it says the same thing and where is missing (declare scope)
Which program do you mean ?
The old set-up consisted of switches that were mounted with pulldown resistors on output pins and +5V on input, it went into some modules molded in plastic so it is not possible to see what is happening in the product, consider it as brand new switches there emits +5V when pressed.
I have seen several videos on the web where button was with pulldown and +5V output and connected to Arduino - pinMode(buttonPin, INPUT); so it should work, but all examples are single button only, maybe I got it wrong
sketch
I am Microsoft trained and there it is called a program, is that what you mean in your question
You said
I do not know which program you reviewed
Is it the word program that doesn't match or are you referring to the sketch I wrote to make it work or am I completely confused.
I was hoping for a solution to 'buttonState' was not declared in this scope, all the above setup is error-free, but I think something is missing for buttonState to be declared
// read the state of the pushbutton1 value:
buttonState = digitalRead(button1Pin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(relayPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
digitalWrite(relayPin1, LOW);
}
You were the one that referred to a program that you had reviewed. I assume that it is your Arduino sketch but I cannot be sure
Please post the full program that you reviewed. buttonState is not declared in the code snippet that you just posted but for all we know it could be declared somewhere else in the sketch, hence the need for you to post the whole sketch
I'm aware that it has to be declared somewhere earlier, but how do you do it, I've seen (int buttonState = 0;) but it's not enough when there are 17 buttons, it doesn't work.
int buttonState = 0;
is correct, but depending on how you write your sketch you might do that in one of a number of places depending on the scope needed
The best way to read the state of 17 input pins is to put the pin numbers in an array and iterate through it to read the pins
How did you do it in the sketch that you still have not posted ?
Have taken an extract from 1 the following are similar, apart from having a libery for buttom there is nothing else
const int button1Pin = A0;
const int ledPin1 = 22;
const int relayPin1 = 23;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(relayPin1, OUTPUT);
pinMode(button1Pin, INPUT);
Serial.begin(115200);
// int buttonState = 0;
}
void loop
I'm wondering if it's because I use analog pins for input, I've read that they can be reprogrammed to digital but can't find the right syntax
@lachris in case you were wondering, many of the helpers on this forum note that a full code has been requested, and simply hold off, not participating until it's provided. Your latest post demonstrates that you know how to post code, so... Since you've been repeatedly asked, I'm assuming you have no intention of doing so, so I'm heading off now.
@UKHeliBob has a lot of patience.
I wish you luck!
Sorry, but I thought that 390 lines with almost the same text would seem unmanageable, therefore I only took the code for 1 and not all 17 contacts, 17 led and 14 relays
Sorry, I'm head down in about 2100 lines right now. I think you do residents here an insult, assuming they couldn't sort wheat from chaff. More often than not, the problem is evident once we see the whole picture.
The purpose of requesting code is precisely so we can place it in our IDE and assess it. The IDE has code-folding, standard formatting, and other goodies that help immensely.
And, the IDE has shift-ctrl-C, which allows you to import the whole thing to a forum message in one keystroke.
Anyway, good luck, I'm gone.