Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pin definiton for motor driver
- const int motorLeftForward = 3;
- const int motorLeftBackward = 5;
- const int motorRightForward = 6;
- const int motorRightBackward = 9;
- // -------------------------------------------
- // kode program sederhana robot line follower
- // www.ardukode.blogspot.com
- // -------------------------------------------
- // Pin definition for line sensor
- const int sensorLeft = A0;
- const int sensorRight = A1;
- void setup() {
- // Set motor driver pins as output
- pinMode(motorLeftForward, OUTPUT);
- pinMode(motorLeftBackward, OUTPUT);
- pinMode(motorRightForward, OUTPUT);
- pinMode(motorRightBackward, OUTPUT);
- // Set sensor pins as input
- pinMode(sensorLeft, INPUT);
- pinMode(sensorRight, INPUT);
- // Start serial communication for debugging
- Serial.begin(9600);
- }
- void loop() {
- // Read sensor values
- int leftSensorValue = analogRead(sensorLeft);
- int rightSensorValue = analogRead(sensorRight);
- // Debugging: print sensor values
- Serial.print("Left Sensor: ");
- Serial.print(leftSensorValue);
- Serial.print(" - Right Sensor: ");
- Serial.println(rightSensorValue);
- // Line following logic
- if (leftSensorValue > 500 && rightSensorValue < 500) {
- // Turn right
- moveRight();
- } else if (rightSensorValue > 500 && leftSensorValue < 500) {
- // Turn left
- moveLeft();
- } else if (leftSensorValue < 500 && rightSensorValue < 500) {
- // Move forward
- moveForward();
- } else {
- // Stop
- stopMoving();
- }
- }
- void moveForward() {
- digitalWrite(motorLeftForward, HIGH);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, HIGH);
- digitalWrite(motorRightBackward, LOW);
- }
- void moveLeft() {
- digitalWrite(motorLeftForward, LOW);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, HIGH);
- digitalWrite(motorRightBackward, LOW);
- }
- void moveRight() {
- digitalWrite(motorLeftForward, HIGH);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, LOW);
- digitalWrite(motorRightBackward, LOW);
- }
- void stopMoving() {
- digitalWrite(motorLeftForward, LOW);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, LOW);
- digitalWrite(motorRightBackward, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement