Created
March 24, 2025 13:45
-
-
Save ArduLite/23cd1e4ac9b35afbab0f6fd8c3498b30 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> | |
#include <ArduLiteTime.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 = 10; | |
// 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); | |
} | |
} | |
int main() { | |
led.write(LOW); | |
while (1) { | |
int leftSensorValue = leftSensor.read(); | |
int rightSensorValue = rightSensor.read(); | |
if (!leftSensorValue && !rightSensorValue) { | |
moveForward(speedSharp); | |
} else if (leftSensorValue && rightSensorValue) { | |
stopMotor(); | |
} else { | |
unsigned long startTime = getTimeMs(); | |
if (!leftSensorValue && rightSensorValue) { | |
while (!leftSensor.read() && rightSensor.read()) { | |
if (getTimeMs() - startTime > normalTurnTime) { | |
blinkLED(3, 100); // Kedip LED jika tikungan tajam | |
turnRight(speedSharp); | |
} else { | |
turnRight(speedNormal); | |
} | |
} | |
} | |
if (leftSensorValue && !rightSensorValue) { | |
while (leftSensor.read() && !rightSensor.read()) { | |
if (getTimeMs() - 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