Two buttons to control variable value not working...

I am using two digital buttons to make a fixed value increase or decrease, depending on which button is pressed. This is being used as a controller in another software to change throttle.

Here is my code:

#include “UnoJoy.h”

const int buttonPin = 2; // the button on the top
const int buttonPin2 = 3; // the button in the front
int buttonState = 0;

void setup() {
setupPins();
setupUnoJoy();

** pinMode(buttonPin, INPUT);**
** pinMode (buttonPin2, INPUT);**
}

int throttle = 0;

void loop() {
buttonState = digitalRead(buttonPin);

** if (buttonState == HIGH) {**
** if (throttle < 255)**
** throttle = ++throttle;**
** else**
** throttle = 255;**
** }**
** buttonState = digitalRead(buttonPin2);**
** if (buttonState == HIGH) {**
** if (throttle > 0)**
** throttle = --throttle;**
** else**
** throttle = 0;**
** }**

dataForController_t controllerData = getControllerData();
setControllerData(controllerData);
}

void setupPins(void) {
pinMode(A4, INPUT);
digitalWrite(A4, HIGH);
pinMode(A5, INPUT);
digitalWrite(A5, HIGH);
}

dataForController_t getControllerData(void) {

dataForController_t controllerData = getBlankDataForController();

controllerData.leftStickX = analogRead(A0) >> 2;
controllerData.leftStickY = analogRead(A1) >> 2;
controllerData.rightStickX = analogRead(A2) >> 2;
** controllerData.rightStickY = throttle;**

return controllerData;
}

I have bolded everything I think is relevant to this issue. The program that’s taking the Arduino’s data is not reading the values of my buttons. Any ideas?

The way your code is written, there needs to be pull-down resistors on your buttons. Are they present? The more common way to to connect one side of the button to ground and the other to the input pin which is configured as INPUT_PULLUP. This method produces LOW when the button is pressed and HIGH when it is not pressed.

You did not provide a schematic of your circuit so only you will know for sure...

I have a 10k resistor connected to one side of the button and the digital pin. The other side is attached to 5V.

lprevs:
I have a 10k resistor connected to one side of the button and the digital pin. The other side is attached to 5V.

Presumably when you press the button it pulls the pin to GND and if so your code should be

if (buttonState == LOW) {

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code looks like this

and is easy to copy to a text editor See How to use the Forum

lprevs:
I have a 10k resistor connected to one side of the button and the digital pin. The other side is attached to 5V.

A bunch of useless words. Draw a picture, and post it. It sounds, from what you wrote, that you have a 3 legged resistor. That would be a potentiometer, and is NOT what you should be using.

I was able to fix it. There weren't any wiring issues. Instead, I was missing the else loop that checked when the button wasn't clicked, and kept the throttle's value steady. Also, I needed a delay on each click (delay(250)) since the loop cycles very quickly in a short time span.

lprevs:
Also, I needed a delay on each click (delay(250)) since the loop cycles very quickly in a short time span.

Yeah, so? Great time to do other stuff. Let it, just don't act :wink:

I was missing the else loop that checked when the button wasn't clicked

The else STATEMENT does not cause looping.