Hi Guys,
I am brand new here, so please forgive my "noobie"ness. This should be quite a simple problem, but it is driving me loopy...
Basically, I want to control two motors (forward fast / slow and steering via differential speed).
The inputs are: X-axis potentiometer (turn) is connected to pin A1 and Y-axis (move) pot is connected to A0.
0,-128 is bottom left and 255,128 is top right. There is a "forward only" strip in the centre from -10 to +10, where turning signal is ignored (just to make it not too squirrely).
Outputs are: left motor digital pin 10 and right motor digital pin 9.
I have written a sketch which sort of works, but whenever the "turn" value is greater than the "move" value, it starts behaving erratically.
I have been trying to figure this out all day, and am starting to lose the plot!! Please find the code below... any ideas? This is my first real go at programming, so please be kind.... Thanks, Tim
/* This script is intended to operate a differential steering system, but is
a little buggy. Whaenever the Y value is greater than the X value, it reverses
it's operation.
*/
const int LMotor=10;
const int RMotor=9;
int val = 0;
int off = 0;
int move;
int turn;
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(LMotor,OUTPUT);
pinMode(RMotor,OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the X (turning) input on analog pin A1 and Y (ahead) input
//on analog pin A0:
int aheadY = analogRead(A0);
int turnX = analogRead(A1);
move = map(aheadY, 0,1023, 0, 255);
move = constrain(move, 0, 255);
turn = map(turnX, 0,1023,-128,127);
turn = constrain(turn, -128,127);
Serial.print("Move ");
Serial.println(move);
Serial.print(" Turn ");
Serial.println(turn);
if (turn >=-10 && turn <=10) // go forward
{
analogWrite (LMotor,move);
analogWrite (RMotor,move);
}
if (turn > move) // this sequence seems to
{
analogWrite (LMotor,0); // have absolutely no effect..
analogWrite (RMotor,0);
}
if (turn <-10) // forward left
{
analogWrite (LMotor,move + turn);
analogWrite (RMotor,move);
}
if (turn >10) // forward right
{
analogWrite (LMotor,move);
analogWrite (RMotor,move - turn);
}
}