因为开了一个Arduino的课,所以自己做了一个利用Led和开关的小游戏,就是打地鼠游戏,只是地鼠用led灯代替了,打就用一个开关代替了,以后有时间将它做出来。这里介绍一下简单的原理和程序设计。
因为Arduino的端口有限,我就只接了5盏灯和5个开关。
以下是代码:
—————————————————————————————————————————————————
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin = 7;
const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 12;
const int ledPin5 = 8;
int buttoni=0;
int buttoni1=0;
int buttoni2=0;
int buttoni3=0;
int buttoni4=0;
int buttoni5=0;
int switchi=0;
int ledi=0;
int uv=0;
int buttonState=0;
int bt=1;
int rdn=random(1, 6);
unsigned long time;
unsigned long time1;
unsigned long dtime1;
unsigned long dtime=1000;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
void loop(){
buttonState = digitalRead(buttonPin);
if(buttonState==HIGH)
{uv=1;
dtime=1000;
buttoni=0;
bt=1;
}
if(uv==1)
{
int i;
for(i=0;i<=2;i++){
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
delay(1000);
if(i==2)
{
uv=2;
} } }
if(uv==2)
{
if(bt==1){
bt=0;
switch (rdn) {
case 1:
digitalWrite(ledPin1, HIGH);
break;
case 2:
digitalWrite(ledPin2, HIGH);
break;
case 3:
digitalWrite(ledPin3, HIGH);
break;
case 4:
digitalWrite(ledPin4, HIGH);
break;
case 5:
digitalWrite(ledPin5, HIGH);
break;
}
time = millis();
}
time1=millis();
dtime1=time1-time;
if(dtime1>=dtime&&bt==0){
uv=3;
}
buttoni1 = digitalRead(buttonPin1);
buttoni2 = digitalRead(buttonPin2);
buttoni3 = digitalRead(buttonPin3);
buttoni4 = digitalRead(buttonPin4);
buttoni5 = digitalRead(buttonPin5);
if(buttoni1==HIGH)
buttoni=1;
if(buttoni2==HIGH)
buttoni=2;
if(buttoni3==HIGH)
buttoni=3;
if(buttoni4==HIGH)
buttoni=4;
if(buttoni5==HIGH)
buttoni=5;
if(buttoni!=0&&buttoni!=rdn)
uv=3;
if(buttoni==rdn)
{
bt=1;
dtime-=20;
switch (buttoni) {
case 1:
digitalWrite(ledPin1, LOW);
break;
case 2:
digitalWrite(ledPin2, LOW);
break;
case 3:
digitalWrite(ledPin3, LOW);
break;
case 4:
digitalWrite(ledPin4, LOW);
break;
case 5:
digitalWrite(ledPin5, LOW);
break;
}
rdn=random(1, 6);
delay(dtime);
time=millis();
buttoni=0;
}
}
if(uv==3)
{
int i;
for(i=0;i<=2;i++){
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin5, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin5, LOW);
delay(500);
if(i==2)
{
uv=0;
} } } }
——————————————————————————————————————————
开始时集体闪灯三次,每次按灭的间隔逐渐减少,开始时是1000毫秒,每按一次减少20毫秒。