++ SOLVED ++array label question

I was wondering if it is possible to label a array of switches ?
For example :
Switch one = LED Red
Switch Two = LED Green
Switch Three = LED Yellow

First i did an easy test with one button , find youtube example:

I was test the sketch in the COOKBOOK , Recipe 2.4 ( Groups and values )

/*
 array sketch
 an array of switches controls an array of LEDs
 see Chapter 5 for more on using switches
 see Chapter 7 for information on LEDs
 */

int inputPins[] = {2,3,4,5};  // create an array of pins for switch inputs

int ledPins[] = {10,11,12,13};  // create array of output pins for LEDs

void setup()
{
  for(int index = 0; index < 4; index++)
  {
    pinMode(ledPins[index], OUTPUT);         // declare LED as output
    pinMode(inputPins[index], INPUT);        // declare pushbutton as input
    digitalWrite(inputPins[index],HIGH);     // enable pull-up resistors
                                             //(see Recipe 5.2)
  }
}

void loop(){
  for(int index = 0; index < 4; index++)
  {
    int val = digitalRead(inputPins[index]);  // read input value
    if (val == LOW)                       // check if the switch is pressed
    {
      digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
    }
    else
    {
      digitalWrite(ledPins[index], LOW);      // turn LED off
    }
  }
}

I understand the working of this code, but how can i put a name at Ledpins 10,11,12,13 ??

I will show this at the LCD display ,during pushing a switch

Regards,

Patrick

I was wondering if it is possible to label a array of switches ?

I don't understand the question. You don't have an array of switches. You have an array of pin numbers that switches are connected to. You have an array of pin numbers that LEDs are connected to.

You could easily create two more arrays, containing pointers to strings that have some meaning:

char *switchPinNames[] = {"Red switch", "Green switch", "Blue switch", "Black switch"};
char *ledPinNames[] = {"Red", "Green", "Pink", "Purple"};

Then, use the same index to find a value in all 4 arrays.

byte i = 3;
if(digitalRead(switchPins[i]) == HIGH)
{
   digitalWrite(ledPins[i], HIGH);
   Serial.print(switchPinNames[i]);
   Serial.print(" is pressed, so the ");
   Serial.print(ledPinNames[i]);
   Serial.println(" LED was turned on");
}

Thanks for reply PaulS,

Wat you have send is just what i mean :wink:
I have try this sketch but it's not working yet ;-((

What do i wrong:

char *switchPinNames[] = {"Red switch", "Green switch", "Yellow switch"};
char *ledPinNames[] = {"Red", "Green", "Yellow"};
int inputPins[] = {2,3,4,5};  // create an array of pins for switch inputs
int ledPins[] = {10,11,12,13};  // create array of output pins for LEDs

void setup()

{
    Serial.begin(9600);
    for(int index = 0; index < 4; index++)
  {
    pinMode(ledPins[index], OUTPUT);         // declare LED as output
    pinMode(inputPins[index], INPUT);        // declare pushbutton as input
    digitalWrite(inputPins[index],HIGH);     // enable pull-up resistors
                                             //(see Recipe 5.2)
  }
}

void loop(){
  for(int index = 0; index < 4; index++)
  {
    int val = digitalRead(inputPins[index]);  // read input value
    if (val == LOW)                       // check if the switch is pressed
    {
      digitalWrite(ledPins[index], HIGH); // turn LED on if switch is pressed
    }
    else
    {
      digitalWrite(ledPins[index], LOW);      // turn LED off
    }
  }
  
  byte index  = 4;
   if(digitalRead(inputPins[index]) == HIGH)
{
   digitalWrite(ledPins[index], HIGH);
   Serial.print(switchPinNames[index]);
   Serial.print(" is pressed, so the ");
   Serial.print(ledPinNames[index]);
   Serial.println(" LED was turned on");
}
  
  
  
}

When i push the first button ( switch red ) the serial Monitor shows:
green is pressed so , the led $#$%%^ was turned on
green is pressed so , the led $#$%%^ was turned on
green is pressed so , the led $#$%%^ was turned on

Find attached movie:

the other buttons will light up the LED , but it will not show at the serial monitor ???

What i 'am doing wrong with this sketch ??

  byte index  = 4;
   if(digitalRead(inputPins[index]) == HIGH)
{
   digitalWrite(ledPins[index], HIGH);
   Serial.print(switchPinNames[index]);
   Serial.print(" is pressed, so the ");
   Serial.print(ledPinNames[index]);
   Serial.println(" LED was turned on");
}

You are already reading the pin state in the for loop. Why do it again?

You have arrays of size 4, with indices ranging from 0 to 3. You have tried to access the 5th element of several 4 element arrays.

I'm thinking your sketch needs to look more like this. I moved the bit that outputs into the loop. You had it outside with index set to a value that was out of range for your array.

char *switchPinNames[] = {
"Red switch", "Green switch", "Yellow switch"};
char *ledPinNames[] = {
"Red", "Green", "Yellow"};
int inputPins[] = {
2,3,4,5}; // create an array of pins for switch inputs
int ledPins[] = {
10,11,12,13}; // create array of output pins for LEDs

void setup()

{
Serial.begin(9600);
for(int index = 0; index < 4; index++)
{
pinMode(ledPins[index], OUTPUT); // declare LED as output
pinMode(inputPins[index], INPUT); // declare pushbutton as input
digitalWrite(inputPins[index],HIGH); // enable pull-up resistors
//(see Recipe 5.2)
}
}

void loop(){
for(int index = 0; index < 4; index++)
{

if(digitalRead(inputPins[index]) == HIGH)
{
digitalWrite(ledPins[index], HIGH);
Serial.print(switchPinNames[index]);
Serial.print(" is pressed, so the ");
Serial.print(ledPinNames[index]);
Serial.println(" LED was turned on");
}

else
{
digitalWrite(ledPins[index], LOW); // turn LED off
}
}
}

The value of index is what points to the desired element in your array.

Dear Jimmy60,

Many thanks for your reply , this is the way i want

It's clear now what i did wrong ,

all works fine now , i have only exchange the follow command:

if(digitalRead(inputPins[index]) == LOW)

in stead of HIGH i put this in LOW ,
In HIGH the LED was ligthing continue , when i exchange this in LOW , I push the button and the LED will on.

Also many thanks to PaulS :slight_smile:

Best Regards,

Patrick , Maarssen The Netherlands

Dear All,

all works well , but now i have adjust this sketch to an I2C LCD,
All works fine but after pushing the last button action , the text shows at the LCD and will not clear.

So when i push the Yellow button , the LCD display continue show Yellow pressed.

I have try it with lcd.clear at the end of this sketch , but then the LCD will continue empty , also during pushing the buttons ????

char *switchPinNames[] = {
  "Red switch", "Green switch", "Yellow switch"};
char *ledPinNames[] = {
  "Red ", "Green", "Yellow"};
int inputPins[] = {
  2,3,4,5};  // create an array of pins for switch inputs
int ledPins[] = {
  10,11,12,13};  // create array of output pins for LEDs

// I2C LCD 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display


void setup()
{
 lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
 
  
  for(int index = 0; index < 4; index++)
  {
    pinMode(ledPins[index], OUTPUT);         // declare LED as output
    pinMode(inputPins[index], INPUT);        // declare pushbutton as input
    digitalWrite(inputPins[index],HIGH);     // enable pull-up resistors
    //(see Recipe 5.2)
  }
}

void loop(){
  for(int index = 0; index < 4; index++)
  {

    if(digitalRead(inputPins[index]) == LOW)
    {
      digitalWrite(ledPins[index], HIGH);
      lcd.setCursor(0, 0);
      lcd.print(ledPinNames[index]);
      lcd.print(" Pressed,   ");
    }    
    else
    {
      digitalWrite(ledPins[index], LOW);      // turn LED off 
      lcd.clear();  //this will not work !!!! LCD keeps empty !!!
     
    }
  }
}

lcd.clear(); //this will not work !!!! LCD keeps empty !!!

Regards,

Patrick , ArduinoPat , The Netherlands.

All works well now !!

all i had to do is the follow line:

lcd.print(" ");

that was all :drooling_face:

Find attached the correct working sketch:

char *switchPinNames[] = {
  "Red switch", "Green switch", "Yellow switch"};
char *ledPinNames[] = {
  "Red ", "Green", "Yellow"};
int inputPins[] = {
  2,3,4,5};  // create an array of pins for switch inputs
int ledPins[] = {
  10,11,12,13};  // create array of output pins for LEDs

// I2C LCD 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display


void setup()
{
   lcd.init();
   lcd.backlight();
   
   
  for(int index = 0; index < 4; index++)
  

  
  {
    pinMode(ledPins[index], OUTPUT);         // declare LED as output
    pinMode(inputPins[index], INPUT);        // declare pushbutton as input
    digitalWrite(inputPins[index],HIGH);     // enable pull-up resistors
    //(see Recipe 5.2)
  }
}

void loop(){
   
    for(int index = 0; index < 4; index++)
  {   

    if(digitalRead(inputPins[index]) == LOW)
    {

      
      digitalWrite(ledPins[index], HIGH);
      lcd.setCursor(0, 0);
      lcd.print(ledPinNames[index]);
      lcd.print(" Pressed,   ");
    }    
    else
    {
      digitalWrite(ledPins[index], LOW);      // turn LED off 
      lcd.print("                ");
    }
  }
}

Best Regards , ArduinoPat , The Netherlands