2019年11月21日 星期四

Week11

1. 把機器用壞
void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:

}

2. 把機器修好,研究程式碼
https://github.com/CytronTechnologies/MakerUno_Examples/blob/master/MakerUno_DefaultSketch/MakerUno_DefaultSketch.ino?fbclid=IwAR046tP4x1ngsRoEyxaqMhvO_GhKrX25ULcrOdUb6nNWusmBLWxRMQy1wNs

3. 按按鈕,燈全暗。不按鈕,燈全亮
void setup() {
  // put your setup code here, to run once:
  pinMode(2,INPUT_PULLUP); //把Button(2)打開,使用INPUT_PULLUP 預設要拉高!HIGH

  for(int i=3; i<=13; i++){  //小心!2是button,所以OUTPUT從3開始
    pinMode(i,OUTPUT);  //只要做一次,把LED(3...13)開關打開
    //digitalWrite(i,OUTPUT); //要做很多次
  }
}

bool bLightHIGH=true;  //自己發明的變數 true亮,false不高
void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(2)==HIGH) bLightHIGH=true;  //2是button,高高的HIGH
  else bLightHIGH=false;  //按下去,變LOW

  for(int i=3; i<=13; i++){
    if(bLightHIGH)digitalWrite(i,HIGH);  //要做很多次
    else digitalWrite(i, LOW);  //要做很多次
  }
}

4. 結合Processing,按數字鍵發出Do Re Mi
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523


void setup() {
  pinMode(8,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()){
    char now = Serial.read();
    if(now=='1') tone(8,NOTE_C4,300);
    if(now=='2') tone(8,NOTE_D4,300);
    if(now=='3') tone(8,NOTE_E4,300);
    if(now=='4') tone(8,NOTE_F4,300);
    if(now=='5') tone(8,NOTE_G4,300);
    if(now=='6') tone(8,NOTE_A4,300);
    if(now=='7') tone(8,NOTE_B4,300);
    if(now=='8') tone(8,NOTE_C5,300);
  }
}

[Processing]
import processing.serial.*;
Serial myPort;
void setup(){
  myPort = new Serial(this, "COM3",9600);
}
void draw(){
}
void keyPressed(){
  if(key=='1') myPort.write('1');
  if(key=='2') myPort.write('2');
  if(key=='3') myPort.write('3');
  if(key=='4') myPort.write('4');
  if(key=='5') myPort.write('5');
  if(key=='6') myPort.write('6');
  if(key=='7') myPort.write('7');
  if(key=='8') myPort.write('8');

}

沒有留言:

張貼留言

alanhc 互動技術-week17 [final]

回顧這學期的作品:  期中作業:LANDING:PLANET 賣點&特點: 炫麗的特效 物理(星球重力及降落)及粒子系統(噴射) 世界地圖可根據視角縮放 困難點: 重寫3次最終改寫成物件導向的CLASS寫法...