Dc motor control using 3 push buttons

hello i am trying to control a dc motor using 3 push buttons and an H bridge. one push button is to increase speed, the other is to decrease it, both by a factor of 25%. The 3rd one is for direction, every 2 consecutive button presses the motor changes direction. I dont know why it isnt working, the motor does not turn or react with the push buttons. This is what i got so far: (NOTE A0 and A1 are connected to the h bridge inputs while speedpin is connected to the enable)

int directionpin = 8;
int uppin = 9;
int downpin = 10;
int speed = 0;
int val;
int buttonState;
int speedpin=6;
void setup() {

pinMode(speedpin, OUTPUT);
pinMode(directionpin, INPUT);
pinMode(uppin, INPUT);
pinMode(downpin, INPUT);
pinMode(directionpin, INPUT_PULLUP);
digitalWrite(uppin, HIGH);
digitalWrite(downpin, HIGH);
digitalWrite(directionpin, HIGH);
buttonState = digitalRead(directionpin);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
Serial.begin(9600); // start up serial communication

}

void loop(){

val = digitalRead(directionpin);
buttonState = val;
if (val != buttonState) {
if (val == LOW) {
digitalWrite(A1, LOW);
digitalWrite(A0, HIGH);
analogWrite(speedpin, speed);
}
else {
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
analogWrite(speedpin, abs(speed));}
}
if (LOW == digitalRead(uppin))
speed = speed + (1024 * .25);
if (LOW == digitalRead(downpin))
speed = speed - (1024 * .25);

speed = constrain(speed, -1023, 1023);
}

Any help regarding why?

All your motor control logic is contained in an if that will always be false, hence why the buttons are apparently ignored.

Which Arduino board are you using?

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

wildbill:
All your motor control logic is contained in an if that will always be false, hence why the buttons are apparently ignored.

thats the problem, i am supposed to set the dc motor to turn in a specific direction after each 2 presses however the motor is not turning whatever i press, nor is the speed configuration working. I tried the code for each 1 press and it still wont work. The error is definitely in the if statement, but how can i fix it.

 buttonState = val; 
 if (val != buttonState) {

What do you expect there? buttonState = val. How, in the very next instruction, can val be not = buttonState?

Again, what Arduino board are you using?

groundFungus:

 buttonState = val; 

if (val != buttonState) {




What do you expect there? buttonState = val. How, in the very next instruction, can val be not = buttonState?

Again, what Arduino board are you using?

i am using arduino UNO R3. also i have modified the code, but now i only face 2 problems, i am not getting any speed change and the motor default speed is way beyond the constrain. Other problem is that im not getting the required output where every 2 push button presses the dc motor change direction. The first problem however is the most problematic. any tips???
byte ButtonCounter =0;
byte ButtonDown =0;
int directionpin = 8;
int uppin = 9;
int downpin = 10; // push button connections
int speed = 0;
int buttonState;
const int speedpin=6;
void setup() {
pinMode(A0, OUTPUT);// counterclockwise
pinMode(A1, OUTPUT); // clockwise
pinMode(speedpin, OUTPUT);
pinMode(directionpin, INPUT);
pinMode(uppin, INPUT);
pinMode(downpin, INPUT);
pinMode(directionpin, INPUT);
digitalWrite(uppin, HIGH);
digitalWrite(downpin, HIGH);
digitalWrite(directionpin, HIGH);
buttonState = digitalRead(directionpin); // read the initial state
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
Serial.begin(9600); // start up serial communication

}
void loop(){
if (LOW == digitalRead(uppin))
speed = speed + (1024 * .25);
if (LOW == digitalRead(downpin))
speed = speed - (1024 * .25);

speed = constrain(speed, -1023, 1023);
if (digitalRead( 8 )) //button up
{
ButtonDown=0;
}
else //button down
{
if (!ButtonDown) //button was previously up
{

ButtonCounter++;
ButtonDown++;
}
}
if (ButtonCounter % 2 == 1)
{
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
analogWrite(speedpin, speed);
}
else
{
digitalWrite(A1, LOW);
digitalWrite(A0, HIGH);
analogWrite(speedpin, abs(speed));
}
}

PWM (analogWrite()) is 8 bit on an Uno (0 to 255).

Here is one way to do what you want using the state change detection method to sense the button presses.

const byte directionPin = 8;
const byte upPin = 9;
const byte downPin = 10; // push button connections
const byte speedPin = 6;

boolean dirState = HIGH;
boolean upState = HIGH;
boolean downState = HIGH;
boolean lastDirState = HIGH;
boolean lastUpState = HIGH;
boolean lastDownState = HIGH;

boolean thisDir = true;

int speed = 0;
byte speedChange = 255 / 4;

void setup()
{
   pinMode(A0, OUTPUT);// counterclockwise
   pinMode(A1, OUTPUT); // clockwise
   pinMode(speedPin, OUTPUT);
   pinMode(directionPin, INPUT_PULLUP);
   pinMode(upPin, INPUT_PULLUP);
   pinMode(downPin, INPUT_PULLUP);
   pinMode(speedPin, OUTPUT);

   Serial.begin(9600); // start up serial communication
   Serial.println("adjust speed and direction of motor");
}

void loop()
{
   static unsigned long timer = 0;
   // do 20 times per second   
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      checkDir();
      checkUp();
      checkDown();
   }
}

void checkUp()
{
   upState = digitalRead(upPin);
   if (upState != lastUpState)
   {
      if (upState == LOW)
      {
         speed = speed + speedChange;
         if (speed > 255)
         {
            speed = 255;
         }
         Serial.print("speed up to ");
         Serial.println(speed);
      }
      lastUpState = upState;
   }
}

void checkDown()
{
   downState = digitalRead(downPin);
   if (downState != lastDownState)
   {
      if (downState == LOW)
      {
         speed = speed - speedChange;
         if (speed <= 0)
         {
            speed = 0;
         }
         Serial.print("speed down to ");
         Serial.println(speed);
      }
      lastDownState = downState;
   }
}

void checkDir()
{
   dirState = digitalRead(directionPin);
   if (dirState != lastDirState)
   {
      if (dirState == LOW)
      {
         thisDir = !thisDir;

         if (thisDir == LOW)
         {
            Serial.println("dir CW");
            digitalWrite(A0, LOW);
            digitalWrite(A1, HIGH);
            analogWrite(speedPin, speed);
         }
         else
         {
            Serial.println("dir CCW");
            digitalWrite(A1, LOW);
            digitalWrite(A0, HIGH);
            analogWrite(speedPin, speed);
         }
      }
      lastDirState = dirState;
   }
}

Note that the code is formatted with the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting and posted in code tags.