Created
March 24, 2025 00:05
-
-
Save ArduLite/bca6289af88ade5b24a249cdf092ffd3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ArduLite.h> | |
| // Sensors | |
| Digital leftSensor(2, IN); | |
| Digital rightSensor(3, IN); | |
| // DC Motors | |
| PWM leftMotor1(5); | |
| PWM leftMotor2(6); | |
| PWM rightMotor1(9); | |
| PWM rightMotor2(10); | |
| // LED Indicator | |
| Digital led(13, OUT); | |
| // Waktu belok berdasarkan jenis tikungan | |
| const int normalTurnTime = 100; // Tikungan biasa (ms) | |
| const int sharpTurnTime = 50; // Tikungan tajam lebih lama (ms) | |
| // Kecepatan motor | |
| const int speedNormal = 200; | |
| const int speedSharp = 255; | |
| void moveForward(int speedValue) { | |
| leftMotor1.write(speedValue); | |
| leftMotor2.write(0); | |
| rightMotor1.write(speedValue); | |
| rightMotor2.write(0); | |
| } | |
| void stopMotor() { | |
| leftMotor1.write(0); | |
| leftMotor2.write(0); | |
| rightMotor1.write(0); | |
| rightMotor2.write(0); | |
| } | |
| void turnLeft(int speedValue) { | |
| leftMotor1.write(0); // Pastikan motor kiri berhenti | |
| leftMotor2.write(speedValue); | |
| rightMotor1.write(speedValue); | |
| rightMotor2.write(0); | |
| } | |
| void turnRight(int speedValue) { | |
| leftMotor1.write(speedValue); | |
| leftMotor2.write(0); | |
| rightMotor1.write(0); // Pastikan motor kanan berhenti | |
| rightMotor2.write(speedValue); | |
| } | |
| void blinkLED(int times, int duration) { | |
| for (int i = 0; i < times; i++) { | |
| led.write(HIGH); | |
| wait(duration); | |
| led.write(LOW); | |
| wait(duration); | |
| } | |
| } | |
| void setup() { | |
| led.write(LOW); | |
| } | |
| void loop() { | |
| int leftSensorValue = leftSensor.read(); | |
| int rightSensorValue = rightSensor.read(); | |
| if (!leftSensorValue && !rightSensorValue) { | |
| moveForward(speedSharp); | |
| } else if (leftSensorValue && rightSensorValue) { | |
| stopMotor(); | |
| } else { | |
| unsigned long startTime = millis(); | |
| if (!leftSensorValue && rightSensorValue) { | |
| while (!leftSensor.read() && rightSensor.read()) { | |
| if (millis() - startTime > normalTurnTime) { | |
| blinkLED(3, 100); // Kedip LED jika tikungan tajam | |
| turnRight(speedSharp); | |
| } else { | |
| turnRight(speedNormal); | |
| } | |
| } | |
| } | |
| if (leftSensorValue && !rightSensorValue) { | |
| while (leftSensor.read() && !rightSensor.read()) { | |
| if (millis() - startTime > normalTurnTime) { | |
| blinkLED(3, 100); | |
| turnLeft(speedSharp); | |
| } else { | |
| turnLeft(speedNormal); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment