kohacraft Lab
/
kc_shingo2
交差点の信号と、横断歩道の信号を1つのマイコンで再現しました。
main.cpp@0:dff00c5f274e, 2018-04-28 (annotated)
- Committer:
- kohacraft
- Date:
- Sat Apr 28 09:00:24 2018 +0000
- Revision:
- 0:dff00c5f274e
- Child:
- 1:1943a21b781b
ver1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kohacraft | 0:dff00c5f274e | 1 | /* 車両用信号機3つと歩行者用信号機1つを再現するプログラムです */ |
kohacraft | 0:dff00c5f274e | 2 | /* 信号機1と信号機2で交差点の信号を再現し、 */ |
kohacraft | 0:dff00c5f274e | 3 | /* 信号機3と信号機4で横断歩道の信号を再現しています */ |
kohacraft | 0:dff00c5f274e | 4 | /* timingの{}の中を変えることで赤黄青の点灯時間を変更できます */ |
kohacraft | 0:dff00c5f274e | 5 | /* 自由に改編して楽しんでください this program is public domain */ |
kohacraft | 0:dff00c5f274e | 6 | #include "mbed.h" |
kohacraft | 0:dff00c5f274e | 7 | #define BLUE 0 |
kohacraft | 0:dff00c5f274e | 8 | #define YELLOW 1 |
kohacraft | 0:dff00c5f274e | 9 | #define RED 2 |
kohacraft | 0:dff00c5f274e | 10 | |
kohacraft | 0:dff00c5f274e | 11 | DigitalOut singo1Led[] = { |
kohacraft | 0:dff00c5f274e | 12 | DigitalOut(dp1), //信号機1 青LED |
kohacraft | 0:dff00c5f274e | 13 | DigitalOut(dp2), //信号機1 黄LED |
kohacraft | 0:dff00c5f274e | 14 | DigitalOut(dp4) //信号機1 赤LED |
kohacraft | 0:dff00c5f274e | 15 | }; |
kohacraft | 0:dff00c5f274e | 16 | |
kohacraft | 0:dff00c5f274e | 17 | DigitalOut singo2Led[] = { |
kohacraft | 0:dff00c5f274e | 18 | DigitalOut(dp9), //信号機2 青LED |
kohacraft | 0:dff00c5f274e | 19 | DigitalOut(dp10), //信号機2 黄LED |
kohacraft | 0:dff00c5f274e | 20 | DigitalOut(dp11) //信号機2 赤LED |
kohacraft | 0:dff00c5f274e | 21 | }; |
kohacraft | 0:dff00c5f274e | 22 | |
kohacraft | 0:dff00c5f274e | 23 | DigitalOut singo3Led[] = { |
kohacraft | 0:dff00c5f274e | 24 | DigitalOut(dp28), //信号機3 青LED |
kohacraft | 0:dff00c5f274e | 25 | DigitalOut(dp26), //信号機3 黄LED |
kohacraft | 0:dff00c5f274e | 26 | DigitalOut(dp25) //信号機3 赤LED |
kohacraft | 0:dff00c5f274e | 27 | }; |
kohacraft | 0:dff00c5f274e | 28 | |
kohacraft | 0:dff00c5f274e | 29 | DigitalOut singo4Led[] = { |
kohacraft | 0:dff00c5f274e | 30 | DigitalOut(dp18), //信号機4(歩行者用) 青LED |
kohacraft | 0:dff00c5f274e | 31 | DigitalOut(dp17), //ダミー(赤と同じdpを指定する) |
kohacraft | 0:dff00c5f274e | 32 | DigitalOut(dp17) //信号機4(歩行者用) 赤LED |
kohacraft | 0:dff00c5f274e | 33 | }; |
kohacraft | 0:dff00c5f274e | 34 | |
kohacraft | 0:dff00c5f274e | 35 | /* 赤黄青の点灯時間{ 赤の点灯時間 , 青の点灯時間 , 黄色の点灯時間 , 赤の点灯時間 }*/ |
kohacraft | 0:dff00c5f274e | 36 | /* 点灯時間は1(秒)以上を指定する 0は指定できない */ |
kohacraft | 0:dff00c5f274e | 37 | int timing1[] = { 1 , 5 , 2 , 5+2+1+2 }; //信号機1の点灯時間の設定 |
kohacraft | 0:dff00c5f274e | 38 | int timing2[] = { 1+5+2+2 , 5 , 2 , 1 }; //信号機2の点灯時間の設定 |
kohacraft | 0:dff00c5f274e | 39 | int timing3[] = { 1 , 3 , 2 , 4+3+1+2 }; //信号機3の点灯時間の設定 |
kohacraft | 0:dff00c5f274e | 40 | int timing4[] = { 1+3+2+2 , 4 , 3 , 1 }; //信号機4の点灯時間の設定 |
kohacraft | 0:dff00c5f274e | 41 | |
kohacraft | 0:dff00c5f274e | 42 | Ticker timer; //歩行者用信号の点滅のためのタイマー |
kohacraft | 0:dff00c5f274e | 43 | |
kohacraft | 0:dff00c5f274e | 44 | int shingo1mode , shingo2mode , shingo3mode , shingo4mode; //現在の点灯している色(赤:0・青:1・黄:2・赤:3) |
kohacraft | 0:dff00c5f274e | 45 | int shingo1counter , shingo2counter , shingo3counter , shingo4counter; //次の色に変わるまでの残り時間 |
kohacraft | 0:dff00c5f274e | 46 | |
kohacraft | 0:dff00c5f274e | 47 | //使用する関数の宣言 |
kohacraft | 0:dff00c5f274e | 48 | void singo( DigitalOut led[] , int *mode , int *counter , int timeing[] ); |
kohacraft | 0:dff00c5f274e | 49 | void hokosya( DigitalOut led[] , int *mode , int *counter , int timeing[] ); |
kohacraft | 0:dff00c5f274e | 50 | |
kohacraft | 0:dff00c5f274e | 51 | //割り込みによる信号機4の点滅処理 |
kohacraft | 0:dff00c5f274e | 52 | void flip() { |
kohacraft | 0:dff00c5f274e | 53 | singo4Led[BLUE] = !singo4Led[BLUE]; |
kohacraft | 0:dff00c5f274e | 54 | } |
kohacraft | 0:dff00c5f274e | 55 | |
kohacraft | 0:dff00c5f274e | 56 | int main() { |
kohacraft | 0:dff00c5f274e | 57 | |
kohacraft | 0:dff00c5f274e | 58 | //初期化 |
kohacraft | 0:dff00c5f274e | 59 | shingo1mode = 0; shingo2mode = 0; shingo3mode = 0; shingo4mode = 0; |
kohacraft | 0:dff00c5f274e | 60 | shingo1counter = timing1[0]; |
kohacraft | 0:dff00c5f274e | 61 | shingo2counter = timing2[0]; |
kohacraft | 0:dff00c5f274e | 62 | shingo3counter = timing3[0]; |
kohacraft | 0:dff00c5f274e | 63 | shingo4counter = timing4[0]; |
kohacraft | 0:dff00c5f274e | 64 | singo1Led[BLUE] = 0; singo1Led[YELLOW] = 0; singo1Led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 65 | singo2Led[BLUE] = 0; singo2Led[YELLOW] = 0; singo2Led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 66 | singo3Led[BLUE] = 0; singo3Led[YELLOW] = 0; singo3Led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 67 | singo4Led[BLUE] = 0; singo4Led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 68 | |
kohacraft | 0:dff00c5f274e | 69 | //無限ループ |
kohacraft | 0:dff00c5f274e | 70 | while(1) { |
kohacraft | 0:dff00c5f274e | 71 | singo( singo1Led , &shingo1mode , &shingo1counter , timing1 ); //信号機1の処理 |
kohacraft | 0:dff00c5f274e | 72 | singo( singo2Led , &shingo2mode , &shingo2counter , timing2 ); //信号機2の処理 |
kohacraft | 0:dff00c5f274e | 73 | singo( singo3Led , &shingo3mode , &shingo3counter , timing3 ); //信号機3の処理 |
kohacraft | 0:dff00c5f274e | 74 | hokosya( singo4Led , &shingo4mode , &shingo4counter , timing4 ); //信号機4の処理 |
kohacraft | 0:dff00c5f274e | 75 | wait(1); //1秒待つ |
kohacraft | 0:dff00c5f274e | 76 | } |
kohacraft | 0:dff00c5f274e | 77 | } |
kohacraft | 0:dff00c5f274e | 78 | |
kohacraft | 0:dff00c5f274e | 79 | //車両用信号の点灯を制御する |
kohacraft | 0:dff00c5f274e | 80 | void singo( DigitalOut led[] , int *mode , int *counter , int timeing[] ) |
kohacraft | 0:dff00c5f274e | 81 | { |
kohacraft | 0:dff00c5f274e | 82 | |
kohacraft | 0:dff00c5f274e | 83 | //カウンターが0以下になったら点灯するLEDを変更する |
kohacraft | 0:dff00c5f274e | 84 | if( *counter <= 0 ){ |
kohacraft | 0:dff00c5f274e | 85 | |
kohacraft | 0:dff00c5f274e | 86 | //modeを一つ増やす 3を超えたら0に戻す |
kohacraft | 0:dff00c5f274e | 87 | *mode = *mode +1 ; |
kohacraft | 0:dff00c5f274e | 88 | if( *mode > 3 ) |
kohacraft | 0:dff00c5f274e | 89 | *mode = 0; |
kohacraft | 0:dff00c5f274e | 90 | |
kohacraft | 0:dff00c5f274e | 91 | //次の色の点灯時間をカウンターにセット |
kohacraft | 0:dff00c5f274e | 92 | *counter = timeing[*mode]; |
kohacraft | 0:dff00c5f274e | 93 | |
kohacraft | 0:dff00c5f274e | 94 | //点灯するLEDを変更する |
kohacraft | 0:dff00c5f274e | 95 | switch( *mode ) { |
kohacraft | 0:dff00c5f274e | 96 | case 0 : //赤を点灯 |
kohacraft | 0:dff00c5f274e | 97 | led[BLUE] = 0; led[YELLOW] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 98 | break; |
kohacraft | 0:dff00c5f274e | 99 | case 1 : //青を点灯 |
kohacraft | 0:dff00c5f274e | 100 | led[BLUE] = 1; led[YELLOW] = 0; led[RED] = 0; |
kohacraft | 0:dff00c5f274e | 101 | break; |
kohacraft | 0:dff00c5f274e | 102 | case 2 : //黄色を点灯 |
kohacraft | 0:dff00c5f274e | 103 | led[BLUE] = 0; led[YELLOW] = 1; led[RED] = 0; |
kohacraft | 0:dff00c5f274e | 104 | break; |
kohacraft | 0:dff00c5f274e | 105 | case 3 : //赤を点灯 |
kohacraft | 0:dff00c5f274e | 106 | led[BLUE] = 0; led[YELLOW] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 107 | break; |
kohacraft | 0:dff00c5f274e | 108 | default : |
kohacraft | 0:dff00c5f274e | 109 | led[BLUE] = 0; led[YELLOW] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 110 | break; |
kohacraft | 0:dff00c5f274e | 111 | } |
kohacraft | 0:dff00c5f274e | 112 | } |
kohacraft | 0:dff00c5f274e | 113 | *counter = *counter -1; |
kohacraft | 0:dff00c5f274e | 114 | |
kohacraft | 0:dff00c5f274e | 115 | } |
kohacraft | 0:dff00c5f274e | 116 | |
kohacraft | 0:dff00c5f274e | 117 | //歩行者用信号の点灯を制御する |
kohacraft | 0:dff00c5f274e | 118 | void hokosya( DigitalOut led[] , int *mode , int *counter , int timeing[] ) |
kohacraft | 0:dff00c5f274e | 119 | { |
kohacraft | 0:dff00c5f274e | 120 | //カウンターが0以下になったら点灯するLEDを変更する |
kohacraft | 0:dff00c5f274e | 121 | if( *counter <= 0 ){ |
kohacraft | 0:dff00c5f274e | 122 | |
kohacraft | 0:dff00c5f274e | 123 | //modeを一つ増やす 3を超えたら0に戻す |
kohacraft | 0:dff00c5f274e | 124 | *mode = *mode +1 ; |
kohacraft | 0:dff00c5f274e | 125 | if( *mode > 3 ) |
kohacraft | 0:dff00c5f274e | 126 | *mode = 0; |
kohacraft | 0:dff00c5f274e | 127 | |
kohacraft | 0:dff00c5f274e | 128 | //次の色の点灯時間をカウンターにセット |
kohacraft | 0:dff00c5f274e | 129 | *counter = timeing[*mode]; |
kohacraft | 0:dff00c5f274e | 130 | |
kohacraft | 0:dff00c5f274e | 131 | //点灯するLEDを変更する |
kohacraft | 0:dff00c5f274e | 132 | switch( *mode ) { |
kohacraft | 0:dff00c5f274e | 133 | case 0 : //赤を点灯 |
kohacraft | 0:dff00c5f274e | 134 | led[BLUE] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 135 | break; |
kohacraft | 0:dff00c5f274e | 136 | case 1 : //青を点灯 |
kohacraft | 0:dff00c5f274e | 137 | led[BLUE] = 1; led[RED] = 0; |
kohacraft | 0:dff00c5f274e | 138 | break; |
kohacraft | 0:dff00c5f274e | 139 | case 2 : //青を点滅 |
kohacraft | 0:dff00c5f274e | 140 | led[BLUE] = 1; led[RED] = 0; |
kohacraft | 0:dff00c5f274e | 141 | timer.attach(&flip, 0.25); //点滅用タイマー割り込み開始 |
kohacraft | 0:dff00c5f274e | 142 | break; |
kohacraft | 0:dff00c5f274e | 143 | case 3 : //赤を点灯 |
kohacraft | 0:dff00c5f274e | 144 | timer.detach(); //点滅用タイマー割り込み停止 |
kohacraft | 0:dff00c5f274e | 145 | led[BLUE] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 146 | break; |
kohacraft | 0:dff00c5f274e | 147 | default : |
kohacraft | 0:dff00c5f274e | 148 | led[BLUE] = 0; led[RED] = 1; |
kohacraft | 0:dff00c5f274e | 149 | break; |
kohacraft | 0:dff00c5f274e | 150 | } |
kohacraft | 0:dff00c5f274e | 151 | } |
kohacraft | 0:dff00c5f274e | 152 | *counter = *counter -1; |
kohacraft | 0:dff00c5f274e | 153 | |
kohacraft | 0:dff00c5f274e | 154 | } |