digitalRead()を使ってセンサーを見るようにした

This commit is contained in:
Nebel 2012-09-14 14:39:10 +09:00
parent 784b38eeb2
commit 817a2ecad3

View file

@ -1,26 +1,23 @@
/* /*
2.1 2.1
(C)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt]
*/
/*
<>
[7 654321 0] [7 654321 0]
| | |
| | |
[]---*----[] []---{}---[]
| / / .\
|/ / | \
[]---#----[] []+--{}--+[]
(c)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt]
*/ */
#include <Servo.h> #include <Servo.h>
#include "Motor.h" #include "Motor.h"
//#define LINE_BLACK //白地・黒ライン有効 //#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 int TARGET_NONE_COUNT = 85;
const unsigned long LAP_COUNT = 854; const unsigned long LAP_COUNT = 854;
@ -32,15 +29,15 @@ const int LANCE_ANGLE3 = -33;
const int LANCE_ANGLE4 = 34; const int LANCE_ANGLE4 = 34;
const unsigned int LANCE_INTERVAL = 70; //ms const unsigned int LANCE_INTERVAL = 70; //ms
const byte SPEED_DEFAULT = 0xff; const byte SPEED_DEFAULT = 255;
const int HANDLE_DEGREE_DEFAULT = 90; //度 const int HANDLE_DEGREE_DEFAULT = 90; //度
const unsigned int HANDLE_MIN = ( 800+30); //ms const unsigned int HANDLE_MIN = ( 800+30); //us
const unsigned int HANDLE_MAX = (2300+30); //ms const unsigned int HANDLE_MAX = (2300+30); //us
const int LANCE_DEGREE_DEFAULT = 90; //度 const int LANCE_DEGREE_DEFAULT = 90; //度
const unsigned int LANCE_MIN = 544; //ms const unsigned int LANCE_MIN = 544; //us
const unsigned int LANCE_MAX = 2400; //ms const unsigned int LANCE_MAX = 2400; //us
const byte PIN_LED = 4; const byte PIN_LED = 4;
const byte PIN_ROT = 2; const byte PIN_ROT = 2;
@ -156,7 +153,9 @@ void lance(int degree){
servoLance.write(LANCE_DEGREE_DEFAULT - degree); servoLance.write(LANCE_DEGREE_DEFAULT - degree);
} }
/* ロータリーエンコーダーの変化を見る */ /* ロータリーエンコーダーの変化を見る
todo:使
*/
volatile unsigned long counter = 0; volatile unsigned long counter = 0;
void count(){ void count(){
static byte rot[2] = {0}; //rot[0]:今の状態, rot[1]:前回の状態 static byte rot[2] = {0}; //rot[0]:今の状態, rot[1]:前回の状態
@ -178,6 +177,19 @@ void sensorInit(){
pinMode(PIN_SENSOR_7, INPUT); 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() { void setup() {
//サーボ //サーボ
servoHandle.attach(PIN_SERVO_HANDLE, HANDLE_MIN, HANDLE_MAX); servoHandle.attach(PIN_SERVO_HANDLE, HANDLE_MIN, HANDLE_MAX);
@ -210,9 +222,11 @@ void loop(){
//センサーを見る //センサーを見る
byte sensor = 0; byte sensor = 0;
#ifdef LINE_BLACK //黒ライン #ifdef LINE_BLACK //黒ライン
sensor = PIN_SENSOR; // sensor = PIN_SENSOR;
sensor = sensorRead();
#else #else
sensor = ~PIN_SENSOR; // sensor = ~PIN_SENSOR;
sensor = ~sensorRead();
#endif #endif
//ロータリーエンコーダーの変化を見る //ロータリーエンコーダーの変化を見る
count(); count();