(first code ever, so no idea what i am doing)
I am trying to program a simple line following robot, which has to do a bunch of stuff, but the code my question is about is just following the line and stopping if the SONAR detects something within 10 cm. I am using 3 IR-sensors, and the line is broad enough to be noticed by 2 or even 3.
At first i coded just: if the line is to the right, go to the right, the further away the sharper the turn, but this made my robot be perpendicular to the course half of the times, so i wanted to change something.
My idea was to use a loop which only checked the SONAR and if the line was far right, far left or in the middle.
Lets say the line is to the left, it should then turn to the left, and when the middle IR-sensor noticed the line, it should straighten out a bit to the right (using an if-statement in the void). In theory this should keep the robot probably to one side of the line if you make the straightening out part a bit harsh. But if done correctly, it could straighten out and then the3 IR sensors should all detect the line, and the robot would go straight.
That was the idea. Turns out, if the condition which is set for a void is not true anymore, it doesn't stay in the void, which is what i wanted. I only wanted it to go out if the robot should go straight or to the other side.
I don't even know if this is somewhat possible and how. Any help would be greatly appreciated.
This is my code, which isn't working, and which makes the motors do funny noises as well.
#include <Servo.h>
Servo myservo;
int pos = 0;
// Define pins for infrared sensors
const int infraredSensor1 = 8;
const int infraredSensor2 = 9;
const int infraredSensor3 = 10;
// Define pins for motors
const int motor1Pin = 5; // Motor 1
const int motor2Pin = 6; // Motor 2
// Define pins for Sonar
const int trigPin = 2; // Trigger
const int echoPin = 3; // Echo
//define variables SONAR
long duration;
int distance;
void setup() {
// Initialize infrared sensor pins as INPUT
pinMode(infraredSensor1, INPUT);
pinMode(infraredSensor2, INPUT);
pinMode(infraredSensor3, INPUT);
// Initialize motor pins as OUTPUT
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(4, OUTPUT);
pinMode(7, OUTPUT);
// Initialize Sonar sensor pins as INPUT & OUTPUT
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
myservo.attach(11);
myservo.write(0);
Serial.begin(9600);
}
int getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
void loop() {
distance = getDistance();
// Check if distance is less than 10 cm
if (distance < 10) {
// Stop both motors
stopMotors();
} else {
if (digitalRead(infraredSensor1) == LOW && digitalRead(infraredSensor2) == HIGH && digitalRead(infraredSensor3) == HIGH) {
left();
}
if (digitalRead(infraredSensor1) == HIGH && digitalRead(infraredSensor2) == HIGH && digitalRead(infraredSensor3) == LOW) {
right();
}
if (digitalRead(infraredSensor1) == LOW && digitalRead(infraredSensor2) == LOW && digitalRead(infraredSensor3) == LOW) {
straight();
}
}
}
void stopMotors() {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 0);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 0);
}
void straight() {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 180);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 180);
}
void left() {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 220);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 120);
delay(1000);
if (digitalRead(infraredSensor1) == LOW && digitalRead(infraredSensor2) == LOW && digitalRead(infraredSensor3) == HIGH) {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 160);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 220);
}
}
void right() {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 120);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 220);
delay(1000);
if (digitalRead(infraredSensor1) == LOW && digitalRead(infraredSensor2) == LOW && digitalRead(infraredSensor3) == HIGH) {
digitalWrite(7, LOW);
analogWrite(motor1Pin, 220);
digitalWrite(4, LOW);
analogWrite(motor2Pin, 160);
}
}