続き

・エッジ検出によるライン位置推定
・CPU間用パラレル通信

#define LED 13
#define AO A0
#define SI 11
#define CLK 12

#define BIT_7 9
#define BIT_6 8
#define BIT_5 7
#define BIT_4 6
#define BIT_3 5
#define BIT_2 4
#define BIT_1 3
#define BIT_0 2

//エッジ検出閾値
#define MIN_EDGE 3
unsigned char data[32];

void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);

digitalWrite(SI, LOW);
pinMode(SI, OUTPUT);
digitalWrite(CLK, LOW);
pinMode(CLK, OUTPUT);

ADCSRA = ADCSRA & 0xfd;//Division Factor 128->32

pinMode(BIT_7, OUTPUT);
pinMode(BIT_6, OUTPUT);
pinMode(BIT_5, OUTPUT);
pinMode(BIT_4, OUTPUT);
pinMode(BIT_3, OUTPUT);
pinMode(BIT_2, OUTPUT);
pinMode(BIT_1, OUTPUT);
pinMode(BIT_0, OUTPUT);

Serial.begin(9600);
while (!Serial);
}

void loop() {
// put your main code here, to run repeatedly:
signed char min,max,dif;
unsigned char maxad,minad,out;

delay(500);

while(1)
{
digitalWrite(BIT_7, LOW);
digitalWrite(SI, HIGH);
digitalWrite(CLK, HIGH);
digitalWrite(SI, LOW);
delayMicroseconds(10);
digitalWrite(CLK, LOW);
for (int j = 0; j <32; j++){
 data[j]=analogRead(A0)/4;
 digitalWrite(CLK, HIGH);
 digitalWrite(CLK, LOW);
 digitalWrite(CLK, HIGH);
 digitalWrite(CLK, LOW);
 digitalWrite(CLK, HIGH);
 digitalWrite(CLK, LOW);
 digitalWrite(CLK, HIGH);
 digitalWrite(CLK, LOW);
}

min=-MIN_EDGE;
max= MIN_EDGE;
minad=0;
maxad=0;
for(char j=0;j<31;j++){
 dif=data[j]-data[j+1];
 if(j<30 && dif<min){min=dif;minad=j;} //黒→白エッジ検出
 if(j>1  && dif>max){max=dif;maxad=j;} //白→黒エッジ検出
}
if(min==-MIN_EDGE || max==MIN_EDGE ||(minad>maxad)){
 out=63;
 //63:検出エラー
}else{
 out=(minad+maxad);
 //out=31位が中心に白線がある状態 15~45位の範囲で横ズレ量を示す
}
Serial.println (out);
if(out>=32){digitalWrite(BIT_5, HIGH);out=out-32;}else{digitalWrite(BIT_5, LOW);}
if(out>=16){digitalWrite(BIT_4, HIGH);out=out-16;}else{digitalWrite(BIT_4, LOW);}
if(out>= 8){digitalWrite(BIT_3, HIGH);out=out- 8;}else{digitalWrite(BIT_3, LOW);}
if(out>= 4){digitalWrite(BIT_2, HIGH);out=out- 4;}else{digitalWrite(BIT_2, LOW);}
if(out>= 2){digitalWrite(BIT_1, HIGH);out=out- 2;}else{digitalWrite(BIT_1, LOW);}
if(out>= 1){digitalWrite(BIT_0, HIGH); }else{digitalWrite(BIT_0, LOW);}
digitalWrite(BIT_7, HIGH);

delay(10);//露光時間
}
}
カテゴリー: RMCR 2017 パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です