So I recently purchased:
DFRobot Wheel Encoders
DFRobot 3PA and 4WD Rovers (2pk)
Romeo V2 Arduino Controller
I was doing a bit of research online and I'm trying to figure out how to use the encoders to control the PWM applied to the motors which will make precise turns. This seems like a popular method to use. Here is some of my code. I have nothing for the encoders yet. I only have the sensors working the PWM control for the motors. All my code does is stop the motors when the front sensor detects a wall. My next step is to implement the encoders to give the robot a 90 degree turn. Does anyone have some sample code or tips on how to do this? Thanks in advanced!
void stop(void)
{
digitalWrite(5,LOW);
digitalWrite(6,LOW);
}
void forward(char a,char b)
{
analogWrite(5,a);
digitalWrite(4,HIGH);
analogWrite(6,b);
digitalWrite(7,HIGH);
}
void back(char a,char b)
{
analogWrite(5,a);
digitalWrite(4,LOW);
analogWrite(6,b);
digitalWrite(7,LOW);
}
void Left(char a,char b)
{
analogWrite (5,a);
digitalWrite(4,LOW);
analogWrite (6,b);
digitalWrite(7,HIGH);
}
void Right(char a,char b)
{
analogWrite (5,a);
digitalWrite(4,HIGH);
analogWrite (6,b);
digitalWrite(7,LOW);
}
void setup(void)
{
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
for(int i =4;i<=7;i++)
pinMode(i, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
forward(100,100);
Serial.begin(9600);
}
void GetSensor()
{
Sen_Val_B = SenAvg(Sensor_Back);
Sen_Val_F = SenAvg(Sensor_Front);
Sen_Val_R = SenAvg(Sensor_Right);
Sen_Val_L = SenAvg(Sensor_Left);
}
int SenAvg(int SensorNumber)
{
Arr[100]= 0;
avg = 0;
for(int i = 0; i<100; i++)
{
Arr[i] = analogRead(SensorNumber);
avg = avg + Arr[i];
}
avg = avg/100;
return avg;
}
void loop(void)
{
GetSensor();
if(Sen_Val_F > 200)
{
stop();
}
}