Author Options:

Arduino Coding help? Answered

I need some guidance on how to write some Arduino code. The physical side is an Arduino mega ADK connected to some buttons and leds. On the code side, I modified the button sketch. I am stuck on how to add multiple buttons. in the void loop. I just need someont to show me how to add 1 after that i can figure out how to add the rest.


const int button1 = 24;     // the number of the pushbutton pin
const int button2 = 25;
const int button3 = 26;
const int button4 = 27;
const int button5 = 28;

const int Bucket1 = 2;
const int Bucket2 = 3;
const int Bucket3 = 4;
const int Bucket4 = 5;
const int Bucket5 = 6;
const int Bucket6 = 7;
const int Bucket7 = 8;
const int Bucket8 = 9;
const int Bucket9 = 10;
const int Bucket10 = 11;
const int Bucket11 = 12;
const int Bucket12 = 13;

int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(Bucket1, OUTPUT);   // declare LED strip pin as output
  pinMode(Bucket2, OUTPUT);
  pinMode(Bucket3, OUTPUT);
  pinMode(Bucket4, OUTPUT);
  pinMode(Bucket5, OUTPUT);
  pinMode(Bucket6, OUTPUT);
  pinMode(Bucket7, OUTPUT);
  pinMode(Bucket8, OUTPUT);
  pinMode(Bucket9, OUTPUT);
  pinMode(Bucket10, OUTPUT);
  pinMode(Bucket11, OUTPUT);
  pinMode(Bucket12, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);

void loop() {

  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);
  buttonState4 = digitalRead(button4);
  buttonState5 = digitalRead(button5);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // turn LED on:
  digitalWrite(Bucket1, HIGH);  // turn LED strip on when button is pressed
  digitalWrite(Bucket2, HIGH);
  digitalWrite(Bucket3, HIGH);
  digitalWrite(Bucket4, HIGH);
  digitalWrite(Bucket5, HIGH);
  digitalWrite(Bucket6, HIGH);
  digitalWrite(Bucket7, HIGH);
  digitalWrite(Bucket8, HIGH);
  digitalWrite(Bucket9, HIGH);
  digitalWrite(Bucket10, HIGH);
  digitalWrite(Bucket11, HIGH);
  digitalWrite(Bucket12, HIGH);
  else {
    // turn LED off:
  digitalWrite(Bucket1, LOW);  // turn LED strip on when button is pressed
  digitalWrite(Bucket2, LOW);
  digitalWrite(Bucket3, LOW);
  digitalWrite(Bucket4, LOW);
  digitalWrite(Bucket5, LOW);
  digitalWrite(Bucket6, LOW);
  digitalWrite(Bucket7, LOW);
  digitalWrite(Bucket8, LOW);
  digitalWrite(Bucket9, LOW);
  digitalWrite(Bucket10, LOW);
  digitalWrite(Bucket11, LOW);
  digitalWrite(Bucket12, LOW);


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 5 years ago

if (buttonstate2==HIGH) {}


Answer 5 years ago

Were would i insert it?


Answer 5 years ago

After the buttonstate1 clause. Copy the other code into the parentheses, and change the pattern of lights.


Answer 5 years ago

And what would i need to put for parentheses?