顯示具有 99163186黃彥鈞 標籤的文章。 顯示所有文章
顯示具有 99163186黃彥鈞 標籤的文章。 顯示所有文章

2014年1月12日 星期日

第10組-互動技術期末作品-節奏音感-99163116,99163186

遊戲名稱:節奏音感
遊戲玩法:在放音樂的過程中,會有音階隨機掉下來,操作的方法就是將Processing與Arduino的程式碼互相連接,再利用Arduino硬體組成的5個按鍵來操作,當音階到達指定的位置時按下,程式碼就會依照所設定的預設值範圍來判斷有無按到,在按到的過程中會出現閃光的特效,每按成功一次就加分數100分,直到音樂放完為止,遊戲結束後,在最後的畫面會出現總分。

Processing程式碼:
PImage bg,blue,orange,green,blue1,orange1;
PImage imgPub;
PImage imgMusic;
PImage imgPop;
import processing.serial.*;
Serial myPort;
int N=6;
int count = 9700;
float []thingX=new float[6], thingY=new float[6],thingType = new float[6];
int grade=0;
int val;
import ddf.minim.*;
Minim m;
AudioPlayer player;
void setup(){
  myPort = new Serial(this, "COM4", 9600);
  imgPub=loadImage("Pub.jpg");
  imgMusic=loadImage("Music.jpg");
  imgPop=loadImage("Pop.jpg");
  image(imgPop,400,600);
  image(imgMusic,400,600);
  image(imgPub,400,600);

  bg = loadImage("background.jpg");
  blue = loadImage("blue.jpg");
  orange = loadImage("orange.gif");
  green = loadImage("green.jpg");
  blue1 = loadImage("blue1.gif");
  orange1 = loadImage("orange1.jpg");

  m = new Minim(this);
  player = m.loadFile("DNA.mp3");
  size(400,600);
  for(int i=0;i<N;i++){
    newThing(i);
  }
}
int countDown=10*30;
void draw(){
  player.play();
  val = myPort.read();
  image(imgPop,0,0,400,600);
  if(keyPressed)
   
        textSize(40);
        text("Start!!", 250, 300);
   
   if(key=='s')
 
      image(imgMusic,0,0,400,600);
      if(countDown>0){
      //background(255,0,255);
    countDown--;
    text("Time: "+countDown/50,170,300);
   
    return;
      }

  image(bg,0,0, 400,600);
  image(blue,0,575,80,40);
  image(orange,80,575,80,40);
  image(green,160,575,80,40);
  image(blue1,240,575,80,40);
  image(orange1,320,575,80,40);


  for(int i=0;i<N;i++){
    if(thingType[i]==1)
    {
     drawblue(i);
      //if (keyPressed)
   
        //if(key=='d'|| key=='D')
     
     
        println(val);
        if(525<thingY[i] && thingY[i]<700 && val==50)
        {
          thingY[i] = 650; grade+=100; //println(grade);
          fill(#0046FF,90);
          rect(0,575,80,-600);
        }
   
   
    }
    else if(thingType[i]==2)
    {
      draworange(i);
      //if (keyPressed)
   
      {
        //if(key=='f'|| key=='F')
     
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==51)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#FF7D00,90);
        rect(80,575,80,-600);
        }
      }
    }
    else if(thingType[i]==3)
    {
      drawgreen(i);
      //if (keyPressed)
   
      {
        //if(key=='b'|| key=='B')
     
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==52)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#10FF00,90);
        rect(160,575,80,-600);
      }
      }
    }
    else if(thingType[i]==4)
    {
      drawblue1(i);
      //if (keyPressed)
   
      {
        //if(key=='k'|| key=='K')
     
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==49)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#0046FF,90);
        rect(240,575,80,-600);
      }
      }
    }
    else if(thingType[i]==5)
    {
      draworange1(i);
      //if (keyPressed)
   
      {
        //if(key=='j'|| key=='J')
     
        println(val);
        if (525<thingY[i] && thingY[i]<600 && val==53)
        {
          thingY[i] = 650; grade+=100; println(grade);
          fill(#FF7D00,90);
          rect(320,575,80,-600);
        }
      }
    }
    thingY[i]+=3;
    if(thingY[i]>600) newThing(i);
   }
  if(count>0){
    count--;
    text("Time: "+count/50,170,300);
    }
    if(count==0){
      //background(#FF0015);
      //background(imgMusic,400,600);
      image(imgMusic,0,0,400,600);
      fill(255,0,0);
      text("Score: "+grade,122,300);
 
    }
}
void newThing(int i){
  thingY[i] = -random(200);
  thingX[i] = random(400);
  thingType[i] = int(random(5))+1;
}
void drawblue(int i){
  thingX[i]=int(random(0,0));
  image(blue,thingX[i],thingY[i],80,22);
}
void draworange(int i){
  thingX[i]=int(random(80,80));
  image(orange,thingX[i],thingY[i],80,22);
}
void drawgreen(int i){
  thingX[i]=int(random(160,160));
  image(green,thingX[i],thingY[i],80,22);
}
void drawblue1(int i){
  thingX[i]=int(random(240,240));
  image(blue1,thingX[i],thingY[i],80,22);
}
void draworange1(int i){
  thingX[i]=int(random(320,320));
  image(orange1,thingX[i],thingY[i],80,22);
}










Arduino硬體程式碼:
int buttonPin5 = 5;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin6 = 6;

int buttonState5 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState6 = 0;
void setup() {

  pinMode(buttonPin5, INPUT);  
  pinMode(buttonPin2, INPUT);  
  pinMode(buttonPin3, INPUT);    
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin6, INPUT);

  Serial.begin(9600);

}
void loop() {
  buttonState5 = digitalRead(buttonPin5);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState6 = digitalRead(buttonPin6);

  if (buttonState5 == HIGH) {  

   Serial.write('1');
  }
  if (buttonState2== HIGH) {  

   Serial.print('2');
  }
  if (buttonState3 == HIGH) {  
 
   Serial.print('3');
  }
  if (buttonState4 == HIGH) {  
 
   Serial.print('4');
  }
  if (buttonState6 == HIGH) {  
 
   Serial.print('5');
  }
  else if(buttonState2 == LOW&&buttonState3 == LOW&&buttonState4 == LOW&&buttonState5 == LOW&&buttonState6 == LOW){
  Serial.write('0');
  }
  delay(100);

}

Arduino硬體電路連接:






圖片結果截圖:




影片DEMO:
遊戲執行結果:

電腦側錄結果:

Week16

int buttonPin5 = 5;
int buttonPin2 = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin6 = 6;

int buttonState5 = 0; 
int buttonState2 = 0; 
int buttonState3 = 0; 
int buttonState4 = 0;
int buttonState6 = 0;
void setup() {

  pinMode(buttonPin5, INPUT);    
  pinMode(buttonPin2, INPUT);    
  pinMode(buttonPin3, INPUT);     
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin6, INPUT);

  Serial.begin(9600);

}
void loop() {
  buttonState5 = digitalRead(buttonPin5);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState6 = digitalRead(buttonPin6);

  if (buttonState5 == HIGH) {    

   Serial.write('1');
  }
  if (buttonState2== HIGH) {    

   Serial.print('2');
  }
  if (buttonState3 == HIGH) {    
  
   Serial.print('3');
  }
  if (buttonState4 == HIGH) {    
  
   Serial.print('4');
  }
  if (buttonState6 == HIGH) {    
  
   Serial.print('5');
  }
  else if(buttonState2 == LOW&&buttonState3 == LOW&&buttonState4 == LOW&&buttonState5 == LOW&&buttonState6 == LOW){
  Serial.write('0');
  }
  delay(100); 

Week 15

Processing程式碼:
PImage bg,blue,orange,green,blue1,orange1;
PImage imgPub;
PImage imgMusic;
PImage imgPop;
import processing.serial.*;
Serial myPort;
int N=6;
int count = 9700;
float []thingX=new float[6], thingY=new float[6],thingType = new float[6];
int grade=0;
int val;
import ddf.minim.*;
Minim m;
AudioPlayer player;
void setup(){
  myPort = new Serial(this, "COM4", 9600);
  imgPub=loadImage("Pub.jpg");
  imgMusic=loadImage("Music.jpg");
  imgPop=loadImage("Pop.jpg");
  image(imgPop,400,600);
  image(imgMusic,400,600);
  image(imgPub,400,600);
 
  bg = loadImage("background.jpg");
  blue = loadImage("blue.jpg");
  orange = loadImage("orange.gif");
  green = loadImage("green.jpg");
  blue1 = loadImage("blue1.gif");
  orange1 = loadImage("orange1.jpg");
 
  m = new Minim(this);
  player = m.loadFile("DNA.mp3");
  size(400,600);
  for(int i=0;i<N;i++){
    newThing(i);
  }
}
int countDown=10*30;
void draw(){
  player.play();
  val = myPort.read();
  image(imgPop,0,0,400,600);
  if(keyPressed)
     
        textSize(40);
        text("Start!!", 250, 300);
    
   if(key=='s')
  
      image(imgMusic,0,0,400,600);
      if(countDown>0){
      //background(255,0,255);
    countDown--;
    text("Time: "+countDown/50,170,300);
    
    return;
      }
 
  image(bg,0,0, 400,600);
  image(blue,0,575,80,40);
  image(orange,80,575,80,40);
  image(green,160,575,80,40);
  image(blue1,240,575,80,40);
  image(orange1,320,575,80,40);
 
 
  for(int i=0;i<N;i++){
    if(thingType[i]==1)
    {
     drawblue(i);
      //if (keyPressed)
     
        //if(key=='d'|| key=='D')
       
      
        println(val);
        if(525<thingY[i] && thingY[i]<700 && val==50)
        {
          thingY[i] = 650; grade+=100; //println(grade);
          fill(#0046FF,90);
          rect(0,575,80,-600);
        }
     
     
    }
    else if(thingType[i]==2)
    {
      draworange(i);
      //if (keyPressed)
     
      {
        //if(key=='f'|| key=='F')
       
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==51)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#FF7D00,90);
        rect(80,575,80,-600);
        }
      }
    }
    else if(thingType[i]==3)
    {
      drawgreen(i);
      //if (keyPressed)
     
      {
        //if(key=='b'|| key=='B')
       
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==52)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#10FF00,90);
        rect(160,575,80,-600);
      }
      }
    }
    else if(thingType[i]==4)
    {
      drawblue1(i);
      //if (keyPressed)
     
      {
        //if(key=='k'|| key=='K')
       
        //println(val);
        if(525<thingY[i] && thingY[i]<600 && val==49)
        {
        thingY[i] = 650; grade+=100; println(grade);
        fill(#0046FF,90);
        rect(240,575,80,-600);
      }
      }
    }
    else if(thingType[i]==5)
    {
      draworange1(i);
      //if (keyPressed)
     
      {
        //if(key=='j'|| key=='J')
       
        println(val);
        if (525<thingY[i] && thingY[i]<600 && val==53)
        {
          thingY[i] = 650; grade+=100; println(grade);
          fill(#FF7D00,90);
          rect(320,575,80,-600);
        }
      }
    }
    thingY[i]+=3;
    if(thingY[i]>600) newThing(i);
   }
  if(count>0){
    count--;
    text("Time: "+count/50,170,300);
    }
    if(count==0){
      //background(#FF0015);
      //background(imgMusic,400,600);
      image(imgMusic,0,0,400,600);
      fill(255,0,0); 
      text("Score: "+grade,122,300);
   
    }
}
void newThing(int i){
  thingY[i] = -random(200);
  thingX[i] = random(400);
  thingType[i] = int(random(5))+1;
}
void drawblue(int i){
  thingX[i]=int(random(0,0));
  image(blue,thingX[i],thingY[i],80,22);
}
void draworange(int i){
  thingX[i]=int(random(80,80));
  image(orange,thingX[i],thingY[i],80,22);
}
void drawgreen(int i){
  thingX[i]=int(random(160,160));
  image(green,thingX[i],thingY[i],80,22);
}
void drawblue1(int i){
  thingX[i]=int(random(240,240));
  image(blue1,thingX[i],thingY[i],80,22);
}
void draworange1(int i){
  thingX[i]=int(random(320,320));
  image(orange1,thingX[i],thingY[i],80,22);
}

2013年12月8日 星期日

99163116鄭祥斌, HW14, HW14 期中作品企劃

有關於這次期末作品的計畫,我們想做有關Pop'n Music的作品,因為在逛街時常看到有關於Pop'n Music的音樂機台,想說以此為這次期末作品的目標,以下是我們參考的影片。
有關控制的部分,我們會裡用Arduino的控制電路版以及麵包版透過線路的連接分別連到5個不同的按鈕,以利我們控制。

2013年12月1日 星期日

Week 13

Lesson 5 Mode Cue
利用手去轉馬達,使得風扇轉動

#include <servo .h="">
Servo myServo;
int const potPin =A0;
int potVal;
int angle;
void setup(){
  myServo.attach(9);
  Serial.begin(9600);
}
void loop(){
  potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  //這裡的 map function 可以把 0~1023 轉換到 0~179度
  //很方便的函示
  angle=map(potVal, 0, 1023, 0, 179);
  Serial.print(", angle: ");
  Serial.print(angle);
  Serial.print("\r\n");
  myServo.write(angle);
  delay(15);
}






Lesson 6 Light Theremin
利用光線的強弱來發出聲音

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  //遮住/打開光敏電阻,讓程式修正最大和最小值
  while (millis() &lt; 5000) {
    sensorValue = analogRead(A0);
    if (sensorValue &gt; sensorHigh) {
      sensorHigh = sensorValue;
    }
    if (sensorValue &lt; sensorLow) {
      sensorLow = sensorValue;
    }
  }
  //開始進入
  digitalWrite(ledPin, LOW);
}

void loop() {
  sensorValue = analogRead(A0);
  //對應最高和最低的數值,對應為 50~4000 hz
  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
  tone(8, pitch, 20);
  delay(10);
}





Lesson 7 Keyboard Instrument
按鍵發出樂器的聲音

int notes[]={262,294,330,349}; void setup() { Serial.begin(9600); } void loop() { int keyVal=analogRead(A0); Serial.println(keyVal); if(keyVal==1023) { tone(8,notes[0]); } else if (keyVal>=900 && keyVal<=1010) { tone(8,notes[1]); } else if(keyVal>=505 && keyVal<=511) { tone(8,notes[2]); } else if (keyVal>=512 && keyVal<=512) { tone(8,notes[3]); } else { noTone(8); } }

2013年11月24日 星期日

Week12

Part1 Love O Meter

利用TMP電阻感應去增加亮燈數目

const int sensorPin = A0; const float baselineTemp = 26.0; //起始溫度
void setup() { Serial.begin(9600); for (int pinNumber = 2; pinNumber < 5; pinNumber++) { pinMode(pinNumber, OUTPUT); digitalWrite(pinNumber, LOW); } }
void loop() { int sensorVal = analogRead(sensorPin); Serial.print("Sensor Value: "); Serial.print(sensorVal); float voltage = (sensorVal/1024.0) * 5.0; Serial.print(", Volts:"); Serial.print(voltage); Serial.print(", degrees C: "); float temperature = (voltage - .5) * 100; Serial.println(temperature); if (temperature < baselineTemp) { digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); } else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) //增加四度亮第一顆 { digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); } else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) //增加六度亮第二顆 { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); } else if (temperature > baselineTemp + 6) //增加六度亮第三顆 { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); } delay(1); }


Part 2 Color Mixing LAMP

利用光敏電阻感應去改變顏色的亮度和變化


const int Brightness = 20; // set overall brightness, use lower value in darkroom to protect your eyes (0-255) const int greenLEDPin = 9; // LED connected to digital pin 9 const int redLEDPin = 10; // LED connected to digital pin 10 const int blueLEDPin = 11; // LED connected to digital pin 11 const int redSensorPin = A0; // pin with the photoresistor with the red gel const int greenSensorPin = A1; // pin with the photoresistor with the green gel const int blueSensorPin = A2; // pin with the photoresistor with the blue gel int redSensorValue = 0; // variable to hold the value from the red sensor int greenSensorValue = 0; // variable to hold the value from the green sensor int blueSensorValue = 0; // variable to hold the value from the blue sensor int redSensorHigh = 0; //High and low value for mapping. Notice that 'High' value were set low at 0, so that during calibration, its value would be be pushed up. See project 6. int greenSensorHigh = 0; int blueSensorHigh = 0; int redSensorLow = 1023; // the opposite is true for 'Low' value int greenSensorLow = 1023; int blueSensorLow = 1023; void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); // set the digital pins as outputs pinMode(greenLEDPin,OUTPUT); pinMode(redLEDPin,OUTPUT); pinMode(blueLEDPin,OUTPUT); //switching on your lamp before calibration, as it would affect calibration too, especially in dark environment analogWrite(redLEDPin, Brightness); analogWrite(greenLEDPin, Brightness); analogWrite(blueLEDPin, Brightness); //actual calibration which takes 10 seconds. During calibration, LED would shine at constant brightness. After that, it'll start to respond. while (millis() < 10000) { redSensorValue = analogRead(redSensorPin); //read pin if (redSensorValue > redSensorHigh) { //push 'High' value up based on sensor value. At the end of calibration, 'High' value would take the highest 'SensorValue' during the 10 seconds. redSensorHigh = redSensorValue; } if (redSensorValue < redSensorLow) { //opposite is true for 'Low' value redSensorLow = redSensorValue; } delay(5); greenSensorValue = analogRead(greenSensorPin); if (greenSensorValue > greenSensorHigh) { greenSensorHigh = greenSensorValue; } if (greenSensorValue < greenSensorLow) { greenSensorLow = greenSensorValue; } delay(5); blueSensorValue = analogRead(blueSensorPin); if (blueSensorValue > blueSensorHigh) { blueSensorHigh = blueSensorValue; } if (blueSensorValue < blueSensorLow) { blueSensorLow = blueSensorValue; } delay(5); } } void loop() { // Read the sensors first: // read the value from the red-filtered photoresistor: redSensorValue = analogRead(redSensorPin); // give the ADC a moment to settle delay(5); // read the value from the green-filtered photoresistor: greenSensorValue = analogRead(greenSensorPin); // give the ADC a moment to settle delay(5); // read the value from the blue-filtered photoresistor: blueSensorValue = analogRead(blueSensorPin); // print out the values to the serial monitor Serial.print("raw sensor Values \t red: "); Serial.print(redSensorValue); Serial.print("\t green: "); Serial.print(greenSensorValue); Serial.print("\t Blue: "); Serial.println(blueSensorValue); /* In order to use the values from the sensor for the LED, you need to do some math. The ADC provides a 10-bit number, but analogWrite() uses 8 bits. You'll want to divide your sensor readings by 4 to keep them in range of the output. */ /* Changed from a mere division by 4 in the original code to mapping. Lowest value recorded at calibration would be mapped to 0, and highest value would be mapped to brightness set earlier. The constraint function is set to prevent flickering, when value are negative or exceeds 255. */ int redValue = map(redSensorValue,redSensorLow,redSensorHigh,0,Brightness); redValue = constrain(redValue,0,255); //紅色最大值255 int greenValue = map(greenSensorValue,greenSensorLow,greenSensorHigh,0,Brightness); greenValue = constrain(greenValue,0,255); //綠色最大值255 int blueValue = map(blueSensorValue,blueSensorLow,blueSensorHigh,0,Brightness); blueValue = constrain(blueValue,0,255); //藍色最大值255 // print out the mapped values Serial.print("Mapped sensor Values \t red: "); Serial.print(redValue); Serial.print("\t green: "); Serial.print(greenValue); Serial.print("\t Blue: "); Serial.println(blueValue); /* Now that you have a usable value, it's time to PWM the LED. */ analogWrite(redLEDPin, redValue); analogWrite(greenLEDPin, greenValue); analogWrite(blueLEDPin, blueValue); }