I'm using Arduino nano
RT and LT stands for left/right line tracker
I am tasked to go forward first, capture the black line, turn left, and start tracing the line.
const byte lMotorA = 5;
const byte lMotorB = 9;
const byte rMotorA = 6;
const byte rMotorB = 10;
const byte LT = 16;
const byte RT = 17;
void setup() {
pinMode(lMotorA, OUTPUT);
pinMode(lMotorB, OUTPUT);
pinMode(rMotorA, OUTPUT);
pinMode(rMotorB, OUTPUT);
pinMode(RT, INPUT);
pinMode(LT, INPUT);
}
void loop() {
return digitalRead(LT);
return digitalRead(RT);
forward();
if (RT== 1 && LT == 1) {
turnleft();
}
stop();
delay(500);
if (RT == 1 && LT == 0) {
right();
delay(1);
}
else if (LT == 1 && RT == 0) {
left();
delay(1);
}
else if (RT == 0 && LT == 0) {
stop();
delay(500);
}
else {
forward();
delay(10);
}
}
void forward() {
analogWrite(lMotorA, 168);
analogWrite(lMotorB, 0);
analogWrite(rMotorA, 168);
analogWrite(rMotorB, 0);
}
void stop() {
analogWrite(lMotorA, 0);
analogWrite(lMotorB, 0);
analogWrite(rMotorA, 0);
analogWrite(rMotorB, 0);
}
void right() {
analogWrite(lMotorA, 0);
analogWrite(lMotorB, 0);
analogWrite(rMotorA, 168);
analogWrite(rMotorB, 0);
}
void left() {
analogWrite(lMotorA, 168);
analogWrite(lMotorB, 0);
analogWrite(rMotorA, 0);
analogWrite(rMotorB, 0);
}
void turnleft() {
analogWrite(lMotorA, 0);
analogWrite(lMotorB, 168);
analogWrite(rMotorA, 168);
analogWrite(rMotorB, 0);
}
gcjr
January 6, 2025, 11:13am
2
look this over
you mixed up the state of the pin and the pin # (LT, RT)
you return from the function instead of reading the pin
it also helps to
Capitalize Constants (only Constants)
name pins with "Pin" in their name
// line tracker
const byte PinMotLA = 5;
const byte PinMotLB = 9;
const byte PinMotRA = 6;
const byte PinMotRB = 10;
const byte PinLt = 16;
const byte PinRt = 17;
const byte MotPwm = 168;
// -----------------------------------------------------------------------------
void setup ()
{
pinMode (PinMotLA, OUTPUT);
pinMode (PinMotLB, OUTPUT);
pinMode (PinMotRA, OUTPUT);
pinMode (PinMotRB, OUTPUT);
pinMode (PinRt, INPUT);
pinMode (PinLt, INPUT);
forward (); // start motor moving forward
}
// -----------------------------------------------------------------------------
void loop ()
{
// read sensors
byte lt = digitalRead (PinLt);
byte rt = digitalRead (PinRt);
// adjust path
if (rt == 1 && lt == 0)
right ();
else if (lt == 1 && rt == 0)
left ();
else if (rt == 0 && lt == 0)
stop ();
else
forward ();
delay (100);
}
// -----------------------------------------------------------------------------
void forward () {
analogWrite (PinMotLA, MotPwm);
analogWrite (PinMotLB, 0);
analogWrite (PinMotRA, MotPwm);
analogWrite (PinMotRB, 0);
}
void stop () {
analogWrite (PinMotLA, 0);
analogWrite (PinMotLB, 0);
analogWrite (PinMotRA, 0);
analogWrite (PinMotRB, 0);
}
void right () {
analogWrite (PinMotLA, 0);
analogWrite (PinMotLB, 0);
analogWrite (PinMotRA, MotPwm);
analogWrite (PinMotRB, 0);
}
void left () {
analogWrite (PinMotLA, MotPwm);
analogWrite (PinMotLB, 0);
analogWrite (PinMotRA, 0);
analogWrite (PinMotRB, 0);
}
void turnleft () {
analogWrite (PinMotLA, 0);
analogWrite (PinMotLB, MotPwm);
analogWrite (PinMotRA, MotPwm);
analogWrite (PinMotRB, 0);
}
Welcome to the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination
https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum
Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
thank you so much for help
Thanks. I'll take note of that
I can see that you tried the code tags. It's not three ticks (''') but three back ticks (```); and they must be on their own line.
I've fixed it for you.
system
Closed
July 5, 2025, 11:47pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.