diff --git a/arduino/hanzo2_1/hanzo2_1.ino b/arduino/hanzo2_1/hanzo2_1.ino index 4bbed9c..5635f1c 100644 --- a/arduino/hanzo2_1/hanzo2_1.ino +++ b/arduino/hanzo2_1/hanzo2_1.ino @@ -1,26 +1,23 @@ /* 競技用ランサーロボット 半蔵 2.1 -(C)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt] -*/ - -/* - <前> [7 654321 0] - | - | -[]---*----[] - | / - |/ -[]---#----[] + | | + | | +[]---{}---[] + / .\ + / | \ +[]+--{}--+[] + +(c)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt] */ #include #include "Motor.h" //#define LINE_BLACK //白地・黒ライン有効 -#define PIN_SENSOR ( ((PINC<<2) & ~0x03) | ((PINB>>4) & 0x03) ) +//#define PIN_SENSOR ( ((PINC<<2) & ~0x03) | ((PINB>>4) & 0x03) ) const unsigned int TARGET_NONE_COUNT = 85; const unsigned long LAP_COUNT = 854; @@ -32,15 +29,15 @@ const int LANCE_ANGLE3 = -33; const int LANCE_ANGLE4 = 34; const unsigned int LANCE_INTERVAL = 70; //ms -const byte SPEED_DEFAULT = 0xff; +const byte SPEED_DEFAULT = 255; const int HANDLE_DEGREE_DEFAULT = 90; //度 -const unsigned int HANDLE_MIN = ( 800+30); //ms -const unsigned int HANDLE_MAX = (2300+30); //ms +const unsigned int HANDLE_MIN = ( 800+30); //us +const unsigned int HANDLE_MAX = (2300+30); //us const int LANCE_DEGREE_DEFAULT = 90; //度 -const unsigned int LANCE_MIN = 544; //ms -const unsigned int LANCE_MAX = 2400; //ms +const unsigned int LANCE_MIN = 544; //us +const unsigned int LANCE_MAX = 2400; //us const byte PIN_LED = 4; const byte PIN_ROT = 2; @@ -156,7 +153,9 @@ void lance(int degree){ servoLance.write(LANCE_DEGREE_DEFAULT - degree); } -/* ロータリーエンコーダーの変化を見る */ +/* ロータリーエンコーダーの変化を見る + todo:外部割り込み使ってみたい、、、 +*/ volatile unsigned long counter = 0; void count(){ static byte rot[2] = {0}; //rot[0]:今の状態, rot[1]:前回の状態 @@ -178,6 +177,19 @@ void sensorInit(){ pinMode(PIN_SENSOR_7, INPUT); } +byte sensorRead(){ + byte sensor = 0; + if(digitalRead(PIN_SENSOR_0)) sensor += 0b00000001; + if(digitalRead(PIN_SENSOR_1)) sensor += 0b00000010; + if(digitalRead(PIN_SENSOR_2)) sensor += 0b00000100; + if(digitalRead(PIN_SENSOR_3)) sensor += 0b00001000; + if(digitalRead(PIN_SENSOR_4)) sensor += 0b00010000; + if(digitalRead(PIN_SENSOR_5)) sensor += 0b00100000; + if(digitalRead(PIN_SENSOR_6)) sensor += 0b01000000; + if(digitalRead(PIN_SENSOR_7)) sensor += 0b10000000; + return sensor; +} + void setup() { //サーボ servoHandle.attach(PIN_SERVO_HANDLE, HANDLE_MIN, HANDLE_MAX); @@ -210,9 +222,11 @@ void loop(){ //センサーを見る byte sensor = 0; #ifdef LINE_BLACK //黒ライン - sensor = PIN_SENSOR; +// sensor = PIN_SENSOR; + sensor = sensorRead(); #else - sensor = ~PIN_SENSOR; +// sensor = ~PIN_SENSOR; + sensor = ~sensorRead(); #endif //ロータリーエンコーダーの変化を見る count();