Professional Documents
Culture Documents
#define DR2 8
#define PWMR 6
#define DL1 9
#define DL2 4
#define PWML 5
int button = 2;
int numSensor = 6;
//////////////////////////////////////////////////
LR();LL();LL();LR();
/////////////////////////////////////////////////
run(0,0); delay(1);
void loop() {
// pid();
readanalog();
}
void readanalog()
{
int S1 = analogRead(0);
int S2 = analogRead(1);
int S3 = analogRead(2);
int S4 = analogRead(3);
int S5 = analogRead(4);
int S6 = analogRead(5);
Serial.print("S1="); Serial.print(S1);
Serial.print("||"); Serial.print("S2="); Serial.print(S2);
Serial.print("||"); Serial.print("S3="); Serial.print(S3);
Serial.print("||"); Serial.print("S4="); Serial.print(S4);
Serial.print("||"); Serial.print("S5="); Serial.print(S5);
Serial.print("||"); Serial.print("S6="); Serial.print(S6);
Serial.println(); delay(1);
}
void TN()
{
while (1)
{
if (analogRead(0) < REF01 && analogRead(5) < REF06 )
{
while (1)
{
if (analogRead(0) > REF01 && analogRead(5) > REF06 )
{
break;
}
pid();
}
break;
}
pid();
}
}
void LL()
{
while(1){
if(analogRead(0) < REF01 and analogRead(1)< REF02 and analogRead(2)<REF03)
{delay(100);l();break;}
pid();
}
}
void LR()
{
while(1){
if(analogRead(3) < REF04 and analogRead(4)< REF05 and analogRead(5)<REF06)
{delay(100);r();break;}
pid();
}
}
void T()
{
while(1){
if((analogRead(1)< REF02) and (analogRead(2)<REF03) and ((analogRead(3)< REF04)
or (analogRead(2)< REF03) and (analogRead(3)<REF04) and (analogRead(4)< REF05)))
{run(speed_max,speed_max);delay(200);break;}
pid();
}
}
//void T()
//{ int j = 0;
// while(1)
// {
// if(analogRead(3) < REF04 and analogRead(4)< REF05 and analogRead(5)<REF06)
{ R(); }
// if(analogRead(0) < REF01 and analogRead(1)< REF02 and analogRead(2)<REF03)
{ L(); }
//
// if(analogRead(1)< REF02 and analogRead(2)<REF03 and
// analogRead(3)< REF04 and analogRead(4)<REF05)
{ run(speed_max,speed_max);delay(200); }
//
// if (j >= time)
// {
// /////
// run(0,0); delay(100*100);
// /////
// break;
// }
// j++;
// pid();
// }
//}
void l()
{
while(1){
if(analogRead(0)<REF01){run(-350,350);}
if(analogRead(1)<REF02){run(-350,350);}
if(analogRead(3)<REF04){run(-200,200);break;}
else {run(-350,350);}
}
}
void r()
{ run(0,0);delay(20);
while(1){
if(analogRead(5)<REF06){run(350,-350);}
if(analogRead(4)<REF05){run(350,-350);}
if(analogRead(2)<REF03){run(200,-200);break;}
else {run(350,-350);}
}
}
void sw_button()
{
while (1)
{
buttonState = digitalRead(2);
if (buttonState == LOW) {
break;
}
else {
run(0, 0);
}
}
}
int readline()
{
bool onLine = false;
long avg = 0;
long sum = 0;
for (uint8_t i = 0; i < numSensor ; i++)
{
long value = map(analogRead(sensor_pin[i]), min_sensor_values[i],
max_sensor_values[i], 1000, 0);
if (value > 200) {
onLine = true;
}
if (value > 50)
{
avg += (long)value * (i * 1000);
sum += value;
}
}
if (!onLine)
{
if (_lastPosition < (numSensor - 1) * 1000 / 2)
{
return 0 - 2000;
}
else
{
return ((numSensor - 1) * 1000) + 2000 ;
}
}
_lastPosition = avg / sum;
return _lastPosition;
}
void pid() {