這是按鈕
我負責軟體,黃豊翔負責硬體import ddf.minim.*;
import processing.serial.*;
PrintWriter output;
Minim minim;
AudioPlayer bgm;
int score=0,combo=000,ansSum=10,clock,s,ms,m=0,befos=0,befoms=0,befom=0,length;
int [] ansTime={1,2,3,4,5,6,7,8,9,10};//拍子的出現時間(秒)
char ansKey[]={'1','2','3','4','5','6','7','8','9','1'};//要按的紐
boolean[] anesd={false,false,false,false,false,false,false,false,false,false};//是否有按過
//int [] ansTime;
//char ansKey[];
char keyPressd='?',okeyPressd=' ';
BufferedReader reader;
BufferedReader rank;
PrintWriter Edit;
String line;
String[] Ans;
Serial myport;
boolean load,edit,playbeat=false;
String[] pieces;
boolean[] btn={false,false,false,false,false,false,false,false,false};//是否為要按的 按鍵0->1 1->2 其他類推
boolean T1,T2,T3,R1,R2,L1,L2;//要畫出的方塊
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup(){
//reader = createReader("gangnamhit.txt");
size(600,650);
minim = new Minim(this);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
//myport=new Serial(this,"com7",9600);連接物理ardunio
}
void keyPressed(){
keyPressd=key;
// judge(s);
//print(keyPressd);
switch(keyPressd){
/* case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
print(int(keyPressd)-48);
if(btn[int(keyPressd)-49]==true){
btn[int(keyPressd)-49]=false;
score++;
combo++;
}
break;*/
case 's':
bgm = minim.loadFile("Elec.mp3");//我爸剛
bgm.play();//放音樂
befos=s;
befoms=ms;
befom=m;
break;
case 'l':
reader = createReader("songset.txt");
bgm = minim.loadFile("Elec.mp3");//我爸剛
load=true;
break;
case 'e':
output = createWriter("songset.txt");
bgm = minim.loadFile("Elec.mp3");//我爸剛
bgm.play();
edit=true;
break;
}
if(edit)edit();
}
void loadsong(){
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
line = null;
}
if (line == null) {
// Stop reading because of an error or file is empty
// noLoop();
print("/ndone1");
bgm.play();//放音樂
befos=s;
befoms=ms;
befom=m;
print(" and start");
load=false;
playbeat=true;
} else {
pieces = split(line, " ");
length=pieces.length;
for(int i=0;i<pieces.length;i++){
//*
if(i%10==1)//id
print("id:"+pieces[i]);
if(i%10==3)//beat
print("beat:"+pieces[i]);
if(i%10==5)//min
print("min:"+pieces[i]);
if(i%10==7)//s
print("s:"+pieces[i]);
if(i%10==9)//ms
print("ms:"+pieces[i]);
//*/
//print(pieces[i]);
}
//print(pieces[(pieces.length-1)]);
}
}
int i=1;
void edit(){
if(keyPressd=='q'){
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}else{
// case 6: case 7: case 8: case 9:
output.print("<ID> "+i+" ");
output.print("<BEAT> "+keyPressd+" ");
output.println("true "+m+" min "+s+" s "+ms+" ms");
i++;
}
}
void beating(int btnl,int NoAnsednber){
boolean NoAnsed =boolean(pieces[NoAnsednber]);
if(NoAnsed)
btn[btnl-1]=true;
for(int by=0;by<=2;by++)//畫出方形
for(int bx=0;bx<=2;bx++){
if(btn[7-3*by+bx-1]) fill(0);
else fill(200);
rect(10+bx*200, 50+by*200, 180, 180);
}
if(int(keyPressd)-48==btnl && NoAnsed)//(int(keyPressd)-49)==btnl-1
{
btn[btnl-1]=false;
pieces[NoAnsednber]=str(false);
score++;
combo++;
}
}
/*void judge(){
if(int(keyPressd)>48&&int(keyPressd)<58){
if(btn[int(keyPressd)-49]==true&&int(okeyPressd)!=int(keyPressd)){
btn[int(keyPressd)-49]=false;
score++;
combo++;
}
okeyPressd=keyPressd;
}
}
*/
void draw(){
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
// if(val-49<0)val=58;
//println(val-49);
switch(val){
case 50: keyPressd='1'; break;
case 51: keyPressd='2'; break;
case 52: keyPressd='3'; break;
case 53: keyPressd='4'; break;
case 54: keyPressd='5'; break;
case 55: keyPressd='6'; break;
case 56: keyPressd='7'; break;
case 57: keyPressd='8'; break;
case 49:
case 48: keyPressd='9'; break;
}
// keyPressd=char(val-1);
}
if(load)loadsong();
// print(" "+s+"秒");
//方格
///////////////
//00_7 10_8 20_9
//01_4 11_5 21_6
//02_1 12_2 22_3
///////////////
// int pA=myport.read();
background(255);
int black=999;
clock= millis();
s = clock/1000-m*60-befos;//SECOND s = millis()/1000;
ms = clock%1000-befoms;//mili-SECOND
if(s>=60){
m++;
s-=60;
}
// m= clock/(60*1000)-befom;
if(s<=0)s=0;
char currentI='+';
if(playbeat)
for(int i=0;i<length/10;i++){
// print("進入迴圈"+pieces[i*10+5]+pieces[5]+int(pieces[i*10+5]));
//if()
if(int(pieces[i*10+5])<m)continue;
else if(int(pieces[i*10+5])==m)//分鐘
{//出來一秒
if(int(pieces[i*10+7])<=s-2)//second
{ if(boolean(pieces[i*10+4])==true){
combo=0;
pieces[i*10+4]=str(false);
}
continue;
}
else if(int(pieces[i*10+7])==s-1)//second
{ // print("進入迴圈分鐘前");
if(ms<=int(pieces[i*10+9])-250)//ms//ms+(1000-pieces[i*10+9])<=1000間隔一秒
{
print("進入迴圈分鐘"+"ID:"+int(pieces[i*10+1])+"<"+int(pieces[i*10+5])+"分"+int(pieces[i*10+7])+"秒");
beating(int(pieces[i*10+3])/*按鍵*/,i*10+4);//顯示出來
// print("進入迴圈分鐘前顯示");
}
}
else if(int(pieces[i*10+7])==s)//second
{
//print("進入迴圈分鐘後");
if(ms-int(pieces[i*10+9])>=250)//ms間隔一秒之內
{
print("進入迴圈分鐘"+"ID:"+int(pieces[i*10+1])+"<"+int(pieces[i*10+5])+"分"+int(pieces[i*10+7])+"秒");
beating(int(pieces[i*10+3])/*按鍵*/,i*10+4);
// print("進入迴圈分鐘後顯示");
}else break;//超過一秒跳出
}
}
}
///////////////
//00_7 10_8 20_9
//01_4 11_5 21_6
//02_1 12_2 22_3
///////////////
for(int i=0;i<9;i++){
if(btn[i])break;
else if(i<=7)continue;
else if(i==8){
fill(200);
for(int by=0;by<=2;by++)//畫出方形
for(int bx=0;bx<=2;bx++)
rect(10+bx*200, 50+by*200, 180, 180);
}
}
for(int i=0;i<9;i++){
btn[i]=false;
}
//分數
fill(0,0,0,120);
for(int digit=0;digit<=2;digit++){
int drawNumber=(int)(score/pow(10,digit))%10;//找出要畫的數字
T1=T2=T3=R1=R2=L1=L2=false;
switch(drawNumber){
case 0:T1=T3=R1=R2=L1=L2=true; break;
case 1:R1=R2=true; break;
case 2:T1=T2=T3=R1=L2=true; break;
case 3:T1=T2=T3=R1=R2=true; break;
case 4:T2=R1=R2=L1=true; break;
case 5:T1=T2=T3=R2=L1=true; break;
case 6:T1=T2=T3=R2=L1=L2=true; break;
case 7:T1=R1=R2=true; break;
case 8:T1=T2=T3=R1=R2=L1=L2=true;break;
case 9:T1=T2=T3=R1=R2=L1=true; break;
}
if(T1)rect(350-150*digit, 260, 60, 20);//橫一Transverse T1
if(R1)rect(410-150*digit, 280, 20, 60);//R1
if(T2)rect(350-150*digit ,340, 60, 20);//T2
if(R2)rect(410-150*digit, 360, 20, 60);//R2
if(T3)rect(350-150*digit, 420, 60, 20);//T3
if(L2)rect(330-150*digit, 360, 20, 60);//L1
if(L1)rect(330-150*digit, 280, 20, 60);//L2
}
for(int digit=0;digit<=2;digit++){
int drawNumber=(int)(combo/pow(10,digit))%10;//找出要畫的數字
T1=T2=T3=R1=R2=L1=L2=false;
switch(drawNumber){
case 0:T1=T3=R1=R2=L1=L2=true; break;
case 1:R1=R2=true; break;
case 2:T1=T2=T3=R1=L2=true; break;
case 3:T1=T2=T3=R1=R2=true; break;
case 4:T2=R1=R2=L1=true; break;
case 5:T1=T2=T3=R2=L1=true; break;
case 6:T1=T2=T3=R2=L1=L2=true; break;
case 7:T1=R1=R2=true; break;
case 8:T1=T2=T3=R1=R2=L1=L2=true;break;
case 9:T1=T2=T3=R1=R2=L1=true; break;
}
if(T1)rect(350-150*digit, 460, 60, 20);//橫一Transverse T1
if(R1)rect(410-150*digit, 480, 20, 60);//R1
if(T2)rect(350-150*digit ,540, 60, 20);//T2
if(R2)rect(410-150*digit, 560, 20, 60);//R2
if(T3)rect(350-150*digit, 620, 60, 20);//T3
if(L2)rect(330-150*digit, 560, 20, 60);//L1
if(L1)rect(330-150*digit, 480, 20, 60);//L2
}
}
ARDUNIO
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton[] = {2,3,4,5,6,7,8,9,10};
boolean checkstat[] = {false,false,false,false,false,false,false,false,false};
int buttonState=0,otarg=0;
//int targ;
int ans[]= {1,1};
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
for(int i=0;i<9;i++)
pinMode(pushButton[i], INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
// print out the state of the button
for(int targ=0;targ<9;++targ )
if( digitalRead(targ+2)==1){
//buttonState = digitalRead(targ);
if(otarg!=targ) {
otarg=targ;
Serial.print(targ+2);
}
//if((targ+1)==otarg)
//Serial.print(otarg);
// if((otarg+1)==targ)
//Serial.print(otarg);
}
delay(1); // delay in between reads for stability
}
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton[] = {2,3,4,5,6,7,8,9,10};
boolean checkstat[] = {false,false,false,false,false,false,false,false,false};
int buttonState=0,otarg=0;
//int targ;
int ans[]= {1,1};
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
for(int i=0;i<9;i++)
pinMode(pushButton[i], INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
// print out the state of the button
for(int targ=0;targ<9;++targ )
if( digitalRead(targ+2)==1){
//buttonState = digitalRead(targ);
if(otarg!=targ) {
otarg=targ;
Serial.print(targ+2);
}
//if((targ+1)==otarg)
//Serial.print(otarg);
// if((otarg+1)==targ)
//Serial.print(otarg);
}
delay(1); // delay in between reads for stability
}
沒有留言:
張貼留言