/toy-vending-machine.ino Secret
Last active
July 23, 2020 15:18
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*自動販売機風のおもちゃ 制御コード | |
1.マグネットを貼ったカードをかざし、リードスイッチがONになる | |
2.ブザーがなる | |
3.Slit disk(ドグ)がフォトインタラプタ内にいたら、回転開始 | |
4.Slit disk(ドグ)の切り欠きがフォトインタラプタを通過し、また元の位置に戻ってきたらモーター停止 | |
*/ | |
#define PBSW 10 // スタートSW | |
#define rotation_sensor 2 // フォトインタラプタ | |
#define BUZZER 3 // ブザー | |
#define MOTOR1 8 | |
#define MOTOR2 9 | |
int sw=1,old_sw=1,s3=0; //SWの値 | |
int start_val = 0; //開始位置の値 | |
int rotation_val = 0; //回転時の値 | |
int rotation_state = 0; //回転状態を記録 | |
int in_rotation_state = 0; //回転途中の状態を記録 | |
int end_state = 0; //回転最終の状態を記録 | |
int sw_state = 0; //sw状態を記録 | |
void setup() { | |
pinMode(PBSW,INPUT); | |
pinMode(MOTOR1, OUTPUT); | |
pinMode(MOTOR2, OUTPUT); | |
pinMode(BUZZER, OUTPUT); | |
pinMode(rotation_sensor, INPUT); | |
} | |
void loop() { | |
sw=digitalRead(PBSW); | |
if(sw==0 && old_sw==1 ) { // SWに変化があったら次の処理へ。 | |
sw_state = 1; | |
in_rotation_state = 0; | |
end_state = 0; | |
digitalWrite(BUZZER, HIGH); // ブザーON | |
delay(500); | |
digitalWrite(BUZZER, LOW); // ブザーOFF | |
delay(10); | |
} | |
while (sw_state == 1){ | |
start_val =digitalRead(rotation_sensor); | |
if(end_state == 0 && start_val ==1) { // 回転0回目且つドグが中にいたら | |
rotation_state = 1; | |
delay(10); | |
} | |
rotation_val =digitalRead(rotation_sensor); | |
if(start_val == 1 && rotation_val ==0) { // 回転開始後、ドグが離れたら | |
in_rotation_state = 1; | |
delay(10); | |
} | |
rotation_val =digitalRead(rotation_sensor); | |
if(in_rotation_state == 1 && rotation_val ==1) { // 一周して戻ってきたら | |
end_state = 1; | |
rotation_state = 0; | |
delay(10); | |
} | |
if (rotation_state == 1) { | |
digitalWrite(MOTOR1, HIGH); // モーター回転 | |
digitalWrite(MOTOR2, LOW); // | |
} else { | |
digitalWrite(MOTOR1, HIGH); // モーター停止 | |
digitalWrite(MOTOR2, HIGH); // モーター停止 | |
delay(500); | |
break; | |
} | |
} | |
sw_state = 0; | |
old_sw=sw; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment