Hello, I have my Arduino connected to a 4 button micro switch joystick, each switch is connected to a different digital IO pin, with resistor going to ground, it reads the state of the switches, then depending on which switch is pushed sends a high output to a different digital IO pin. I intend on connecting these outputs to a motor driver. When I pull the joystick backward It works perfectly with a high signal (I checked the outputs with an oscilloscope) the problem is that when I push the joystick forward, rather than getting a high signal from the pin I get what looks like a messy square wave. I used the digitalwrite function for both directions, and even if I change the output pins I get the same problem. Here is my code
// set pin numbers:
const int JoystickFWD = 2; // the number of the pin attached to forward on joystick
const int FWD1 = 6; // the number of the pin attached to h bridge FWD1 pin (Left motor forward)
const int FWD2 = 5; // the number of the pin attached to h bridge FWD2 pin (Right motor forward)
const int JoystickBACK = 4; // the number of the pin attached to back on joystick
const int BACK1 = 10; // the number of the pin attached to h bridge BACK1 pin (Left motor back)
const int BACK2 = 11; // the number of the pin attached to h bridge BACK2 pin (Right motor back)
const int JoystickLEFT = 7; // the number of the pin attached to left on the joystick
const int JoystickRIGHT = 8; // the number of the pin attached to right on the joystick
// variables will change:
int buttonState = 0; // variable for reading the Joystick Direction
void setup() {
// initialize the h bridge pins as outputs:
pinMode(FWD1, OUTPUT);
pinMode(FWD2, OUTPUT);
pinMode(BACK1,OUTPUT);
pinMode(BACK2,OUTPUT);
// initialize the joystick directions as inputs:
pinMode(JoystickFWD, INPUT);
pinMode(JoystickBACK, INPUT);
pinMode(JoystickLEFT, INPUT);
pinMode(JoystickRIGHT, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(JoystickFWD);
// check if the joystick is pushed forward.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH){
// turn motors on:
digitalWrite(FWD1, HIGH);
digitalWrite(FWD2, HIGH);
}
else {
// turn motors off:
digitalWrite(FWD1, LOW);
digitalWrite(FWD2,LOW);
}
buttonState = digitalRead(JoystickBACK);
if (buttonState == HIGH){
digitalWrite(BACK1, HIGH);
digitalWrite(BACK2, HIGH);
}
else {
digitalWrite(BACK1, LOW);
digitalWrite(BACK2,LOW);
}
buttonState = digitalRead(JoystickLEFT);
if (buttonState == HIGH){
digitalWrite (FWD1, HIGH);
}
else {
digitalWrite(FWD1,LOW);
}
buttonState = digitalRead(JoystickRIGHT);
if (buttonState == HIGH){
digitalWrite (FWD2, HIGH);
}
else{
digitalWrite(FWD2, LOW);
}
}
Any suggestions?
I'm not sure if this is a hardware or software problem so I posted in both hope that's alright