Having trouble interfacing buttons w/ this code..

The code is pretty self explanatory and commented. I started with my full code, then when I encountered problems with implementing hardware I made a shorter, not fully complete, version for trying to test my hardware, but I’m still stumped. I think my problem lies with pullup/pulldown resistors which is a subject im having a hard time grasping because 12v systems dont need stuff like that. Ill post code then explain problems Im having.

“full version”

//2 signal Wire controlled H-Bridge. Analog-Linear setup
// 0v = full reverse, 2.5v = stop 5v = full forward

#define LeftMotorSig 9  //pwm outputs
#define RightMotorSig 10 //pwm outputs

#define LeftNeutral 8 // neutral indicator left
#define RightNeutral 7 // neutral indicator right

#define LeftReverse 0  // button in
#define LeftForward 1 // button in
#define RightReverse 2 // button in
#define RightForward 3 // button in

int LRVal = 0;
int LFVal = 0;
int RRVal = 0;
int RFVal = 0;

void setup ()
{
  pinMode(LeftMotorSig, OUTPUT);
  pinMode(RightMotorSig, OUTPUT);
  pinMode(LeftNeutral, OUTPUT);
  pinMode(RightNeutral, OUTPUT);
  Serial.begin(9600);
}
void loop ()
{
  LRVal = analogRead(LeftReverse);
  LFVal = analogRead(LeftForward);
  RRVal = analogRead(RightReverse);
  RFVal = analogRead(RightForward);
  
int LRVal2 = LRVal/8;
int LFVal2 = LFVal/8;
int RRVal2 = RRVal/8;
int RFVal2 = RFVal/8;

if ((LRVal2 == 0) && (LFVal2 == 0)) {
  analogWrite(9, 127);
  digitalWrite(LeftNeutral, HIGH);
} else {
  digitalWrite(LeftNeutral, LOW);
}
if ((RRVal2 == 0) && (RFVal2 == 0)) {
  analogWrite(10, 127);
  digitalWrite(RightNeutral, HIGH);
} else {
  digitalWrite(RightNeutral, LOW);
}
if (LFVal2 > 0)
analogWrite(9, LFVal2 + 127);

if (LRVal2 > 0)
analogWrite(9, 127 - LRVal2);

if (RFVal2 > 0)
analogWrite(10, RFVal2 + 127);

if (RRVal2 > 0)
analogWrite(10, 127 - RRVal2);


}

As you can see, the idea is using pressure sensitive buttons to control PWM signal. The problem is that when I breadboard it, every button ends up pulling the PWM signal LOW from 2.5v.

At first I thought that maybe I was somehow showing 129 bytes for the forward values, and adding 127 to them for 256 which would be low, so I shortened my code a lot to just two buttons and changed the forward to not use math and just use 255. Obviously this is not how it would stay, I’m just trying to troubleshoot.

#define LeftMotorSig 9  //pwm outputs
#define LeftNeutral 13 // neutral indicator left
#define LeftReverse 0  // button in
#define LeftForward 1 // button in
int LRVal = 0;
int LFVal = 0;


void setup ()
{
   pinMode(LeftMotorSig, OUTPUT);
     Serial.begin(9600);
}
void loop ()
{
  LRVal = analogRead(LeftReverse);
  delay(30);
  LFVal = analogRead(LeftForward);
  int LRVal2 = LRVal/8;
int LFVal2 = LFVal/8;
  if ((LRVal2 == 0) && (LFVal2 == 0)) {
  analogWrite(9, 127);
  digitalWrite(LeftNeutral, HIGH);
} else {
  digitalWrite(LeftNeutral, LOW);
}
 
 if (LRVal2 > 0) {
analogWrite(9, 127 - LRVal2);
} else {
  if (LFVal2 > 0)
  analogWrite(9, 255);
}
}

I still end up getting ~0v when buttons are pressed, unless I pull the signal straight from Left Reverse ( analog 0) and run it straight to ground. Then I will get +5v when i push Left Forward button. Do I need a pull down resistor on all the buttons that would make the 2.5 go low and a pull up on buttons that are intended to go from 2.5 → 5v?

I’ve spent about 8 hours coding and messing with this (sad, I know) and I am completely stumped. Can anyone point me in the right direction? Thanks.

It sounds like your hardware is not wired up right. How are you wiring the buttons, the best way is for the button to be between input and ground and then enable the internal pull up resistor with a digitalwrite HIGH in the setup.

Why are you reading digital switch states using analogRead? Why are digital switches connected to analog pins?

Or, are you using something besides a digital switch?

It sounds like your hardware is not wired up right. How are you wiring the buttons, the best way is for the button to be between input and ground and then enable the internal pull up resistor with a digitalwrite HIGH in the setup.

Thank you, I will try that.

Why are you reading digital switch states using analogRead? Why are digital switches connected to analog pins?

Or, are you using something besides a digital switch?

Im not sure the answer to this question. I took apart some old console controllers that have buttons that are pressure sensitive, and cut the ribbons and was running from those. They are variable resistance based on how hard you press the button, I confirmed this with the DMM and watched resistance decrease based on pressure sensitivity, so I would assume those are analog and not digital but I know what happens when I assume..

I actually just got back from Radioshack where I picked up a pair of 2 pole/ 6 position rotary switches. I realize this requires rewriting most of the code, but does this sound like a better option?

I can't see how a 6-position switch is an option for a pressure-sensitive switch? What exactly are you are trying to do here?

I found it difficult to follow exactly how things are connected. A diagram is worth 1000 words.

Do you know exactly what is the the no-force and max-force resistance for each button? You need to scale the pull-down or pull-up resistor in proportion to the no-force resistance.

Max force = 0 ohms, not pressed = OL, ill have to experiment to find the no force point? or is that the not pressed point of OL? I should have explained, I am just wanting PWM control for a pair of motors. Pressure switches seemed like a good option since I had old console stuff to scrounge them from. If I used the 6 position switches it would be to have different speed settings as opposed to a constant curve/line as would be applied by pressure switch. At this point, I plan on trying it both ways, with the position switches, and the pressure switches after messing with the internal pull up stuff. Worst case scenario : I learn something new, right? ;)