Question on buttons and leds

My project is to make a prop TL-50 form Star Wars. I am using an Arduino Nano and am very new to the coding side of things. Basically I am trying to figure out if when pushing a button/trigger I can toggle between two leds, each individually or none at all. So I want the two buttons to work only when the trigger button is pulled, but so far they work all the time even if the trigger is not pulled. Here is the code so far. Disclaimer I am very new.

int LED1 = 2;   //main fire 
int LED2 = 3;  // secondary fire
int LEDP = 4; //to test the if the pushbutton works 
int BUTTON1 = 10;     // button for main fire 
int BUTTON2 = 11;  // button for secondary fire 
int BUTTONT = 12;  //trigger button


void setup() {
  // put your setup code here, to run once
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT); 
pinMode(LEDP, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(BUTTONT, INPUT);
}



void loop() {
  // put your main code here, to run repeatedly:
if(digitalRead(BUTTONT) == LOW){
digitalWrite (BUTTON1, HIGH);
digitalWrite (BUTTON2, HIGH);
digitalWrite (LEDP, HIGH);
}
else {digitalWrite (BUTTON1, LOW);
digitalWrite (BUTTON2, LOW);
digitalWrite (LEDP, LOW); 
}

 if(digitalRead(BUTTON1) == LOW){
  digitalWrite (LED1 , HIGH);
}
else {digitalWrite(LED1, LOW);
}

 if (digitalRead(BUTTON2) == LOW){
   digitalWrite (LED2 , HIGH);
  
 }
else {digitalWrite(LED2, LOW);
}


}

Just formatted it to make it easier to follow:

int LED1 = 2;   //main fire
int LED2 = 3;  // secondary fire
int LEDP = 4; //to test the if the pushbutton works
int BUTTON1 = 10;     // button for main fire
int BUTTON2 = 11;  // button for secondary fire
int BUTTONT = 12;  //trigger button


void setup() {
  // put your setup code here, to run once
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LEDP, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTONT, INPUT);
}



void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(BUTTONT) == LOW) {
    digitalWrite (BUTTON1, HIGH);
    digitalWrite (BUTTON2, HIGH);
    digitalWrite (LEDP, HIGH);
  }
  else {
    digitalWrite (BUTTON1, LOW);
    digitalWrite (BUTTON2, LOW);
    digitalWrite (LEDP, LOW);
  }

  if (digitalRead(BUTTON1) == LOW) {
    digitalWrite (LED1 , HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
  }

  if (digitalRead(BUTTON2) == LOW) {
    digitalWrite (LED2 , HIGH);
  }
  else {
    digitalWrite(LED2, LOW);
  }
}

Please post a wiring diagram.

1 Like
if (digitalRead(BUTTONT) == LOW) {
    digitalWrite (BUTTON1, HIGH);
    digitalWrite (BUTTON2, HIGH);
    digitalWrite (LEDP, HIGH);
  }
  else {
    digitalWrite (BUTTON1, LOW);
    digitalWrite (BUTTON2, LOW);
    digitalWrite (LEDP, LOW);
  }

Manipulating BUTTON1 and BUTTON2, considering they're inputs, isn't going to get you there.  The wiring convention for buttons includes using INPUT_PULLUP if wired as S2 S3 is below.  How are your switches wired?

Stuff like that is done with logical operators.

1 Like

I will look into Logical operators, thank you! and would it be possible for when I push a button for the buttons to go into a pattern of either blinking or blinking then on then off?
not asking how to do it just if its possible.


this is the current design.

What is the Vf of the LEDs? 100 ohms seems much too low to stay under 20mA output pin current limitation.

100 ohms is an insanely low value for a pull down resistor. Actually, you don't need resistors for those inputs, use INPUT_PULLUP to use the internal pullups, connect to ground instead of 5V, and reverse the logic sense of the readings. See S3 in the diagram of reply #3.

Yes, it is possible. It's relatively easy.

Post #3 fixed. (S2 vs. S3)

I think I'm going to change the design a bit to be a simpler. Now instead of 3 buttons I will use one and work it out for a press to do one function and hold to do the other. and what do you mean by reverse logic the sense of the readings? I'm assuming that's with the code? Thank you!

You can do this without any code. Wire the two pushbuttons common connection, then wire that in series with the trigger.

image

I changed my design a bit to now I get two functions when I either press the button or hold. The problem doing it without code is that I want the two outputs to have 2 functions running at the same time a light and a sound.

Then please post an updated schematic, in a new post, not editing the original message #6.

he problem doing it without code is that I want the two outputs to have 2 functions running at the same time a light and a sound.

Why is that a problem? Or, can you not do it with using code?

1 Like

I am new so I dont know the full potential to coding but Im assuming that I need the buttons connected with the arduino to be able to use 2 functions. But im not sure, Plus I want to get more comfortable with coding so integrating with my new design would help me do that. single press is the secondary fire and hold is the primary fire.

That sounds backwards right away.

a7

basically i want for an led to flash when i hold. and if I press a separate led stays on then flash then turn off.

I got it mostly figured out but I have one problem. Whenever I hold the button the led for short press turns on when I let go of the hold.
circuit (2)
here is the circuit diagram and code

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
 */

#include <ezButton.h>

const int SHORT_PRESS_TIME = 200; // 1000 milliseconds
const int LONG_PRESS_TIME  = 200; // 1000 milliseconds

const int LEDP = 9;
const int LEDS = 8;

ezButton button(10);  // create ezButton object that attach to pin 10;

int lastState = LOW;
int currentState;

unsigned long pressedTime  = 0;
unsigned long releasedTime = 0;

bool isPressing = false;
bool isLongDetected = false;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(50); // set debounce time to 50 milliseconds
pinMode(LEDP, OUTPUT);
pinMode(LEDS, OUTPUT);
digitalWrite(LEDP, LOW);
digitalWrite(LEDS, LOW);
  
}


void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed()){
    pressedTime = millis();
    isPressing = true;
    isLongDetected = false;
    
  }

  if(button.isReleased()) {
    isPressing = false;
    releasedTime = millis();
   digitalWrite(LEDP, LOW);
    

    long pressDuration = releasedTime - pressedTime;

    if( pressDuration < SHORT_PRESS_TIME )
      Serial.println("A short press is detected");
      digitalWrite(LEDS, HIGH);
      delay(1000);
      digitalWrite(LEDS, LOW);
  }



  if(isPressing == true && isLongDetected == false) {
    long pressDuration = millis() - pressedTime;

    if( pressDuration > LONG_PRESS_TIME ) {
      Serial.println("A long press is detected");
      digitalWrite(LEDP, HIGH);
    
      isLongDetected = true;
    }
 
    
   }
  

}

How can these be the same value?

later on in the code it makes the short press time less than 200 and long press greater, the press has to be less than 200 for it to be considered a short press.

I'm also not sure how to make it blink without delay since millis is used already for the buttons.

millis() isn't tied to any particular task. Each task just keeps its own time stamps and so runs independent timers.