Apologies in advance if this question has been answered, I could not find it on this forum (or anywhere else, for that matter).
I'm trying to develop my coding skills by weaving together two tasks together and have exhausted my ideas on solving this.
What I am working on:
A four wheeled rover with a DC motor driving two wheels, a servo turning the other set of wheels and an IR
Sensor that provides feedback on how to behave.
What I want the rover to do:
Move forward until the IR Sensor detects an obstacle.
Move backward and turn the wheels to reorient the rover.
Move forward again.
So far, I have been successful at either turning the wheels left and right or rolling forward and backward as separate sketches.
My code is missing the component that weaves these two activities together.
Here is my code so far:
#include <Servo.h>
const int EN=9; //Half bridge 1 enable
const int MC1=3; //MOtor control 1 on pin 3
const int MC2=2; //MOtor control 2 on pin 2
const int IR=0; //IR on analog pin 0
const int SERVO=10; //Half bridge 3,4 enable
Servo myServo; //Servo object
int dist1 = 0; //forward
int dist2 = 0; //reverse
int dist3 = 0; //brake
int val = 0; //for storing the reading from the pot
int velocity = 0; //For storing the desired velocity (from 0-255)
void setup()
{
Serial.begin(9600);
myServo.attach(SERVO);
pinMode(EN, OUTPUT);
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
brake(); //Initialize with motor stopped
}
void loop()
{
Serial.println(val);
val = analogRead(IR); //read IR
val = map(val, 0, 1023, 0, 179); //scale it to servo range
myServo.write(val); //sets the servo
//delay(15); //waits for the servo
dist1 = readDistance(0); //measure IR distance at zero degrees
analogWrite(SERVO, dist1);
delay(300); //delay before next measurement
dist2 = readDistance(25); //measure IR distance at zero degrees
analogWrite(SERVO, dist2);
delay(300);
dist3 = readDistance(0);
analogWrite(SERVO, dist1);
delay(300); //delay before next measurement
//go forward
if (val > 108)
{
velocity = map(val, 108, 1023, 0, 255);
forward(velocity);
digitalWrite(MC2, 140);
//dist1 = readDistance(0); //measure IR distance at zero degrees
//analogWrite(SERVO, dist1);
//delay(30); //delay before next measurement
}
//go backward
else if (val < 107)
{
velocity = map(val, 107, 1023, 0, 255);
reverse(velocity);
digitalWrite(MC1, 190);
dist2 = readDistance(15); //measure IR distance at zero degrees
//analogWrite(SERVO, dist2);
//delay(30);
}
//brake
else
{
brake();
dist3 = readDistance(0);
analogWrite(SERVO, dist1);
delay(30); //delay before next measurement
}
}
//Motor goes forward at given rate (from 0-255)
void forward (int rate)
{
digitalWrite(EN, HIGH);
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
analogWrite(EN, rate);
}
//Motor goes backward at given rate (from 0-255)
void reverse (int rate)
{
digitalWrite(EN, HIGH);
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
analogWrite(EN, rate);
}
//stops motor
void brake()
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
digitalWrite(EN, LOW);
}
int readDistance(int pos)
{
int dist = analogRead(IR); //read ir sensor
}
Attached are the circuits. IR replaces the trim pot.
Any advise is appreciated. Thank you.