CrossRoads:
position = analogRead (JoyY); // read the joystick position once, make a decision
if (position > 600) {
digitalWrite(Rmotor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(Rmotor2Pin, HIGH); // set leg 2 of the H-bridge high
}
if (position < 500) {
digitalWrite (Rmotor1Pin,HIGH);
digitalWrite (Rmotor2Pin,LOW);
}
if ( (position >=500) && (position <= 600) ) // in the no-movement center zone?
{
digitalWrite (Rmotor1Pin,LOW);
digitalWrite (Rmotor2Pin,LOW);
}
Thank you very much for the super fast reply. I didn't get the chance to actually test the code until now but it I just did and it works flawlessly. I really appreciate your help!
Hani:
FullyJosh:
I know that when the Joystick is stationary, the value is 510.
what is this value??! and how did u know it ..... ??
sorry for that stupid question
Someone posted this code on Sparkfun so I just incorporated it into my code.
/* Released to public domain */
const int selectPin = 2;
const int joystick_xPin = A0;
const int joystick_yPin = A5;
int oldX = 0;
int oldY = 0;
int oldSelect = 0;
void setup()
{
pinMode(selectPin, INPUT);
digitalWrite(selectPin, HIGH);
Serial.begin(9600);
}
void loop()
{
int joystick_x;
int joystick_y;
int select;
joystick_x = map(analogRead(joystick_xPin), 0, 1023, 1, 20);
joystick_y = map(analogRead(joystick_yPin), 0, 1023, 1, 20);
select = !digitalRead(selectPin);
if((oldX != joystick_x) ||
(oldY != joystick_y) ||
(oldSelect != select)){
Serial.print("joystick X: ");
Serial.print(joystick_x);
Serial.print(" joystick Y: ");
Serial.print(joystick_y);
if(select){
Serial.print(" select");
}
Serial.println("");
oldX = joystick_x;
oldY = joystick_y;
oldSelect = select;
}
delay(10);
}
Steen:
couldn't yo ujust do this (instead of CrossRoads' code)
position = analogRead (JoyY); // read the joystick position once, make a decision
if (position > 600)
{
digitalWrite(Rmotor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(Rmotor2Pin, HIGH); // set leg 2 of the H-bridge high
}
else if (position < 500)
{
digitalWrite (Rmotor1Pin,HIGH);
digitalWrite (Rmotor2Pin,LOW);
}
else
{
digitalWrite (Rmotor1Pin,LOW);
digitalWrite (Rmotor2Pin,LOW);
}
because this covers every case completely, which is better imo, but it doesn't make any difference on the normal functioning
I tested your code too and it works. To me, it doesn't matter which code I use so long as it works. I'm actually really glad you posted an alternative. The reason being is that I need to drive a second motor and having two scripts to work with can help me figure out which way would be easiest for this all to work. I also need to add the 'x' value on the joystick to switch between the left and right motor.
I was thinking of just having a bunch of 'if' - or in your case, 'else if' statements that cover every scenario. I'm counting 6 right now.
Both forward/back
Left forward/back
Right forward/back
Anyway, I think I'm rambling at this point. Nonetheless thanks for helping me with my code! This whole thing is coming together nice and smoothly!