LED stays on even when digitalRead is low - arduino

I am just creating a simple basic program, but I can't figure out what's going wrong.
I have set three pins as output and three pins as input. When those three pins digitalRead == HIGH they will set an LED to HIGH, but instead my LED is always staying high.
Here is my Arduino code:
int LED_Low = 4; // Red LED
int LED_Avg = 3; // Yellow LED
int LED_High = 2; // Green Led
int WaterLow = 7;
int WaterAvg = 8;
int WaterHigh = 9;
void setup() {
// Put your setup code here, to run once:
pinMode(LED_Low, OUTPUT);
pinMode(LED_Avg, OUTPUT);
pinMode(LED_High, OUTPUT);
pinMode(WaterLow, INPUT);
pinMode(WaterAvg, INPUT);
pinMode(WaterHigh, INPUT);
}
void check(){
if(digitalRead(WaterLow) == HIGH){ // If Water level is low
digitalWrite(ledLow, HIGH); // Turn on red LED indication water level is low
}
else{
digitalWrite(ledLow, LOW);
}
if(digitalRead(WaterAvg) == HIGH){ // If water level is medium
digitalWrite(ledAvg, HIGH); // Turn on yellow LED indicating water level is average
}
else{
digitalWrite(ledAvg, LOW);
}
if(digitalRead(WaterHigh) == HIGH){ //
digitalWrite(ledHigh, HIGH); //
}
else{
digitalWrite(ledHigh, LOW);
}
}
void loop() {
// Put your main code here, to run repeatedly:
check();
}
In the above image I have connected led on pin 2, 3, and 4 with 1.5 kilohm resistor and three wires in pin 7, 8, and 9 which will receive input from the 5 volt pin and turn on the LED. Accordingly, the 5 volt pin is connected to the positive terminal on the power bus and with 9.1 *2 resistors in series and then this wire connect with pin 2, 3, and 4.

I found the issue. My code was OK. It was my circuit.
The pins I declared to receive input were not connected to ground.

You can make that with the help of two cases
Define the delay
if (digitalRead(WaterLow) == HIGH) // If Water level is low
{
digitalWrite(ledLow, HIGH); // Turn red LED indication water level is low
delay(2000);
}
else
{
digitalWrite(ledLow, LOW);
}
Make a condition like this
int stateled = LOW;
int previous = LOW;
long time = 0;
long debounce = 200;
void loop()
{
stateButton = digitalRead(WaterLow);
if (stateButton == HIGH && previous == LOW && millis() - time > debounce)
{
if(stateLED == HIGH)
{
stateLED = LOW;
}
else
{
stateLED = HIGH;
}
time = millis();
}
digitalWrite(ledlow, stateLED);
previous == stateButton;
}

Related

How to prevent an initial HIGH output in PIR Sensor

I am testing out the HC-SR501 PIR sensor on arduino. I tried a simple code tutorial online
int buzz = 13;
int pir = 2;
int value = 0;
int pirState = LOW;
void setup() {
pinMode(buzz, OUTPUT);
pinMode(pir, INPUT);
Serial.begin(9600);
}
void loop() {
delay(5000);
value = digitalRead(pir);
if (value == HIGH) {
digitalWrite(buzz, HIGH);
if (pirState == LOW) {
Serial.println("Motion Detected!");
pirState = HIGH;
}
} else {
digitalWrite(buzz, LOW);
if (pirState == HIGH){
Serial.println("Motion Ended!");
pirState = LOW;
}
}
}
This works, however, I'm trying to initialize it to a LOW output. When I first turn on the board, it initially gives me a high output, and so the buzzer activates instantly, even though I placed it away from myself. The serial prints out Motion Detected. I tried adding a delay, however it still gives out a HIGH output afterwards. Anyone knows how to solve this?
Thank you!
The pinMode sets the pin as an output, but the default state is LOW, so there should be no problem with it.
However, pin 13 is wired to onboard LED. And the onboard LED is also used by bootloader to signal its activity after the reset. You should check other pins but 13.

How to use millis on traffic light controller in Arduino

I'm doing a smart city project with my Arduino and I have a question. I have created 2 functions and one of them is the traffic light controller and I use the delay() to make them have the right delays between them. But I have a problem. I call both of the functions inside loop(), but one of them only runs when the other is finished. Is there any way to run them both? I've seen people using millis().
My code:
int smartled1 = 13;
int smartled2 = 12;
int smartled3 = 11;
int smartled4 = 10;
int smartled5 = 9;
int smartled6 = 8;
int smartled7 = 7;
int smartled8 = 6;
int smartled9 = 50;
int smartled10 = 51;
int smartled11 = 52;
int smartled12 = 53;// Pin para ligar o led
int sensorPin = A0; // Seleção do pin de entrada do LDR
int sensorValor = 0; // Variavel de armazenamento do LDR inicializada a 0
int semaforo1[]= {22, 24, 26};
int semaforo2[]= {5, 4, 3};
int semaforo3[]= {29, 31, 33};
int semaforo4[]= {28, 30, 32};
int Delayvermelho = 5000;
int Delayamarelo = 2000;
void setup() {
Serial.begin(9600); // Define a porta serie para comunicação
pinMode(smartled1, OUTPUT);
pinMode(smartled2, OUTPUT);
pinMode(smartled3, OUTPUT);
pinMode(smartled4, OUTPUT);
pinMode(smartled5, OUTPUT);
pinMode(smartled6, OUTPUT);
pinMode(smartled7, OUTPUT);
pinMode(smartled8, OUTPUT);
pinMode(smartled9, OUTPUT);
pinMode(smartled10, OUTPUT);
pinMode(smartled11, OUTPUT);
pinMode(smartled12, OUTPUT);// Define o pin do Led como saída
for (int i = 0; i < 3; i++) {
pinMode(semaforo1[i], OUTPUT);
pinMode(semaforo2[i], OUTPUT);
pinMode(semaforo3[i], OUTPUT);
pinMode(semaforo4[i], OUTPUT);
}
}
void loop() {
smart_lights();
semaforos_cruzamento();
}
void semaforos_cruzamento(){
// Making Green LED at signal 1 and red LED's at other signal HIGH
digitalWrite(semaforo1[2], HIGH);
digitalWrite(semaforo1[0], LOW);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 2 seconds
digitalWrite(semaforo1[1], HIGH);
digitalWrite(semaforo1[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo1[1], LOW);
// Making Green LED at signal 2 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[2], HIGH);
digitalWrite(semaforo2[0], LOW);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 2 seconds
digitalWrite(semaforo2[1], HIGH);
digitalWrite(semaforo2[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo2[1], LOW);
// Making Green LED at signal 3 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[2], HIGH);
digitalWrite(semaforo3[0], LOW);
digitalWrite(semaforo4[0], HIGH);
delay(Delayvermelho);
// Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 2 seconds
digitalWrite(semaforo3[1], HIGH);
digitalWrite(semaforo3[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo3[1], LOW);
// Making Green LED at signal 4 and red LED's at other signal HIGH
digitalWrite(semaforo1[0], HIGH);
digitalWrite(semaforo2[0], HIGH);
digitalWrite(semaforo3[0], HIGH);
digitalWrite(semaforo4[2], HIGH);
digitalWrite(semaforo4[0], LOW);
delay(Delayvermelho);
// Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 2 seconds
digitalWrite(semaforo4[1], HIGH);
digitalWrite(semaforo4[2], LOW);
delay(Delayamarelo);
digitalWrite(semaforo4[1], LOW);
}
void smart_lights(){
int sensorValor = analogRead(sensorPin);// Lê o valor fornecido pelo LDR
Serial.println(sensorValor);//Imprime os valores provenientes do sensor na ecrã
// Caso o valor lido na porta analógica A5 seja maior do que
// 800, acende o LED
// Ajuste o valor abaixo de acordo com o circuito
if (sensorValor < 400)
{
digitalWrite(smartled1, HIGH);
digitalWrite(smartled2, HIGH);
digitalWrite(smartled3, HIGH);
digitalWrite(smartled4, HIGH);
digitalWrite(smartled5, HIGH);
digitalWrite(smartled6, HIGH);
digitalWrite(smartled7, HIGH);
digitalWrite(smartled8, HIGH);
digitalWrite(smartled9, HIGH);
digitalWrite(smartled10, HIGH);
digitalWrite(smartled11, HIGH);
digitalWrite(smartled12, HIGH);
}
else //Caso contrário, apaga o led
{
digitalWrite(smartled1, LOW);
digitalWrite(smartled2, LOW);
digitalWrite(smartled3, LOW);
digitalWrite(smartled4, LOW);
digitalWrite(smartled5, LOW);
digitalWrite(smartled6, LOW);
digitalWrite(smartled7, LOW);
digitalWrite(smartled8, LOW);
digitalWrite(smartled9, LOW);
digitalWrite(smartled10, LOW);
digitalWrite(smartled11, LOW);
digitalWrite(smartled12, LOW);
}
}
Yes, you can use state machines.
Example:
// totally randon delays. Prime to each other.
static const unsigned char MY_EVENT_TIMEOUT = 100; // in milliseconds.
static const unsigned int HIS_EVENT_TIMEOUT = 2533; // in milliseconds.
// setup two sta=te machines. In this example both state machines will simply
// wait a bit before toggling between two states.
// state machines consist of different state values, a state variable, and some data
enum MyEventState {
my_event_state_initial, // we'll just start timing
my_event_state_1,
my_event_state_2,
/* and so on... */
};
MyEventState my_state = my_state_initial;
unsigned char my_event_timestamp; // largest my_event delay is less than 255 ms
// second state machine.
enum HisEventState {
his_event_state_iinitial, // we'll wait for some external event
his_event_state_1,
his_event_state_2,
/* more states if you need */
};
HisEventState his_state = his_state_initial;
unsigned int his_event_timestamp; // largest his_event delay is less than 65535 ms
void my_event_handler()
{
switch (my_state)
{
case my_event_state_initial:
// initialize our timestamp and go straight to state 1
my_event_timestamp = (unsigned char)millis();
my_state = my_event_state_1;
// passing though to execute next state handler immediately
case my_event_state_1:
// in real application, you'd likely CHECK for a triggering event first
// and check millis() for timeouts, etc. Using different states to
// check for time out... Note the use of subtraction of UNSIGNED
// values to avoid rollover issues altogether
// the extra cast is the correct way to to it. C++ subtraction MAY
// return an unsigned int, according to the standard. In practice, it
// does not happens for 8 and 16-bit MCUs.
// no matter what you do, do not wait, poll your input line, or
// check if there are bytes on the serial buffer, do not block.
if ((unsigned char)((unsigned char)millis() - my_event_timestamp) < MY_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
my_event_timestamp = (unsigned char)millis(); // get a time stamp
my_state = my_event_state_2; // change state
// passing though to execute next state handler immediately
case my_event_state_2:
// it's always the same logic in this simple state machine,
// but you can put any logic you want here to turn one light on or off,
// check inputs, etc..
if ((unsigned char)millis() - my_event_timestamp < MY_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
my_event_timestamp = (unsigned char)millis(); // get a time stamp
my_state = my_event_state_1; // change state
// we're done. the handler for state 1 will execute the next time
// loop() is called.
// This would be the place you could find an infamous goto within a
// switch blck, if timing needs to be suoer duper extra tight.
// It does happen sometimes, but rarely.
return;
}
}
void his_event_handler()
{
// this is the same logic, but with a different beat.
// since these handlers do not block for timers or events
// the handlers appear to run 'concurrently'
switch (his_state)
{
case his_event_state_initial:
// initialize our timestamp and go straight to state 1
his_event_timestamp = (unsigned int)millis();
his_state = his_event_state_1;
// passing though to execute next state handler immediately
case his_event_state_1:
if ((unsigned int)millis() - his_event_timestamp < HIS_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
his_event_timestamp = (unsigned int)millis(); // get a time stamp
his_state = his_event_state_2; // change state
// passing though to execute next state handler immediately
case his_event_state_2:
if ((unsigned int)millis() - his_event_timestamp < HIS_EVENT_TIMEOUT)
{
// not enough time has elapsed, nothing to do, so return
return;
}
his_event_timestamp = (unsigned int)millis(); // get a time stamp
his_state = his_event_state_1; // change state
// we're done. the handler for state 1 will execute the next time
// loop() is called.
return;
}
}
void setup()
{
}
void loop() {
// call our concurrent state machines
my_event_handler();
his_event_handler();
/* some other non-blocking code... */
}

Ultrasonic sensors

Now i am using 2 ultrasonic sensors but i found that they become unstable outdoors and give random readings .....although they act normally inside a room .....how can i solve this problem???
That is the code
it seems like the off Sensor always Reading and i checked the wiring and it is good so i don't really know where is the problem is ??
#define trig 2
#define echo 3
#define led 7
#define relay 4
#define trig2 10
#define echo2 9
#define led2 8
long duration,distance,OnSensor,OffSensor;
void setup() {
// put your setup code here, to run once:
pinMode(echo,INPUT);
pinMode(echo2,INPUT);
Serial.begin(9600);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(relay,OUTPUT);
pinMode(trig,OUTPUT);
pinMode(trig2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led,LOW);
digitalWrite(led2,LOW);
digitalWrite(relay,LOW);
SonarSensor(trig2, echo2);
OffSensor = distance;
delay(50);
SonarSensor(trig, echo);
OnSensor=distance;
if(OnSensor<80&&OffSensor>=80){//sensor 1 activated
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
delay(1000);
digitalWrite(led,HIGH);
digitalWrite(relay,HIGH);
delay(150);
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor>80){//No sensor activated
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
else if(OnSensor>80&&OffSensor<80){//sensor2 activated
digitalWrite(led2,HIGH);
digitalWrite(relay,LOW);
delay(5000);
}
else if(OnSensor<80&&OffSensor<80){//Both sensors activated
digitalWrite(led2,HIGH);
digitalWrite(led,HIGH);
digitalWrite(relay,LOW);
delay(3000);
}
}
void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
I think there are 2 problem here
You use 2 sensor
List item
Use use it outdoor
From my experience the ultra sonic wave can bouncing around object many time. depend on your setup, second sensor might read echo form first sensor.
follow these step
1. Try to use 1 sensor per time to see if it error
2. If single sensor work you should add more delay after sensor reading
3. Plot the data to see how it look like you might need digital filter to improve reading
For step 3 if you use sensor as On Off by threshold you can see my example code.
This code work the same way as button de-bouncing so in need to trig continuously for 10 time to prevent false alarm (This code for single sensor)
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define RELAY_PIN 4
/**/
#define ULTRASONIC_INTERVAL_MS 100//50
#define ULTRASONIC_TRIG_COUNT_THRESH 10
#define ULTRASONIC_DISTACE_THRESH_CM 50
/**/
#define RELAY_TRIG__INTERVAL_MS 5000
#define RELAY_TRIG_LOGIC HIGH
void setup()
{
pinMode (RELAY_PIN, OUTPUT);
pinMode (ULTRASONIC_TRIG_PIN, OUTPUT);
pinMode (ULTRASONIC_ECHO_PIN, INPUT);
Serial.begin(115200);
}
void loop()
{
static unsigned long ultrasonic_timer = millis();
static unsigned long relay_trig_timer = millis();
static float us_distance_cm = 0;
static int us_trig_count = 0;
static byte relay_state = !RELAY_TRIG_LOGIC; // initial value as off;
if (millis() - ultrasonic_timer >= ULTRASONIC_INTERVAL_MS) {
ultrasonic_timer += ULTRASONIC_INTERVAL_MS;
us_distance_cm = getDistanceCM();
if (us_distance_cm <= ULTRASONIC_DISTACE_THRESH_CM) {
if (us_trig_count >= ULTRASONIC_TRIG_COUNT_THRESH) {
digitalWrite(RELAY_PIN, RELAY_TRIG_LOGIC);
relay_state = RELAY_TRIG_LOGIC;
relay_trig_timer = millis();
}
else {
us_trig_count++;
}
}
else {
us_trig_count = 0;
}
Serial.print("distance = ");
Serial.print(us_distance_cm);
Serial.print("cm, relay = ");
Serial.print(relay_state);
Serial.print(", count = ");
Serial.println(us_trig_count);
}
if (relay_state == RELAY_TRIG_LOGIC) {
if (millis() - relay_trig_timer > RELAY_TRIG__INTERVAL_MS) {
relay_state = !RELAY_TRIG_LOGIC;
digitalWrite(RELAY_PIN, !RELAY_TRIG_LOGIC);
}
}
}
float getDistanceCM() {
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
unsigned long duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH, 50000);
float distance = float(duration * 343) / 20000.0;
delay(5);
return distance;
}

Arduino: Pin time inactivity/activity

The purpose of the following Arduino code is to interface with the high and low signals sent from a Raspberry Pi; the full explanation is rather complex so I'll spare you the waste in time. The signals sent from the Pi (pins 10 and 11) turn a stepper motor connected to an A4988 driver clockwise or counterclockwise. The pins that dictate this out of the Arduino are the step and direction pins (9 and 8). What I am trying to accomplish is to enable the sleepPin after 60 seconds of pin 10 and 11 inactivity.
Likewise, in the same fashion, I want to stop accepting input from pin 10 and 11 if they both read the same input signal for more than 3 seconds. I've looked up methods on how to incorporate time into Arduino script but do not know how to incorporate it in this instance.
byte directionPin = 9;
byte stepPin = 8;
byte sleepPin = 12;
byte buttonCWpin = 10;
byte buttonCCWpin = 11;
boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
long previousMillis = 0;
long interval = 1000;
void setup() {
//determines length of stepper movement
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
//moves motors clockwise or counterclockwise
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);
}
void loop() {
readButtons();
actOnButtons();
}
void readButtons() {
buttonCCWpressed = false;
buttonCWpressed = false;
if (digitalRead(buttonCWpin) == LOW) {
buttonCWpressed = true;
}
if (digitalRead(buttonCCWpin) == LOW) {
buttonCCWpressed = true;
}
}
void actOnButtons() {
if (buttonCWpressed == true) {
digitalWrite(directionPin, LOW);
for(int x = 0; x < 1; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(515);
digitalWrite(stepPin,LOW);
delayMicroseconds(515);
}
}
if (buttonCCWpressed == true) {
digitalWrite(directionPin, HIGH);
for(int x = 0; x < 1; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(515);
digitalWrite(stepPin,LOW);
delayMicroseconds(515);
}
}
}
Any help would be greatly appreciated along with any tips or concerns.
Thank You.
If you have the luxury to use pins 2 and 3 rather than pins 10 and 11 (assuming you have an Arduino Uno for instance), it could be useful to work with external interrupts.
As a solution for your first problem, here's a minimal code that should put the sleep pin high after 60 seconds of inactivity on both direction pins 2 & 3:
volatile long int last_activity;
void setup(){
attachInterrupt(2, tstamp, CHANGE);
attachInterrupt(3, tstamp, CHANGE);
pinMode(2, INPUT); // your new CW pin
pinMode(3, INPUT); // your new CCW pin
pinMode(12, OUTPUT);
digitalWrite(12, LOW);
last_activity = millis();
}
void loop(){
if (millis() - last_activity > 60e3) {
digitalWrite(12, HIGH);
// do some other things...
}
}
void tstamp(){
last_activity = millis();
}
Now for your second problem, what exactly do you mean by "stop accepting input from pins 10 and 11"? If you only need to check that their state is the same, adding volatile long int last_common_state; in the preamble and checking for digitalRead(2) == digitalRead(3); in tstamp()'s body to update last_common_state should get you on the right track.

athletic response light trainer by arduino

currently I am trying to develop a light response trainer by arduino , as a beginning I used 3 led and 3 push button ,the led must work randomly and when the ledx is flash the user press push bottonx and so on
of course I must use approximate sensor or something similar to be more reliable
when uploading following code all leds continuous week glow (flash) what the problem? thanks for help.
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb1 = 4;
int pb2 = 5;
int pb3 = 6;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0: //if ledcolor equals 0 then the led1 will turn on
digitalWrite(led1, HIGH);
if (digitalRead(pb1),HIGH)
digitalWrite(led1,LOW);
break;
case 1: //if ledcolor equals 1 then the led2 will turn on
digitalWrite(led2, HIGH);
if (digitalRead(pb2),HIGH)
digitalWrite(led2,LOW);
break;
case 2: //if ledcolor equals 2 then the led3 will turn on
digitalWrite(led3, HIGH);
if (digitalRead(pb3),HIGH)
digitalWrite(led3,LOW);
break;
}
}
There are two timing problems in this program: 1) loop executes too quickly to see the response, 2) the pushbutton is read before the person has time to respond to the led.
I recommend restructuring your loop() to have the following general structure:
Turn on an led, based on random()
delay to give the person time to respond. For example, delay(500); will wait 1/2 second (1000 / 2).
read the pushbutton, based on a second switch(ledselect) code block.
delay to give the person time to get ready for the next loop. For example, delay(1000);
thankx alot I catch it ,i change the circuit so I Connect all output's switchs and connect to pin 4 in arduino (every switch become active only if his led on)
int ledselect = 0;
int led1 = 11;
int led2 = 12;
int led3 = 13;
int pb = 4;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(pb, INPUT);
}
void loop() {
int ledselect = random(3);
switch (ledselect) {
case 0:
digitalWrite(led1, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led1,LOW);
break;
case 1:
digitalWrite(led2, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led2,LOW);
break;
case 2:
digitalWrite(led3, HIGH);
delay(20);
while(digitalRead(pb) != HIGH){delay(20);}
digitalWrite(led3,LOW);
break;
default:
delay(20);
break;
}
}

Resources