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:
}
Part2 發亮13個燈
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
for (int i = 3; i <= 13; i++)
{
pinMode(i, OUTPUT);
}
}
bool bLightHIGH = true;
void loop() {
// put your main code here, to run repeatedly:
if (dightalRead(2) == HIGH)bLightHIGH = true;
else bLightHIGh = false;
for (int i = 3; i <= 13; i++)
{
if (bLightHIGH)digitalWrite(i, HIGH);
else digitalWrite(i, LOW);
}
}
Part3 小蜜蜂
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
tone(8, NOTE_G5, 230);
delay(230);
tone(8, NOTE_E5, 230);
delay(230);
tone(8, NOTE_E5, 230);
delay(230);
tone(8, NOTE_F5, 230);
delay(230);
tone(8, NOTE_D5, 230);
delay(230);
tone(8, NOTE_D5, 230);
delay(230);
delay(1300);
}
Part4 由Processing按鍵發聲
Porcessing:
import processing.serial.*;
Serial myPort;
void setup() {
myPort = new Serial(this, "COM4", 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');
}
Arduino:
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
char now = Serial.read();
if (now == '1') tone(8, NOTE_C5, 300);
if (now == '2') tone(8, NOTE_D5, 300);
if (now == '3') tone(8, NOTE_E5, 300);
if (now == '4') tone(8, NOTE_F5, 300);
if (now == '5') tone(8, NOTE_G5, 300);
delay(1300);
}
}
沒有留言:
張貼留言