Stap 1: Circuit diagram en code
CODE:
Const int RightMotor = 12;
Const int LeftMotor = 11;
Const int RightSensor = 1;
Const int LeftSensor = 2;
int SensorLeft;
int SensorRight;
int SensorDifference;
VOID Setup {}
pinMode (LeftMotor, uitvoer);
pinMode (RightMotor, uitvoer);
pinMode (LeftSensor, INPUT);
pinMode (RightSensor, INPUT);
digitalWrite (A1, hoge);
digitalWrite (A2, hoge);
Serial.begin(9600);
Serial.println ("\nBeginning-licht op zoek naar gedrag");
}
void loop {}
SensorLeft = 1023 - analogRead(LeftSensor);
delay(1);
SensorRight = 1023 - analogRead(RightSensor);
delay(1);
SensorDifference = abs (SensorLeft - SensorRight);
Als (SensorLeft > 500 & & SensorRight > 500) {}
Als (SensorLeft > SensorRight & & SensorDifference > 75) {}
digitalWrite (RightMotor, hoge);
digitalWrite (LeftMotor, laag);
}
Als (SensorLeft < SensorRight & & SensorDifference > 75) {}
digitalWrite (RightMotor, laag);
digitalWrite (LeftMotor, hoge);
}
Als (SensorDifference < 75) {}
digitalWrite (RightMotor, hoge);
digitalWrite (LeftMotor, hoge);
}
}
Als (SensorLeft < 500 & & SensorRight < 500) {}
Als (SensorLeft > SensorRight & & SensorDifference > 75) {}
digitalWrite (RightMotor, laag);
digitalWrite (LeftMotor, laag);
}
}
}