黒電話の回転ダイヤル式電話機を再現する.
void setup() {
Serial.begin(9600);
DDRB = B001111; //PB0~PB5
}
int dly = 2;
int del = 400;
void loop() {
if (Serial.available() > 0) {
num(Serial.read());
delay(del / 2);
}
}
void num(int x) {
switch (x) {
case 1:
case 49:
step1(20 + 2, 3);
delay(del);
step2(20 + 2, 4);
break;
case 2:
case 50:
step1(20 + 8, 3);
delay(del);
step2(20 + 8, 4);
break;
case 3:
case 51:
step1(20 + 14, 3);
delay(del);
step2(20 + 14, 4);
break;
case 4:
case 52:
step1(20 + 20, 3);
delay(del);
step2(20 + 20, 4);
break;
case 5:
case 53:
step1(20 + 26, 3);
delay(del);
step2(20 + 26, 4);
break;
case 6:
case 54:
step1(20 + 32, 3);
delay(del);
step2(20 + 32, 4);
break;
case 7:
case 55:
step1(20 + 38, 3);
delay(del);
step2(20 + 38, 4);
break;
case 8:
case 56:
step1(20 + 44, 3);
delay(del);
step2(20 + 44, 4);
break;
case 9:
case 57:
step1(20 + 50, 3);
delay(del);
step2(20 + 50, 4);
break;
case 0:
case 48:
step1(20 + 56, 3);
delay(del);
step2(20 + 56, 4);
break;
}
}
void step2(int x, int spd) {
for (int i = 0; i <= x; ++i) {
PORTB = B0001;
delay(spd);
PORTB = B0010;
delay(spd);
PORTB = B0100;
delay(spd);
PORTB = B1000;
delay(spd);
}
}
void step1(int x, int spd) {
for (int i = 0; i <= x; ++i) {
PORTB = B1000;
delay(spd);
PORTB = B0100;
delay(spd);
PORTB = B0010;
delay(spd);
PORTB = B1001;
delay(spd);
}
}
0 件のコメント:
コメントを投稿