1.結合可做出什麼:
感覺電腦的遊戲改善之後都可以利用Arduino來玩
原本只是鍵盤滑鼠操作,現在可以變成按鈕,機器人等等
會更栩栩如生,更有趣
2.一個相關Arduino的影片:
他結合了魔術方塊,很有趣的遊戲跟結合
令人佩服~
銘傳大學 Processing & Arduino 課程部落格
/*Arduino*/ int pushButton = 2; int pushButton2 = 6; int pushButton3 = 7; void setup() { Serial.begin(9600); pinMode(pushButton, INPUT_PULLUP); pinMode(pushButton2, INPUT_PULLUP); pinMode(pushButton3, INPUT_PULLUP); } int a=0; int a2=0; int a3=0; // the loop routine runs over and over again forever: void loop() { // read the input pin: int buttonState = digitalRead(pushButton); int buttonState2 = digitalRead(pushButton2); int buttonState3 = digitalRead(pushButton3); // print out the state of the button: if (buttonState == HIGH && a==0 ) { // If switch is ON, Serial.print(1); a=1; // send 1 to Processing } if (buttonState == LOW && a==1) { // If the switch is not ON, Serial.println(0); a=0; // send 0 to Processing } if (buttonState2 == HIGH && a2==0 ) { // If switch is ON, Serial.print(2); a2=2; // send 1 to Processing } if (buttonState2 == LOW && a2==1) { // If the switch is not ON, Serial.println(3); a2=3; // send 0 to Processing } if (buttonState3 == HIGH && a3==0 ) { // If switch is ON, Serial.print(4); a3=4; // send 1 to Processing } if (buttonState3 == LOW && a3==1) { // If the switch is not ON, Serial.println(5); a2=5; // send 0 to Processing } // Serial.println(buttonState); delay(10); // delay in between reads for stability }
/*Processing*/ import processing.serial.*; Serial myPort; int val; PImage bg,helicopter,bomb; PImage [] life = new PImage[5]; float hx=350,hy=400; PImage [] missile = new PImage[5]; int[] mx = new int [5]; int[] my = new int [5]; int bx=0,by=0; void setup(){ size(840,525); String portName = Serial.list()[0]; myPort = new Serial(this, "COM5", 9600); bg =loadImage("sky.jpg"); helicopter= loadImage ("Helicopter.png"); for(int i=0;i<5;i++){ life[i]= loadImage("Life.png"); } for(int i=0;i<5;i++){ missile[i]= loadImage("Missile.png"); } for(int i=0;i<5;i++){ mx[i]=i*200; my[i]=(-300+i*40); } bomb= loadImage ("bomb.png"); } void draw(){ background(bg); life(); helicopterA(); missilefly(); myserial(); bomb(); } void helicopterA(){ image(helicopter,hx,hy); } void life(){ fill(180,180,180); rect(740,340,60,160); fill(255,0,0); rect(740,300,60,40); fill(255,255,255); textFont(createFont("Georgia", 25)); text("LIFE ",742,330); for(int i=0;i<5;i++){ image(life[i],690,320+(30*i),150,100); } } void missilefly(){ for(int i=0;i<5;i++){ image(missile[i],mx[i],my[i],50,100); } for(int i=0;i<5;i++){ my[i]+=2; } for(int i=0;i<5;i++){ if(my[i]==600){ my[i]=(-300+i*40); } } } void myserial(){ if ( myPort.available() &rt; 0) { // If data is available, val = myPort.read(); // read it and store it in val } if (val == 0) { // If the serial value is 0, translate(hx,hx+=10); } else if(val == 2){ // If the serial value is not 0, translate(hx,hx-=10); } } void bomb(){ int p=0; image(bomb,hx+90,hy+by,10,15); if(val != 4 && by!=0){ by-=10; } if (val == 4){ by-=10; } if((hy+by)<=0){ by=0; } }
//大的圓形鈕 const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; const int buttonPin6 = 7; //確認鈕 int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int buttonState6 = 0; int input=0, oldInput=0, a=0; void setup(){ Serial.begin(9600); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); pinMode(buttonPin6, INPUT); } void loop(){ buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); buttonState6 = digitalRead(buttonPin6); if(buttonState1== HIGH) input=1; else if(buttonState2== HIGH) input=2; else if(buttonState3== HIGH) input=3; else if(buttonState4== HIGH) input=4; else if(buttonState5== HIGH) input=5; else if(buttonState6== HIGH) input=6; else input=0; if(input!=oldInput){ if(input==1){ Serial.print(1); oldInput=input; a=1; }else if(input==2){ Serial.print(2); oldInput=input; a=2; }else if(input==3){ Serial.print(3); oldInput=input; a=3; }else if(input==4){ Serial.print(4); oldInput=input; a=4; }else if(input==5){ Serial.print(5); oldInput=input; a=5; }else if(input==6){ Serial.print(6); oldInput=input; a=6; } } }Processing:
import processing.serial.*; Serial myport; PImage rule, start; //開始 PImage easy, nor, hard, bEasy, bNormal, bHard; //選單 PImage gmioumi1, gmioumi2, gmioumi3, gmioumi4, gmioumi5; //底圖 PImage mioumi1, mioumi2, mioumi3, mioumi4, mioumi5; //亮圖 PImage pass, over, win, End;//結果 float x=0, y=0, z=0, oldx=0, nowx=0; float xTime=3; String str="", str2="",oldstr2="", a="6",b="6", c="", oldc=""; int inByte=0, choose=0, end=3, e=0, f=0, g=0; void setup(){ myport=new Serial(this,"COM3",9600); //開始 rule = loadImage("rule.jpg"); start = loadImage("start.png"); //選單 easy = loadImage("easy.png"); nor = loadImage("normal.png"); hard = loadImage("hard.png"); bEasy = loadImage("bEasy.jpg"); bNormal = loadImage("bNormal.jpg"); bHard = loadImage("bHard.jpg"); //底圖 gmioumi1 = loadImage("g1.jpg"); gmioumi2 = loadImage("g2.jpg"); gmioumi3 = loadImage("g3.jpg"); gmioumi4 = loadImage("g4.jpg"); gmioumi5 = loadImage("g5.jpg"); //亮圖 mioumi1 = loadImage("c1.png"); mioumi2 = loadImage("c2.png"); mioumi3 = loadImage("c3.png"); mioumi4 = loadImage("c4.png"); mioumi5 = loadImage("c5.png"); //結果 pass = loadImage("pass.jpg"); over = loadImage("over.jpg"); win = loadImage("win.jpg"); End = loadImage("end.jpg"); size(600,400); background(0,0,0); } void draw(){ if(myport.available()>0){ //測試有無訊號來 inByte=myport.read(); //如果有來就read if(inByte==1+48) c="1"; else if(inByte==2+48) c="2"; else if(inByte==3+48) c="3"; else if(inByte==4+48) c="4"; else if(inByte==5+48) c="5"; else if(inByte==6+48) c="6"; } if(c!=oldc){ println(c); oldc=c; } if(c==""){ image(rule,50,50,500,250); image(start,200,320,200,50); e=1; }else if(c=="6" && e==1){ fill(0,0,0); rect(0,0,600,400); image(easy,50,50,160,50); image(nor,220,50,160,50); image(hard,390,50,160,50); image(bEasy,50,100,160,250); image(bNormal,220,100,160,250); image(bHard,390,100,160,250); c="0"; } if(c=="1" && e==1){ choose=1; e=2; c="0"; }else if(c=="3" && e==1){ choose=3; e=2; c="0"; }else if(c=="5" && e==1){ choose=5; e=2; c="0"; } if(choose==1){ //easy if(y<6){ if(millis()-xTime>1000){ x=random(5); b=a; nowx=(int)x/1; if(nowx==oldx){ if(nowx==4){ x=1; oldx=(int)x/1; }else if(nowx==5){ x=2; oldx=(int)x/1; }else{ x=x+1; oldx=(int)x/1; } }else oldx=(int)x/1; if(x>0 && x<=1){ fill(0,0,0); rect(0,0,600,400); image(mioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="1"; }else if(x>1 && x<=2){ fill(0,0,0); rect(0,0,600,400); image(mioumi2,150,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="2"; }else if(x>2 && x<=3){ fill(0,0,0); rect(0,0,600,400); image(mioumi3,250,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="3"; }else if(x>3 && x<=4){ fill(0,0,0); rect(0,0,600,400); image(mioumi4,350,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi5,450,100,100,100); a="4"; }else if(x>4 && x<=5){ fill(0,0,0); rect(0,0,600,400); image(mioumi5,450,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); a="5"; } xTime=millis(); y=y+1; if(y<6 && b!=a){ str=str+a; println("*"+str); z=0; }else if(y<6 && z==0 && b==a){ str=str+a; println("*"+str); z=1; } }else if(millis()-xTime>600 && millis()-xTime<800){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } }else if(e==2){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } if(c!=oldstr2){ if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ oldstr2=c; str2=str2+c; println("**"+str2+"**"); } } }else if(choose==3){ //normal if(y<8){ if(millis()-xTime>1000){ x=random(5); b=a; nowx=(int)x/1; if(nowx==oldx){ if(nowx==4){ x=1; oldx=(int)x/1; }else if(nowx==5){ x=2; oldx=(int)x/1; }else{ x=x+1; oldx=(int)x/1; } }else oldx=(int)x/1; if(x>0 && x<=1){ fill(0,0,0); rect(0,0,600,400); image(mioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="1"; }else if(x>1 && x<=2){ fill(0,0,0); rect(0,0,600,400); image(mioumi2,150,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="2"; }else if(x>2 && x<=3){ fill(0,0,0); rect(0,0,600,400); image(mioumi3,250,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="3"; }else if(x>3 && x<=4){ fill(0,0,0); rect(0,0,600,400); image(mioumi4,350,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi5,450,100,100,100); a="4"; }else if(x>4 && x<=5){ fill(0,0,0); rect(0,0,600,400); image(mioumi5,450,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); a="5"; } xTime=millis(); y=y+1; if(y<8 && b!=a){ str=str+a; println(str); z=0; }else if(y<8 && z==0 && b==a){ str=str+a; println(str); z=1; } }else if(millis()-xTime>600 && millis()-xTime<800){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } }else if(e==2){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } if(c!=oldstr2){ if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ oldstr2=c; str2=str2+c; println("**"+str2+"**"); } } }else if(choose==5){ //hard if(y<9){ if(millis()-xTime>500){ x=random(5); b=a; nowx=(int)x/1; if(nowx==oldx){ if(nowx==4){ x=1; oldx=(int)x/1; }else if(nowx==5){ x=2; oldx=(int)x/1; }else{ x=x+1; oldx=(int)x/1; } }else oldx=(int)x/1; if(x>0 && x<=1){ fill(0,0,0); rect(0,0,600,400); image(mioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="1"; }else if(x>1 && x<=2){ fill(0,0,0); rect(0,0,600,400); image(mioumi2,150,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="2"; }else if(x>2 && x<=3){ fill(0,0,0); rect(0,0,600,400); image(mioumi3,250,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); a="3"; }else if(x>3 && x<=4){ fill(0,0,0); rect(0,0,600,400); image(mioumi4,350,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi5,450,100,100,100); a="4"; }else if(x>4 && x<=5){ fill(0,0,0); rect(0,0,600,400); image(mioumi5,450,100,100,100); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); a="5"; } xTime=millis(); y=y+1; if(b!=a){ str=str+a; println(str); z=0; }else if(z==0 && b==a){ str=str+a; println(str); z=1; } }else if(millis()-xTime>200 && millis()-xTime<400){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } }else if(e==2){ fill(0,0,0); rect(0,0,600,400); image(gmioumi1,50,100,100,100); image(gmioumi2,150,100,100,100); image(gmioumi3,250,100,100,100); image(gmioumi4,350,100,100,100); image(gmioumi5,450,100,100,100); } if(c!=oldstr2){ if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ oldstr2=c; str2=str2+c; println("**"+str2+"**"); e=3; } } } if(str=="" || str2==""){ str="1"; str2="1"; } f=Integer.parseInt(str); g=Integer.parseInt(str2); //f= new Integer(str).intValue(); //g= new Integer(str2).intValue(); if(c=="6" && e==2){ if(f==g){ image(pass,0,0,600,400); }else if(f!=g){ image(over,0,0,600,400); } }else if(c=="6" && e==3){ if(f==g){ image(win,0,0,600,400); }else{ image(over,0,0,600,400); } } if(mousePressed && mouseX>500 && mouseY>300){ if(choose==1){ choose=3; str=""; str2=""; }else if(choose==3){ choose=5; str=""; str2=""; } e=1; }else if(mousePressed && mouseX<100 && mouseY>300){ image(End,0,0,600,400); c="0"; e=4; } }