Hello, I am using the arduino to read inputs from a joystick, and then send outputs to an H-bridge driver. I am using analogWrite(pin#, 255), but rather than getting an always on output I get a pulsed output with a duty cycle of around 50% the weird thing is that this only happens when I push the joystick forward, left, and right. If I push the joystick back I get the output that I expect. I've tried changing the output pins and the input pins of the joystick, I've tried different joysticks, and I tried using digitalwrite but the problem persists. here is my code.
/* Capstone Project
Sends On signals to the inputs of an H bridge when a joystick is pushed
in various directions
*/
// constants won't change. They're used here to
// 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){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
}
else {
// turns both motors off:
analogWrite(FWD1, 0);
analogWrite(FWD2, 0);
}
buttonState = digitalRead(JoystickBACK);
if (buttonState == HIGH){
analogWrite(BACK1, 255);
analogWrite(BACK2, 255);
}
else {
analogWrite(BACK1, 0);
analogWrite(BACK2, 0);
}
buttonState = digitalRead(JoystickLEFT);
if (buttonState == HIGH){
analogWrite (FWD1, 255);
}
else {
analogWrite(FWD1, 0);
}
buttonState = digitalRead(JoystickRIGHT);
if (buttonState == HIGH){
analogWrite (FWD2, 255);
}
else{
analogWrite(FWD2, 0);
}
}
/*
Code modified from
created 2005
by DojoDave <http://www.0j0.org>
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
Joysticks use potentiometers, which are analog devices. You appear to have the joystick connected to digital pins, and are using digital functions to read the joystick.
I don't think debouncing is necessary, I've checked signals going to the arduino with an oscilloscope when I hold the joystick in a direction the signal is very steady.
/* Capstone Project
Sends On signals to the inputs of an H bridge when a joystick is pushed
in various directions
*/
// constants won't change. They're used here to
// 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){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
delay(40);
}
else {
// turns both motors off:
analogWrite(FWD1, 0);
analogWrite(FWD2, 0);
}
buttonState = digitalRead(JoystickBACK);
if (buttonState == HIGH){
analogWrite(BACK1, 255);
analogWrite(BACK2, 255);
}
else {
analogWrite(BACK1, 0);
analogWrite(BACK2, 0);
}
buttonState = digitalRead(JoystickLEFT);
if (buttonState == HIGH){
analogWrite (FWD1, 255);
analogWrite (FWD2, 0);
}
else {
analogWrite(FWD1, 0);
analogWrite(FWD2, 0);
}
buttonState = digitalRead(JoystickRIGHT);
if (buttonState == HIGH){
analogWrite (FWD2, 255);
analogWrite (FWD1 ,0);
}
else{
analogWrite(FWD2, 0);
analogWrite(FWD1, 0);
}
}
/*
Code modified from
created 2005
by DojoDave <http://www.0j0.org>
modified 17 Jun 2009
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Button
*/
I'm just guessing on the duty cycle, it looks like about 50% when I check out the output on an oscilloscope
actually adding that delay after
if (buttonState == HIGH){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
helped, but the same thing doesn't work with the left and right sections
I suspect that the problem is that more than one switch is being triggered at a time. I suspect that you are executing the analogWrite on and analogWrite off statements so close together that the duty cycle really is only 50%.
Try reading all 4 switches into different variables, making the appropriate amalogWrite statements, then Serial.print() the 4 switch states.
I didn't do it with serial print, but I have checked to make sure multiple buttons aren't being turned on. I also have a restrictor plate on the joystick that ensures that won't happen. And it doesn't make sense that it only happens on left and right, and that the problem persists when i rearrange the buttons
I think I've pretty conclusively ruled out the buttons as the problem, is there any other reason why I can turn on two pins with analog write i.e. with
buttonState = digitalRead(JoystickFWD);
// check if the joystick is pushed forward.
// if it is, the buttonState is HIGH
if (buttonState == HIGH){
// turns both motors on
analogWrite(FWD1, 255);
analogWrite(FWD2, 255);
delay(40);
}
else {
// turns both motors off
analogWrite(FWD1, 0);
analogWrite(FWD2, 0);
}
buttonState = digitalRead(JoystickBACK);
if (buttonState == HIGH){
analogWrite(BACK1, 255);
analogWrite(BACK2, 255);
}
else {
analogWrite(BACK1, 0);
analogWrite(BACK2, 0);
}