6 led controlled by 6 switches

Hi to everyone I have an arduino nano. I want to control 8 leds with 8 momentary switches. The trick here is that I want every time I push switch no1 once the led no 1 must stays on and when I push switch no3 the led no3 turn on and led no1 turn off. So the thing is that i want only one led to be swicthed on controlled by it switch. I am newbie and i dont know coding and didnt find something like this online.

Do you mean something like this? (Click the blue buttons.)

What have you tried? Post your code using [code][/code] tags (use the </> button in the “reply” editor).

Pieter

PieterP:
Do you mean something like this? (Click the blue buttons.)

What have you tried? Post your code using [code][/code] tags (use the </> button in the “reply” editor).

Pieter

Yes Pieter this is what i want. I dont have a code. Can you help please?

We're not going to write the code for you. Try it yourself first, an when you get stuck, you can come back here to ask specific questions.

PieterP: We're not going to write the code for you. Try it yourself first, an when you get stuck, you can come back here to ask specific questions.

Ok thank you Pieter

PieterP: We're not going to write the code for you. Try it yourself first, an when you get stuck, you can come back here to ask specific questions.

Alternatively, I just might give you the pre-written code to do the job precisely.

// Radio Buttons!
const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5; 
const int button2 =  4;
const int led3Pin =  7;
const int button3 =  6;
const int led4Pin =  9;
const int button4 =  8;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(button1, INPUT_PULLUP); 
  pinMode(led2Pin, OUTPUT);      
  pinMode(button2, INPUT_PULLUP);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button3, INPUT_PULLUP);      
  pinMode(led4Pin, OUTPUT);      
  pinMode(button4, INPUT_PULLUP);        
  digitalWrite (led1Pin, LOW);
  digitalWrite (led2Pin, LOW);
  digitalWrite (led3Pin, LOW);
  digitalWrite (led4Pin, LOW);
}

void loop() {
  // Select LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    digitalWrite (led1Pin, HIGH);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  // Select LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, HIGH);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  // Select LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, HIGH);
    digitalWrite (led4Pin, LOW);
    delay(500);
  } 
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    // Select LED if button debounced
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, HIGH);
    delay(500);      
  } 
}

The challenge here is for you to understand how it works, because this example uses four buttons (and LEDs), and you have to add to it to control eight!

There are two ways to do that, one is to laboriously copy and paste and modify pieces which will work just fine.

The other is to substantially alter it to use arrays.

Lots of luck!

Note here that the correct way to wire buttons, is between the input pin and ground. Using INPUT_PULLUP you probably do not need any other pull-up resistors.

Edited:
Your code can be as short as like this (by using library and array)

#include <ezButton.h>

ezButton switch1(2);  // create ezButton object that attach to pin 2;
ezButton switch2(3);  // create ezButton object that attach to pin 3;
ezButton switch3(4);  // create ezButton object that attach to pin 4;
ezButton switch4(5);  // create ezButton object that attach to pin 5;
ezButton switch5(6);  // create ezButton object that attach to pin 6;
ezButton switch6(7);  // create ezButton object that attach to pin 7;

ezButton switchArray[6] = {switch1, switch2, switch3, switch4, switch5, switch6};
int ledPinArray[6] = {8, 9, 10, 11, 12, 13};

void setup() {
  for(int i = 0; i < 6; i++){
    switchArray[i].setDebounceTime(50); // set debounce time to 50 milliseconds
    pinMode(ledPinArray[i], OUTPUT);
  }
}

void loop() {
  for(int i = 0; i < 6; i++)
    switchArray[i].loop(); // MUST call the loop() function first

  for(int i = 0; i < 6; i++) {
    if(switchArray[i].isPressed()) {
      for(int j = 0; j < 6; j++) { // turn off all LEDs
        digitalWrite(ledPinArray[j], LOW);
      }

      digitalWrite(ledPinArray[i], HIGH); // turn on only the pressed LED
    }
  }
}

Library is available here

noiasca: using a library without providing a link to it? create/duplicate objects? no const pin definitions? using magical numbers? no range based loops?

but most of all: have you tested your code with hardware? I guess your "code" even doesn't compile, so it will not be of great help for the TO on his way to learn programming.

Dear noiasca, Thank you for your comment,

  1. "using a library without providing a link to it?" => I edited and added the link.
  2. "create/duplicate objects?" => NO create/duplicate objects in this code. Each object represents for one button.
  3. "no const pin definitions?" => I think it is unnecessary in this case.
  4. "using magical numbers?" => I am sorry, I do NOT understand what you meant.
  5. "no range based loops?"=> I am not clear what you meant. As you can see in the code, for loop is from 0 to 6.
  6. "but most of all: have you tested your code with hardware?" => I am sorry. The code has a small bug (short of a comma). I edited. However. I have done a similar project with this buttons library. It worked well.

Thank you again for pointing some errors.

IoT_hobbyist: 2. "create/duplicate objects?" => NO create/duplicate objects in this code. Each object represents for one button.

Nope, each pin has two button objects associated with it. Once as "switch#" and once in the array. The "switch#" variables are redundant, remove them and initialize the array directly.

IoT_hobbyist: 3. "no const pin definitions?" => I think it is unnecessary in this case.

Not using const for things that are constant is bad practice, it makes the code harder to reason about and results in bugs.

IoT_hobbyist: 4. "using magical numbers?" => I am sorry, I do NOT understand what you meant.

The upper bounds of your for loops are all hardcoded, your code will break if a different number of buttons is used, and changing them requires editing all of the for-loops. Search-and-replace bugs or missed occurrences guaranteed.

IoT_hobbyist: 5. "no range based loops?"=> I am not clear what you meant. As you can see in the code, for loop is from 0 to 6.

Using hard-coded loop bounds often results in out of bounds errors. By using range-based for loops, this is impossible.

Using the ezbutton library is unnecessary here, there's no need for debouncing. Even if you did need debouncing, the ezButton library doesn't look like an attractive choice. It uses macros where an enum would be more appropriate, manipulates pin registers in the constructor (before the platform is initialized), and lacks clear documentation.