Turn repetitive code into arrays?

I have got some code worked out for 2 pushbuttons which works. I want to expand to accomadate 9 pushbuttons.

Can I make some of my statements into arrays an run a loop to see which pushbutton has been pressed.

Or is there a better way. Thanks

const int buttonPin  = 7;     // the number of the pushbutton pin
const int buttonPin1 = 8;     // the number of the pushbutton pin
const int ledPin1 =  11;      // the number of the LED pin
const int ledPin2 =  12;      // the number of the LED pin

int buttonState  = 0;         // variable reading pushbutton status
int buttonState1 = 0;         // variable reading pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin1, INPUT);

}

void loop() {
  // read the state of pushbutton
  buttonState  = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);

  if (buttonState == HIGH) {
    digitalWrite(ledPin1, HIGH);
  }

  if (buttonState1 == HIGH) {
    digitalWrite(ledPin2, HIGH);
  }

  // Turn off LED1when button released
  digitalWrite(ledPin1, LOW);
  // Turn off LED2when button released
  digitalWrite(ledPin2, LOW);
}

Can I make some of my statements into arrays

No. You can put pin numbers in an array, and store the states in an array. Code can not be in an array.

const int buttonPin  = 7;     // the number of the pushbutton pin
const int buttonPin1 = 8;     // the number of the pushbutton pin
const int ledPin1 =  11;      // the number of the LED pin
const int ledPin2 =  12;      // the number of the LED pin

Yep, one...one, two... WTF? Consistency is a good thing.

Many thanks Paul.

Just tried

int buttons[4]; //declare array of 4 buttons, so I know I can put into loop and set value = 0 to start.

Buttonstates presume can do same. So thanks again, will see what happens. Anything to shorten code

byte x;
byte pins[] = {2,3,4,5,6,7,8,9,10,};
byte leds[] = {11,12,13,14,15,16,17,18,19,};
void setup(){
  for (x=0; x<9; x=x+1){
  pinMode (pins[x], INPUT_PULLUP); // button/switch connect pin to Gnd when pressed
  pinMode (leds[x],OUTPUT); // pin to LED anode, cathode to resistor to Gnd,  High = On
  }
}
void loop(){
  for (x=0; x<9; x=x+1){
  digitalWrite ( leds[x],digitalRead(pins[x]) );
  }
}

Short enough?

Now that is impressive! Never thought the code would ever get that short. Many thanks

ZOR2:
I have got some code worked out for 2 pushbuttons which works. I want to expand to accomadate 9 pushbuttons.

In case you need more than just a acouple of pushbuttons: Do yoyu know what a "keypad matrix" is?

Readily available keypad matrix hardware is available with 3x4 or 4x4 keypad buttons.

There is an Arduino library available to control such keypad matrizes and read keycodes from them:
http://playground.arduino.cc/Code/Keypad

Typically a 3x4 matrix offers these zwelve buttons:

  • digits '0' up to '9' and special characters '*' and '#'

A 4x4 matrix typically offers the same buttons and additionally 'A', 'B', 'C', and 'D' to detect individually.

What about your multi-pushbutton application?
Could you possibly use an 3x4 or 4x4 keypad matrix instead of many single pushbuttons wired to the Arduino board?
In case yes, you can use the Arduino keypad library to detect pressed buttons.
In case no: Describe your application!

Maybe your actual application has to do a bit more than just switching on an LED when button is down and switching same LED off when button is released.

A 3x4 keypad matrixwith 12 buttons would need 7 digital inputs for wiring to an Arduino (3 rows, 4 columns)
A 4x4 keypad matrix with 16 buttons would need 8 digital inputs (4 rows, 4 columns)

Thanks jurs. I am wanting to use individual buttons so I can arrange them in a meaningful pattern.

x
x x x x x x x
x

Each button pressed is to pass a number for serial transmission output. The project is using HC-12 TX/RX

Recently played with Nrf24lo1 units but the HC-12 units are so much better. Free air supposed to go 1-1.5km. I am now playing with CrossRoads code to not only illuminate LED's but identify from the button pressed the number to get transmitted.

Using int Mycodes[9] = {20,25,30,35,40,45,50,55,60};

The pushbutton pressed is to pass the Mycodes value into a println value.

The remote controller is to send commands for a camera Pan left/Pan right/Tilt up/Tilt down. The other 5 are for congiguring the camera properties.

Regards

A object oriented version of CrossRoad’s code.

Button/LED pairs may be added or deleted by simply adding or deleting entries of the ‘button_status’ array.

#include <Arduino.h>

#define NUM_ENTRIES(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))

class button_status_pair
{
    const uint8_t   k_pinBUTTON;
    const uint8_t   k_pinLED;

public:    
    button_status_pair(uint8_t const pinButton, uint8_t const pinLED)
        : k_pinBUTTON(pinButton), k_pinLED(pinLED)
    {   }

    void init()
    {
        pinMode (k_pinBUTTON, INPUT_PULLUP);
        pinMode (k_pinLED,OUTPUT);
    }

    void process()
    {
        digitalWrite(k_pinLED, digitalRead(k_pinBUTTON));
    }
};

button_status_pair  button_status[] =
{
      button_status_pair( 2, 11)
    , button_status_pair( 3, 12)
    , button_status_pair( 4, 13)
    , button_status_pair( 5, 14)
    , button_status_pair( 6, 15)
    , button_status_pair( 7, 16)
    , button_status_pair( 8, 17)
    , button_status_pair( 9, 18)
    , button_status_pair(10, 18)
};

void loop()
{
    for ( size_t i = NUM_ENTRIES(button_status); i--; )
    {
        button_status[i].process();
    }
}

void setup()
{
    for ( size_t i = NUM_ENTRIES(button_status); i--; )
    {
        button_status[i].init();
    }
}

Thanks for the help lloyddean, I will try and work it all out, I am very new to Arduino code.

Going back to CrossRoads code, I am trying to illuminate the LED of the selected pushbutton which it does perfectly. At the same time retrieve the data of the selected pushbutton (MyArray(x)

My attempts fail! Appreciate being shown how with this code I can get it. Thanks

byte x;
int MyArray[2]={25,40};
byte pins[] = {7,8,};
byte leds[] = {11,12,};
void setup(){

  Serial.begin(9600);
  for (x=0; x<2; x=x+1){
  pinMode (pins[x], INPUT_PULLUP); // button/switch connect pin to Gnd when pressed
  pinMode (leds[x],OUTPUT); // pin to LED anode, cathode to resistor to Gnd,  High = On
  }
}
void loop(){
  for (x=0; x<2; x=x+1){
  digitalWrite ( leds[x],digitalRead(pins[x]) );
  Serial.println(MyArray[x]);  
  }
}

Given that I don't know what the contents of 'MyArray' are, other than integers, I'm not sure what to tell you without a more clear explanation on your part.

Sorry

Sorry, the data to be retrieved are numbers in an array insex that match the value of x (pushbutton selected) . Numeric numbers held in MyArray

int MyArray[2]={25,40};

So if button 1 is pressed I would want to retrieve number 25.

Serial.println(MyArray[x]), x

being the index value of MyArray. Maybe this does not work like this?

const uint8_t   BUTTON_UP   = LOW:
const uint8_t   BUTTON_DOWN = HIGH:

int MyArray[2]  = { 25, 40 };
byte pins[]     = {  7,  8 };
byte leds[]     = { 11, 12 };

void setup()
{
    Serial.begin(9600);

    for ( int x = 0; x < 2; x = x + 1 )
    {
        pinMode(pins[x], INPUT_PULLUP);
        pinMode(leds[x], OUTPUT);
    }
}

void loop()
{
    for ( int x = 0; x < 2; x = x + 1 )
    {
        int stateButton = digitalRead(pins[x]);
        if ( BUTTON_DOWN ==  stateButtonn)
        {
            Serial.println(MyArray[x]);
        }

        digitalWrite(leds[x], stateButton);
    }
}

Thanks, however it errors on the very first line

exit status 1
expected ',' or ';' before ':' token

also changed stateButtonn to stateButton

Regards

Just changed : to ; on first two lines.

Now gives same error on

if ( BUTTON_DOWN == stateButton)

Nearly there!

Now gives same error on

It can't possibly give the same error on a different line of code, since the error, which you stubbornly refuse to post, includes the line number and column number.

That message, generally, means that you need to look at the previous line which is missing a ;.

Don't know why but it was missing a; further down tried again with what I've changed in the post above

I got the errors out

void loop()
{
    for ( int x = 0; x < 2; x = x + 1 )
    {
         stateButton = digitalRead(pins[x]);
         
        if ( BUTTON_DOWN == stateButton);
        {
            Serial.println(MyArray[x]);
        }

        digitalWrite(leds[x], stateButton);
    }
}

I also declared stateButton prior to setup

int stateButton;

The code runs, however both array values appear in the serial monitor. My pushbuttons/pins are held low until being pushed. The monitor immediately fills with both array values. Will play on but thanks for your help

        if ( BUTTON_DOWN == stateButton);

If statements rarely end with ;.

Just removed a ; which I had added trying to stop errors. Now it shows both array numbers 25,40 during loop, but if I hold down the button it shows a repitition of the correct number on each pushbutton.

So now if I can get nothing when the loop is running, then get the correct number when a pushbutton is selected and somehow keep the number and not change it until another button is pressed.

It’s basically a remote control unit.

Code now:

void loop()
{
    for ( int x = 0; x < 2; x = x + 1 )
    {
         stateButton = digitalRead(pins[x]);
         
        if ( BUTTON_DOWN == stateButton)
        {
            Serial.println(MyArray[x]);
        }

        digitalWrite(leds[x], stateButton);
    }
}

I removed the ; I added at the end of line

if ( BUTTON_DOWN == stateButton)

Thanks

Thanks Paul, yes was my mistake not knowing, however learned for next time. Regards