help plss , i code in Raspberry pi3 using C language how can i use one button to
ID: 3349230 • Letter: H
Question
help plss , i code in Raspberry pi3 using C language
how can i use one button to controll 3 LEDS
if i press button one time , led #1 will on
if i press button two time , led# 2 will on
and if i press buton 3 time. led# 3 will on
how can i do it
include <wiringPi.h>
#include <stdio.h>
void mode1();
void mode2();
void mode3();
#define Led1 3
#define Led2 5
#define Led3 7
#define Button 9
int main(){
wiringPiSetupGpio();
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Button, INPUT);
pullUpDnControl(Button, PUD_UP);
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}
while(1){
int button_press = digitalRead(Button);
if (button_press == 1){
mode3();
}
if(button_press == 2){
printf("press 2");
mode2();
}
if (button_press == 3){
printf("press 3");
mode3(); }
}
return 0;
}
void mode1(){
digitalWrite(Led1, LOW);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, HIGH);
}
void mode2(){
digitalWrite(Led1, HIGH);
digitalWrite(Led2, LOW);
digitalWrite(Led3, HIGH);
}
void mode3(){
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, LOW);
}
||||| 25 35.Explanation / Answer
You are storing the Button status in button_press
on execution its value is only '1' on every press , so we need an other integer variable which count number of pressess.
lets take new variable name : press_count
initially press count must be zero.
if button press equal to 1 the increment the press_count
based on the press_count we enter into switch case block
where switch argument is press_count and three cases 1,2,3
when press_count become 3 then in case 3 make press_count to 0 because it is the final state.
then from next button_press system will stats executing case 1.
Hece the program is:
include <wiringPi.h>
#include <stdio.h>
void mode1();
void mode2();
void mode3();
#define Led1 3
#define Led2 5
#define Led3 7
#define Button 9
int main(){
wiringPiSetupGpio();
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Button, INPUT);
pullUpDnControl(Button, PUD_UP);
int Press_Count = 0;
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}
while(1){
int button_press = digitalRead(Button);
if (button_press == 1)
{
Press_Count = Press_Count+1;
while(button_press == 1); // wait here utill button is released
}
switch(Press_Count){
case 1:
printf("press_1");
mode1();
break;
case 2:
printf("press_2");
mode2();
break;
case 3:
printf("press_3");
mode3();
Press_Count = 0;
break;
}
if(button_press == 2){
printf("press 2");
mode2();
}
if (button_press == 3){
printf("press 3");
mode3(); }
}
return 0;
}
void mode1(){
digitalWrite(Led1, LOW);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, HIGH);
}
void mode2(){
digitalWrite(Led1, HIGH);
digitalWrite(Led2, LOW);
digitalWrite(Led3, HIGH);
}
void mode3(){
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, LOW);
}
Note: please tell me if the code is not working explain the exact problem if my code not giving correct result thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.