2013年12月1日 星期日

第十三週作業

Project 5
void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); //程式一開始的五秒內是校正用的,這時候要趕快遮住/打開光敏電阻,讓程式修正最大和最小值 while (millis() < 5000) { sensorValue = analogRead(A0); if (sensorValue > sensorHigh) { sensorHigh = sensorValue; } if (sensorValue < sensorLow) { sensorLow = sensorValue; } } //等到燈熄滅,就開始進入 loop digitalWrite(ledPin, LOW); } void loop() { sensorValue = analogRead(A0); //這裡也使用了 map 函示來對應最高和最低的數值,對應為 50~4000 hz int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000); tone(8, pitch, 20); delay(10); } #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); myServo.write(angle); delay(15); }
Project 6
int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  //程式一開始的五秒內是校正用的,這時候要趕快遮住/打開光敏電阻,讓程式修正最大和最小值
  while (millis() < 5000) {
    sensorValue = analogRead(A0);
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    if (sensorValue < sensorLow) {
      sensorLow = sensorValue;
    }
  }
  //等到燈熄滅,就開始進入 loop
  digitalWrite(ledPin, LOW);
}

void loop() {
  sensorValue = analogRead(A0);
  //這裡也使用了 map 函示來對應最高和最低的數值,對應為 50~4000 hz
  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
  tone(8, pitch, 20);
  delay(10);
}
Project 7
int buttons[6];
int notes[]={262,294,330,349};
void setup(){
buttons[0]=2;
 Serial.begin(9600); 
}
void loop(){
 int keyVal=analogRead(A0);
 Serial.println(keyVal);
if(keyVal==1023){
tone(8,notes[0]);
}
else if(keyVal >= 990 && keyVal <= 1010){
tone(8,notes[1]);
}
else if(keyVal >= 505 && keyVal <= 515){
tone(8,notes[2]);
}
else if(keyVal >= 950 && keyVal <= 980){
tone(8,notes[3]);
}
else{
noTone(8);
}
}

沒有留言:

張貼留言