Motor PWM using buttons

Here is the start to my code. I am starting out with buttons to control the motor's speed instead of a joystick, because it's is easier to start out with. Although I can not get the program to read the button states. I have practically copied what the Button example sketch shows. Could you read through the code and guide me to my solution. I have only programmed the forward function, so just read ignore the backward function.

//Button controlled PWM //Connor Widder //7/5/13

//Pins const int MotorPinForward = 5; //const int MotorPinBackward = #; const int Button1Pin = 2; const int Button2Pin = 3; const int Button3Pin = 4;

int Button1State = 0; int Button2State = 0; int Button3State = 0;

void setup () {

Serial.begin(9600); pinMode(MotorPinForward, OUTPUT); //pinMode(MotorPinBackward, OUTPUT); pinMode(Button1Pin, INPUT); pinMode(Button2Pin, INPUT); pinMode(Button3Pin, INPUT);

}

void loop () {

Serial.print("Begin");

Button1State = digitalRead(Button1Pin); Button2State = digitalRead(Button2Pin); Button3State = digitalRead(Button3Pin);

if (Button1State == HIGH) { analogWrite(MotorPinForward, 85); Serial.println("Low Speed"); }

else if (Button2State == HIGH) { analogWrite(MotorPinForward, 170); Serial.println("Medium Speed"); }

else if (Button3State == HIGH) { analogWrite(MotorPinForward, 255); Serial.println("High Speed"); }

else { analogWrite(MotorPinForward, 0); Serial.println("Off"); }

}

Try adding print of ButtonXState for debug

...
  Button1State = digitalRead(Button1Pin);
  Button2State = digitalRead(Button2Pin);
  Button3State = digitalRead(Button3Pin);
  Serial.print("Button1State= ");   Serial.println(Button1State); 
  Serial.print("Button2State= ");   Serial.println(Button2State); 
  Serial.print("Button3State= ");   Serial.println(Button3State); 
  if (Button1State == HIGH)
...

P.S. the code need to be under tags (use button with simbol #)

connor1868:
Although I can not get the program to read the button states.

What evidence do you have to support this?

How is your switch wired up? What value pulldown resistor are you using?

The buttons are wired up to 5 volts, so that when the button is pressed 5 volts is sent into a input pin to read that it is pressed. I am using a 10K pull down resister. I can tell that the program is not even moving into the if else statements because if a button is not presses, the else statement, the motor is set to 0 pwm, or otherwise not moving.

Hi connor1868,

I suggest that you add a delay statement at the bottom of the loop to allow the print statement to output. Start with 500 (1/2 second) and see what happens. Assuming that the wiring is correct, you are giving your program a bad case of output constipation. You can reduce this significantly when you get this working and remove the printouts.

Also, add the debug statements that nid96ita recommends. (You should increase your delay to 1 or 2 seconds in this case.)

Then let us know what happens!

Pat.

The code you posted contains serial print statements which should tell you which path the code is taking. What do they tell you is happening?