2017年1月10日 星期二

Week 18 Sound Voltex

Sound Voltex
    1.當黑色方塊於底部色塊時,點擊相對應的按鍵(按鈕),消除並加分
    2.利用滑鼠(旋鈕)控制黃色方塊,當藍色長條於底部色塊碰到黃色方塊時,消除並加分


    使用Excel紀錄陣列,使用於來黑方塊及藍長條是否出現及出現位置

    Arduino外接裝置



程式碼:
1.Processing

Table table1,table2;
int box[][] = new int [200][4];
int knob[][] = new int [200][6];
int myKey[]={0,0,0,0};
int myKnob[]={0,0,0,0,0,0};
int score=0;
boolean addScore = false;
float roll=0;//-1...+1
import processing.serial.*;//arduino
Serial myPort;
int val;
float sensorValue;
import processing.sound.*;
SoundFile music;
void setup(){
  size(440,540,P3D);
  table1 = loadTable("table1.csv");
  table2 = loadTable("table2.csv");
  myPort = new Serial(this, "COM4", 9600);
  for(int r=0;r<200;r++){
    for(int c=0;c<4;c++){
      if(table1.getInt(r,c)==0) box[r][c]=0;
      else box[r][c]=1;
    }
    for(int c=0;c<6;c++){
      if(table2.getInt(r,c)==0) knob[r][c]=0;
      else knob[r][c]=1;
    }
  }
  frameRate(30);
  music = new SoundFile(this, "music.mp3");
  music.play();
}
void draw(){
  background(255,255,87);
  buttonPressed();

  pushMatrix();
    translate(width/2,height/3,0);//box_path

    fill(0);//score
    textSize(40);
    text("Score:"+score,-200,-135);
 
    rotateZ( radians(-roll*30));//roll
    rotateX(PI/4);
    rectMode(CENTER);
    fill(255);
    stroke(0);
    rect(0,0,240,600);
 
    line(-80,-300,0,-80,300,0);//line
    line(-40,-300,0,-40,300,0);
    line(0,-300,0,0,300,0);
    line(40,-300,0,40,300,0);
    line(80,-300,0,80,300,0);
 
    translate(-100,0,0.1);//knob_path
    fill(0,0,255,60);
    noStroke();
    rect(0,0,40,600);
    translate(200,0,0);
    fill(255,0,0,60);
    noStroke();
    rect(0,0,40,600);

    noStroke();//background2
    fill(94,87);
    rect(-100,289,160,23);
 
    translate(0,0,0.1);//rollBox
    fill(255,255,0);
    rect(-100+roll*200, 289, 40, 20, 7);

    translate(-160,-290,0.1);
    scale(1,0.5,0);
    for(int r=0;r<200;r++){
      for(int c=0;c<4;c++){
        if(1120 < -r*100+frameCount*10 &&
        box[r][c]==1 &&
        box[r][c]==myKey[c]){
          myKey[c]=0;
          score+=10;
          break;
        }
        if(1160 < -r*100+frameCount*10 || 0 > -r*100+frameCount*10)break;
     
        if(table1.getInt(r,c)==1){
          fill(0);
          rect(c*40, -r*100+frameCount*10, 40, 40);
        }
      }  
    }
    for(int r=0;r<200;r++){
      for(int c=0;c<6;c++){
        if(1120 < -r*100+frameCount*10 && knob[r][c]==1){
          /*if(c==0 && roll<-0.4){
            score+=5;
            break;
          }*/
          if(c==1 && roll>=-0.4 && roll<-0.2){
            score+=5;
            break;
          }
          if(c==2 && roll>=-0.2 && roll<0){
            score+=5;
            break;
          }
          if(c==3 && roll>=0 && roll<0.2){
            score+=5;
            break;
          }
          if(c==4 && roll>=0.2 && roll<0.4){
            score+=5;
            break;
          }
          if(c==5 && roll>=0.4){
            score+=5;
            break;
          }
        }
     
        if(1135 < -r*100+frameCount*10 || 35 > -r*100+frameCount*10)break;
        if(table2.getInt(r,c)==1){
          fill(0,0,255,80);
          rect(-40+c*40, -r*100+frameCount*10, 40, 100);
        }
      }
    }

  popMatrix();
//  println(score);
//println(roll);
}
void keyPressed(){
  if(key=='f') myKey[0]=1;
  if(key=='g') myKey[1]=1;
  if(key=='h') myKey[2]=1;
  if(key=='j') myKey[3]=1;
}
/*void keyReleased(){
  if(key=='f') myKey[0]=0;
  if(key=='g') myKey[1]=0;
  if(key=='h') myKey[2]=0;
  if(key=='j') myKey[3]=0;
}*/
void mouseMoved(){
  roll=(mouseX-width/2)/ float(width);
}
void buttonPressed(){//arduino
  while (myPort.available() > 0){
    val = myPort.read();
    if(val==48) myKey[0]=1;
    if(val==49) myKey[1]=1;
    if(val==50) myKey[2]=1;
    if(val==51) myKey[3]=1;
 
  }
/*  while(myPort.available() > 0){
    println(sensorValue);
    sensorValue = myPort.read();
    roll = -0.5+sensorValue/255;
  }*/

}

2.Arduino

int potPin = 0;
void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}
void loop() {

  int sensorVal = digitalRead(2);
  int sensorVal4 = digitalRead(3);
  int sensorVal5 = digitalRead(4);
  int sensorVal6 = digitalRead(5);


  if (sensorVal == HIGH) Serial.print('0');
  if (sensorVal4 == HIGH) Serial.print('1');
  if (sensorVal5 == HIGH) Serial.print('2');
  if (sensorVal6 == HIGH) Serial.print('3');

  int sensorValue = analogRead(potPin);
  Serial.write(sensorValue/4);
  delay(100);
}

沒有留言:

張貼留言