專案名稱 : 我要打星星
程式碼 :
arduino :
const int buttonPin1 = 2;
const int buttonPin2 = 4;
const int buttonPin3 = 6;
const int buttonPin4 = 8;
const int buttonPin5 = 10;
const int buttonPin = 6;
const int ledPin1 = 3;
const int ledPin2 = 5;
const int ledPin3 = 7;
const int ledPin4 = 9;
const int ledPin5 = 11;
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;
}
}
}
}
processing :
按中間的開始鍵會有加10分
import processing.serial.*;
Serial myPort;
int score = 0;
float count=1.5;
float count2=0;
void setup(){
size(600,400);
//background(255, 204, 0);
myPort = new Serial(this, "COM3", 9600);
float yrope = 100;
}
int value=0;
void draw(){
background(255, 204, 0);
fill(0);
textSize(150);
text(score, 300, 350);
while(myPort.available()>0)
{
value = myPort.read();
println(value);
if(value=='G'){
score+=10;//分數跳一次加10分
count2+=1;
}
}
}
圖片 :
心得:
在製作的過程中電線有時候一直短路很困擾,再連接processing 時一直出現狀況
燈有時候會一直閃不停QQ
原本processing是用來加分數,可是只做到傳送開始鈕的值~
我們利用遊戲時間即次數來控制遊戲結束與否。
沒有留言:
張貼留言