Hi, I am an international student without English language being my mother tongue, so I am looking forward to understand each other. I had experience with arduino and basic coding few years ago but currently I feel like a complete noobie.
I am trying to create the famous line tracking robot (2 wheels + 1 "crazy" wheel)
In this stage of the project I have implemented one IR sensor on the front of the car: TCRT5000 IR Sensor Module
One motor driver controls 2 DC motors: I2C Motor Driver V1.3 (L298N)
Arduino UNO and arduino base shield are also being used.
Standar power supply of four 1.5V batteries.
With the following code I have managed to make the car move forward whenever the IR sensor detecs a black surface and stop when the surface detected is white.
PROBLEM: when I disconnect the arduino UNO from my PC, the robot does not follow the basic intructions given in the code, it starts doing "random" movements such us changing the spin of one wheel every second while the other one starts turning without stopping despite the color of the surface.
I will now share the basis testing code (I used Auto format and the "copy to forum", hope that works)
IR sensor connected to A0 and 5 (digital);
#include "Grove_I2C_Motor_Driver.h"
#define I2C_ADDRESS 0x0a
int analogPin = A0;
int val = 0;
void setup()
{
Serial.begin(9600);
Motor.begin(I2C_ADDRESS);
pinMode(analogPin, INPUT);
pinMode(5, OUTPUT);
}
void loop()
{
digitalWrite(5, HIGH);
delayMicroseconds(500);
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
if (digitalRead(analogPin) == 0) {
Motor.stop(MOTOR1);
Motor.stop(MOTOR2);
// Serial.println(analogPin);
} else {
Motor.speed(MOTOR1, -50);
Motor.speed(MOTOR2, -50);
// Serial.println(analogPin);
}
delay (100);
}
Thank you in advance, hoping to solve this weird issue!