Question

In: Computer Science

in arduino, having two LED lights, I need to find a way to turn one LED...

in arduino, having two LED lights, I need to find a way to turn one LED with the push of a button, then with a next push of the button turn off the first LED and turn the second LED on. Help with the circuit and code would be appreciated.

Solutions

Expert Solution

Answer : please up vote or comment for any query i will update the same .Thanks

Circuit Description

1. push button connected to PIN 2

2. LED1 connected to PIN 13

3. LED2 connected to PIN 12

Program plan : -

1. push button set INPUTand PULL UP on to detect LOW state when button pressed

2. set OUTPUT LED1 and LED2

3. read button state as LOW if first time turn ON LED1 and turn OFF LED2

4. button pressed again turn OFF LED1 and turn ON LED2

Program :

int pushButton = 2; //pushbutton pin of arduin UNO
int LED1=13; //pin of LED1
int LED2=12; //pin of LED2
int count=0; //to check for button press count


void setup() {
  

pinMode(pushButton, INPUT); //set push button pin as input
digitalWrite(pushButton,HIGH); //turn on pull up resistor
pinMode(LED1,OUTPUT); //set LED1 pin as output
pinMode(LED2,OUTPUT); //set LED2 pin as output
  
}


void loop() {

int buttonState = digitalRead(pushButton); //read push button state
if(buttonState==LOW) //if button state is LOW button pressed
{
if(count==0) //if first time button pressed turn on LED1 and turn off LED2
{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
count++; //increment count by 1
}
else if(count==1) //if button pressed second time
{
digitalWrite(LED1,LOW); //turn off LED1 and turn on LED2
digitalWrite(LED2,HIGH);   
count=0;
}
delay(200); //delay for debounce to cancel noise
}

  
}

Circuit Diagram :


Related Solutions

I am working on an Arduino Project. I would like to build a controllable RGB LED...
I am working on an Arduino Project. I would like to build a controllable RGB LED Nightlight (with a debounce code included). With each time pushing the button I want a different color. like the following Initial state = LED off first press= LED turns on Red second Press = LED turns on Green third Press = LED turns on Blue Fourth Press = LED turns on Purple Fifth Press = LED turns on Teal sixth press= LED turns on...
Using Arduino to blink an LED in morse code. Morse code is used as one of...
Using Arduino to blink an LED in morse code. Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
in this code I have used array two times . I need a way to make...
in this code I have used array two times . I need a way to make the program functional using a string to store the results and print it again later . this game should be array free. import java.util.Scanner; import java.util.Random;//starter code provided public class inputLap { public static char roller; public static String playerName; public static int printed=0; public static int rounds=8,lives=0,randy; public static int tries[]=new int[4];//use arrays to store number of tries in each life public static...
From the information below I need to find the best predicted probability of having a new...
From the information below I need to find the best predicted probability of having a new client give the highest level of satisfaction. The first item you want to highlight is your customer satisfaction record. Over the past 18 months of employment you have worked with 24 clients, and according to the customer satisfaction surveys all clients are asked to take after their contract is completed, 22 of them rated you with the highest level of satisfaction. Use this information...
I need an Arduino uno code to measure high low levels with hcsr04 of a tank...
I need an Arduino uno code to measure high low levels with hcsr04 of a tank and display it on i2c 20x4 lcd.
I need to turn sections 3.1 and 3.2 into a methods and material section that is...
I need to turn sections 3.1 and 3.2 into a methods and material section that is around 100-200 words. My experiment is how effective the granularity of coffee is with removing copper ions from water. Thanks. 3.1 Calibration Curve 1. Collect six 25 mL Sample vials and label a vial for each of the concentrations (blank, 0.5, 1, 2, 5, 10 ppm) 2. Dispense 10 mL of each Cu2+ solution into five separate labelled 25mL sample vials 3. Add 10...
5. A driver encounters two traffic lights on the way to work each morning. Each light...
5. A driver encounters two traffic lights on the way to work each morning. Each light is either red, yellow, or green. The probabilities of the various combinations of colors is given in the following table: Second Light First Light R Y G R 0.31 0.02 0.18 Y 0.02 0.03 0.03 G 0.14 0.04 0.23 a) What is the probability that the first light is red? b) What is the probability that the second light is green? c) Find the...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must...
I need simulation on protues of speed control of unipolar stepper motor using uno arduino. Must using protues..
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT