lab2-3-2.c 1.45 KB
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 }; 
const int Keypad[5] = { 6, 12, 13, 16, 19 }; 
int state=0;

void checkKey3(){
	if(KeypadRead()==2)
		state=0;
}
int KeypadRead() { 
	int i, keypadnum=-1; 
	for(i=0; i<8; i++) { 
		if(!digitalRead(Keypad[i])) { 
			keypadnum = i; 
			break; 
		} 
	} 
	return keypadnum; 
} 
void LedControl(int num) { 
	int i; 
	for(i=0; i<8; i++) { 
		checkKey3();
		if(state==0)
			return;
		if(i==num) 
			digitalWrite(LedRed[i], HIGH); 
		else 
			digitalWrite(LedRed[i], LOW); 
	} 
} 
void LedON(int order){
	int i;
	if(order==1){
		for(i=0; i<8; i++) { 
			LedControl(i);
			if(state==0)
				return; 
			delay(500); 
		} 
		LedControl(-1); 
	}
	else if (order==-1){
		for(i=7; i>=0; i--) { 
			LedControl(i);
			if(state==0)
				return; 
			delay(500); 
		} 
		LedControl(-1);  
	}	
}
int main(void) { 
	int i, keypadnum=-1; 
	if(wiringPiSetupGpio() == -1) 
		return 1; 
	for(i=0; i<8; i++) { 
		pinMode(LedRed[i], OUTPUT); 
		digitalWrite(LedRed[i], LOW); 
	} 
	for(i=0; i<5; i++) 
		pinMode(Keypad[i], INPUT); 
	while(1) { 
		state=0;
		keypadnum = KeypadRead(); 
		if(keypadnum== 3) { 
			state=1;
			LedON(1);
		} 
		else if(keypadnum== 4) {
			state=1; 
			LedON(-1); 		
		} 
		else if(keypadnum== 0){
			while(1){
				state=1;
				LedON(1);
				if(state==0)
					break;
			}
		}
		else if(keypadnum== 1){
			while(1){
				state=1;
				LedON(-1);
				if(state==0)
					break;
			}
		}				
	} 
	return 0; 
}