Hi!
I'm hopping in on a project and need to work with someone else's code, and learn quickly! Please help:)
So I have two joysticks that connect to stepper motors that control an object.
The problem I'm having, is that the object is moving even when the joysticks aren't being touched.
I'm trying to solve this by testing when the joysticks are actually being pushed, and if they are, then go ahead and do steps, etc.
//CONTROLS THE JOYSTICKS, global variables
#define Xpin1 A0 // Pin A0 connected to Joystick 1 x-axis
#define Ypin1 A1 // Pin A1 connected to Joystick 1 y-axis
#define button_1 38 // Pin 38 connected to Joystick Button 1
#define Xpin2 A3 // Pin A2 connected to Joystick 2 x-axis
#define Ypin2 A2 // Pin A3 connected to Joystick 2 y-axis
#define button_2 39 // Pin 39 connected to Joystick Button 2
void setup() {
Serial.begin(9600);
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
//This was never implemented but unsure if should be?
//pinMode(Xpin1, INPUT);
//pinMode(Xpin2, INPUT);
//pinMode(Ypin1, INPUT);
//pinMode(Ypin2, INPUT);
....
...
...
}
void loop() {
....
....
..
if(switch1) {
///THE IF STATEMENT COUNTER==25 used to be the if statement in the code, however that was causing
///the problem, bc the counter increases no matter if the joysticks are touched or not.
//if (counter == 25)
//BELOW is my thoughts, except this doesn't work either.
if (analogRead(Xpin1) != 512 || analogRead(Xpin2) != 512
|| analogRead(Ypin1) != 512 || analogRead(Ypin2) != 512) {
set_bottom(scale(analogRead(X_pin_1) - 512.0), scale(analogRead(Y_pin_1) - 512.0));
set_top();
}
}
....
......
}
My logic for using the current if statement:
When the joystick isn't being touched, analogRead() returns numbers around 512, ranging between 480ish - 528ish. I'm wondering what conditional I can use to determine if either of the two joysticks are being pushed?
I used to think that I could use digitalRead() on the joystick Xpins and Ypins, but someone told me that reading would only work for digital joysticks? Either way, when I don't touch the joysticks and do digitalRead on their position pins, I get one y position reading as HIGH and the rest reading as LOW.
Can someone please enlighten me?