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);
}
}
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);
}
}
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?
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.
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.
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!
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.
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.
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.