Forum Rule: Always post complete source code & details to reproduce any issue!
Page 2 of 2 FirstFirst 1 2
Results 26 to 27 of 27

Thread: indicator lights for buttons

  1. #26
    Senior Member
    Join Date
    Feb 2018
    Posts
    154
    There are only 3 momentary switches connected to pins 0,1,2 and gnd. The led is connected to pin 13 and gnd. The led on both the board and pin 13 work, the brightness is not an issue, I'm trying to get it to work first. The code you posted in #17 won't compile because of the error 'usbMidi' was not declared in this scope.
    This code works for one button

    Code:
    /*
    * Programmed by: Md. Mehrab Haque
    
    * Dhaka, Bangladesh.
    
    * Phone: +8801795415378 */
    
    int stateNow = 0;
    
    int stateBefore = 0;
    
    int i = 0;
    
    int j = 0;
    
    void setup() {
    
    pinMode (0, INPUT);
    
    pinMode (13, OUTPUT);
    
    }
    
    void loop() {
    
    stateNow = digitalRead(0);
    
    if (stateNow != stateBefore) {
    
    if (stateNow==HIGH and i==0) {
    
    digitalWrite (13, HIGH);
    
    j=1;
    
    }
    
    else if (stateNow==LOW and j==1) {
    
    i=1;
    
    }
    
    else if (stateNow==HIGH and i==1) {
    
    digitalWrite (13, LOW);
    
    j=0;
    
    }
    
    else if (stateNow==LOW and j==0) {
    
    i=0;
    
    }
    
    }
    
    stateBefore=stateNow;
    
    }

  2. #27
    Senior Member
    Join Date
    Feb 2018
    Posts
    154
    This code works for 1 button, Can anyone tell me how to expand it for 16 buttons with 16 lights?

    Code:
    /*********************
    Simple toggle switch
    Created by: P.Agiakatsikas
    *********************/
    
    int button = 0;
    int led = 13;
    int status = false;
    
    void setup(){
    pinMode(led, OUTPUT);
    pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
    }
    
    void loop(){
    //a) if the button is not pressed the false status is reversed by !status and the LED turns on
    //b) if the button is pressed the true status is reveresed by !status and the LED turns off
    
    if (digitalRead(button) == true) {
    status = !status;
    digitalWrite(led, status);
    } while(digitalRead(button) == true);
    delay(50); // keeps a small delay
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •