概要
GR-KURUMIマイコンボードを使て、くるみラップタイマーを制作しました。
これは、マイコンカーレース等でマイコンカーのラップライムを計測するために使用します。赤外線距離センサーを使って、マイコンカーがラップタイマーの前を通過すると赤外線距離センサーの計測値が変化するので、それを利用してラップライムのオン・オフを制御します。くるみラップタイマーの前をマイコンカーが通過した時点からラップタイムがスタートし、またマイコンカーが返って来てセンサーの前を横切りるとタイマーがストップします。ラップライムクリアスイッチでリセットでき、カウントスタートの状態に戻ります。
赤外線距離センサーには、写真のような箱のような反射壁が必要で40cmほど離して置いてください。
プログラムは外部割込みやタイマー割込みは使用せず、millis( )タイマーでLEDをダイナミック点灯しています。赤外線距離センサーは,loop() 内で計測していますが、毎回1ms以内に計測しています。ソフトウェアの変更なしで、Arduino-ProMini等への移植も可能で確認済みです。
部品
赤外線センサー: SHARP GP2Y0A21YK8セグメントLEDモジュール: WSN233 (www.wsnak.com)
プッシュスイッチ: A0ピン(アナログ値で読む)
ソースコード
// Applicable Segment LED Module: WSN233 (www.wsnak.com)// LED Pin <--> KURUMI Pin
#define COM1 11
#define COM2 12
#define COM3 13
#define COM4 5
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 6
#define SEG_E 7
#define SEG_F 8
#define SEG_G 9
#define DOT A3
// Applicable Infra-red Distance Sensor: Sharp GP2Y0A21YK
#define IRD A1 // Infeared Distance Sensor Port
// Super Sonic Distance Sensor
#define Echo 10
#define Trg A2
// Applicable Tact-switch: SFE-COM-00097 (Switch-Science)
#define PUSH_SW A0 // Push Switch Port. Read the switch state by analog. Push < 10
#define PUSH_SW1 A1
#define CALIB 0 // Calibration Mode
#define WAIT_CAR 1
#define GO 2 // Car cross the sensor and wrap-timer started
#define FINISH 3 // Car cross the sensor and wrap-timer stopped
#define AGAIN 4 // Switch to GO mode
unsigned long Blinking = 0;
unsigned long Interval = 0;
int Seg1 = 17, Seg2 = 17, Seg3=17, Seg4 = 17; // Display Value for Segment LEDs
int SegPoint = 1;
unsigned long Sec1 = 0;
int Mode; // Flow Control with SW and IRD
int IrdLevel = 0x100;
int CarIsHere = 0; // 1 -- Car is Here
void setup( ) {
Serial.begin(9600);
Init_Led_Seg();
Mode = CALIB; // Set Caribration mode
}
void loop() { // Do not use delay() in loop()
LedDrive(); // Seg-LED driver.
RunMode();
}
void RunMode(){
switch (Mode) {
case CALIB:
CalibrationMode();
break;
case WAIT_CAR:
WaitCar();
break;
case GO:
GoMode();
break;
case FINISH:
FinishMode();
break;
default:
break;
}
}
void CalibrationMode(){
Seg1 = 17;
Seg2 = 17;
Seg3 = 17;
Seg4 = 17;
if (analogRead(PUSH_SW) > 10) {
if((millis() - Interval > 100) ) {
Interval = millis();
Serial.println(analogRead(IRD));
}
Mode = CALIB;
} else {
Seg1 = 0;
Seg2 = 0;
Seg3 = 0;
Seg4 = 0;
Mode = WAIT_CAR;
}
}
void WaitCar(){
if (analogRead(IRD) > 200) {
CarIsHere = 1;
}
if (analogRead(IRD) < 200 && CarIsHere == 1){
CarIsHere = 0;
Mode = GO;
}
}
void GoMode() {
if (analogRead(IRD)> 200){
Mode = FINISH;
} else {
if((millis() - Interval > 10) ) {
Interval = millis();
if (++Seg1 > 9){
Seg1 = 0;
Seg2++;
}
if (Seg2 > 9){
Seg2 = 0;
Seg3++;
}
if(Seg3 > 9) {
Seg3 = 0;
Seg4++;
}
if (Seg4 > 6){
Seg4 = 0;
}
}
}
}
void FinishMode(){
if (analogRead(PUSH_SW) < 10) {
Mode = CALIB;
}
}
void LedDrive(){
if(micros() - Blinking > 1000) {
Blinking = micros();
switch (SegPoint++){
case 1:
Led(1, Seg1);
break;
case 2:
Led(2, Seg2);
break;
case 3:
Led(3, Seg3);
break;
case 4:
Led(4, Seg4);
break;
case 5:
Led(4, Seg4);
break;
default:
SegPoint = 1;
}
}
}
void Led(int com, int n){ // 'n' must be 0x0 to 0x10 (OFF)
digitalWrite(COM1, LOW); // 'com' must be 1 to 3, and other number is OFF
digitalWrite(COM2, LOW);
digitalWrite(COM3, LOW);
digitalWrite(COM3, LOW);
switch(n){
case 0:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
case 1:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
case 2:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 3:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 4:
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 5:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 6:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 7:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
case 8:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 9:
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 10: // Charracter A
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
case 11: // Character B
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 12: // Character C
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
case 13: // Character d
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, HIGH);
digitalWrite(SEG_C, HIGH);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 14: // Character E
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, HIGH);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 15: // Character F
digitalWrite(SEG_A, HIGH);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, HIGH);
digitalWrite(SEG_F, HIGH);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
case 16: // Chareacter .
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, HIGH);
break;
case 17: // '-' Minus
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, HIGH);
digitalWrite(DOT, LOW);
break;
default: // OFF
digitalWrite(SEG_A, LOW);
digitalWrite(SEG_B, LOW);
digitalWrite(SEG_C, LOW);
digitalWrite(SEG_D, LOW);
digitalWrite(SEG_E, LOW);
digitalWrite(SEG_F, LOW);
digitalWrite(SEG_G, LOW);
digitalWrite(DOT, LOW);
break;
}
switch(com){
case 1:
digitalWrite(COM1, HIGH);
digitalWrite(COM2, LOW);
digitalWrite(COM3, LOW);
digitalWrite(COM4, LOW);
break;
case 2:
digitalWrite(COM1, LOW);
digitalWrite(COM2, HIGH);
digitalWrite(COM3, LOW);
digitalWrite(COM4, LOW);
break;
case 3:
digitalWrite(COM1, LOW);
digitalWrite(COM2, LOW);
digitalWrite(COM3, HIGH);
digitalWrite(COM4, LOW);
break;
case 4:
digitalWrite(COM1, LOW);
digitalWrite(COM2, LOW);
digitalWrite(COM3, LOW);
digitalWrite(COM4, HIGH);
break;
default:
digitalWrite(COM1, LOW);
digitalWrite(COM2, LOW);
digitalWrite(COM3, LOW);
digitalWrite(COM4, LOW);
break;
}
}
void Init_Led_Seg(){
pinMode(COM1, OUTPUT);
digitalWrite(COM1, LOW);
pinMode(COM2, OUTPUT);
digitalWrite(COM2, LOW);
pinMode(COM3, OUTPUT);
digitalWrite(COM3, LOW);
pinMode(COM3, OUTPUT);
digitalWrite(COM3, LOW);
pinMode(SEG_A, OUTPUT);
digitalWrite(SEG_A, LOW);
pinMode(SEG_B, OUTPUT);
digitalWrite(SEG_B, LOW);
pinMode(SEG_C, OUTPUT);
digitalWrite(SEG_C, LOW);
pinMode(SEG_D, OUTPUT);
digitalWrite(SEG_D, LOW);
pinMode(SEG_E, OUTPUT);
digitalWrite(SEG_E, LOW);
pinMode(SEG_F, OUTPUT);
digitalWrite(SEG_F, LOW);
pinMode(SEG_G, OUTPUT);
digitalWrite(SEG_G, LOW);
pinMode(DOT, OUTPUT);
digitalWrite(DOT, LOW);
}
0 件のコメント:
コメントを投稿