利用GPIO编程实现LED亮灭控制

利用GPIO编程实现LED亮灭控制

#include
#include 
#include 
#define Gpin 21
#define Rpin 22
#define BtnPin 24
void LED(char* color)
{
	pinMode(Gpin,OUTPUT);
	pinMode(Rpin,OUTPUT);
	if(color == "RED")
	{
		digitalWrite(Rpin,HIGH);
		digitalWrite(Gpin,LOW);
	}
	else if(color =="GREEN")
	{
		digitalWrite(Rpin,LOW);
		digitalWrite(Gpin,HIGH);
	}
	else
		printf("LED Error");
	}
int main (void)
 {
 	if(wiringPiSetup()==-1)
 	{
 		printf("setup wiringPi failed!");
 		return 1;
 	}
 	pinMode(BtnPin,INPUT);
 	LED("GREEN");
 	for (;;)
 	 {
 	 	if(0==digitalRead(BtnPin))
 	 	{
 	 		delay(10);
 	 		if(0==digitalRead(BtnPin))
 	 		{
 	 			LED("RED");
 	 		}
 	 	}
 	 	else if(1==digitalRead(BtnPin))
 	 	{
 	 		delay(10);
 	 		if(1==digitalRead(BtnPin))
 	 		{
 	 			while(!digitalRead(BtnPin));
 	 			LED("GREEN");
 	 			printf("Button is pressed\n");
 	 		}
 	 	}
 	 }
 	 return 0;
 }


你可能感兴趣的:(基于树莓派的智能小车)