Buttons will not read. Please help

Hi everyone,

I just changed my program to use the internal pullup resistors and now 2 of my buttons will not work. I have written a separate program to check if the wiring and boards are working properly, and they are. But in my project program I cannot get them to work.

For some reason only the "indexDownButton" and the "printDownButton" are not working. But the "indexUpButton" and "printUpButton" are working perfectly.

These buttons are used to increment values as seen in the program. I'm new to this, so I'm really sorry if my programming is hard for you guys to sift through. I really need to get this working by tomorrow night.

#include <openGLCD.h>
#include <openGLCD_Buildinfo.h>
#include <openGLCD_Config.h>
#include <Stepper.h>
#include <AccelStepper.h>


int STEPS = 1600;
int indexLength = 0;
AccelStepper stepper(1,3, 2);

int indexDownButton = 49; 
int indexUpButton = 51;
int printUpButton = 46;
int printDownButton = 47;
int startButton = 52;
int modeButton = 53;  
int relay = 50;
int busySig = 48;

int modePushCounter = 0;
int modeState = 0;
int lastModeState = 0;

int indexCounter = 0;
int indexUpState = 0;
int lastIndexUpState = 0;
int indexDownState = 0;
int lastIndexDownState = 0;


int printCounter = 0;
int printUpState = 0;
int lastPrintUpState = 0;
int printDownState = 0;
int lastPrintDownState = 0;

int JOGGING = 0;



int sigState = 0;
int lastSigState = 0;


void setup()
{
GLCD.Init();
GLCD.SelectFont(Callibri11);

pinMode(modeButton, INPUT_PULLUP);   
pinMode(startButton, INPUT);
pinMode(busySig, INPUT);
pinMode(relay, OUTPUT);


pinMode(indexUpButton, INPUT_PULLUP);
pinMode(indexDownButton, INPUT_PULLUP);
pinMode(printUpButton, INPUT_PULLUP);
pinMode(printDownButton, INPUT_PULLUP);

stepper.setMaxSpeed(1000000);
stepper.setAcceleration(10000);

Serial.begin(9600);
  delay(7000);
  GLCD.ClearScreen();
  modePushCounter = 1;

}



void loop()
{

printUpState = digitalRead(printUpButton);

  // compare the buttonState to its previous state
  if (printUpState != lastPrintUpState) {
    // if the state has changed, increment the counter
    if (printUpState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      printCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(printCounter);
    }
    else {
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
    lastPrintUpState = printUpState;

  if(indexCounter < 0)
  {
  (indexCounter = 0);
  // save the current state as the last state,
  //for next time through the loop
  }



printDownState = digitalRead(printDownButton);

  // compare the buttonState to its previous state
  if (printDownState != lastPrintDownState) {
    // if the state has changed, increment the counter
    if (printDownState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      printCounter--;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(printCounter);
    }
    else {
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastPrintUpState = printUpState;


/***************************************************************************/ 
  indexUpState = digitalRead(indexUpButton);

  // compare the buttonState to its previous state
  if (indexUpState != lastIndexUpState) {
    // if the state has changed, increment the counter
    if (indexUpState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      indexCounter = indexCounter + 5;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(indexCounter);
    }
    else {
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  lastIndexUpState = indexUpState;


  indexDownState = digitalRead(indexDownButton);

  // compare the buttonState to its previous state
  if (indexDownState != lastIndexDownState) {
    // if the state has changed, increment the counter
    if (indexDownState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      indexCounter = indexCounter - 5;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(indexCounter);
    }
    else {
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  if(indexCounter > 80)
  (indexCounter = 0);
  // save the current state as the last state,
  //for next time through the loop
  lastIndexUpState = indexUpState;

  
indexLength = 3.183*(indexCounter+20);  //3.183 steps/mm :  add 20mm for spacing

/***************************************************************************/
 // read the mode button input pin:
  modeState = digitalRead(modeButton);

  // compare the buttonState to its previous state
  if (modeState != lastModeState) 
  {
  
    // if the state has changed, increment the counter
    if (modeState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      modePushCounter++;
      Serial.println(modePushCounter);
      
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      

    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastModeState = modeState;



 //Change mode
 if(modePushCounter == 1)
 {
   GLCD.SelectFont(Callibri11_bold);
   GLCD.CursorTo(0, 0);
   GLCD.print("                                            ");
   GLCD.CursorTo(0, 1.75);
   GLCD.print("          SELECT MODE           ");
   GLCD.CursorTo(0, 3);
   GLCD.print("             TO BEGIN                  ");
   GLCD.CursorTo(0, 4);
   GLCD.print("                           ");
   GLCD.CursorTo(0, 2);
   GLCD.print("                                           ");
   
 }



 if(modePushCounter == 2)
 {
   GLCD.SelectFont(Callibri11_bold);
   GLCD.CursorTo(0, 0);
   GLCD.SetFontColor(PIXEL_OFF);
   GLCD.print("              JOG WEB              ");
   GLCD.SetFontColor(PIXEL_ON);
   GLCD.SelectFont(Callibri11);
   GLCD.CursorTo(0, 1.75);
   GLCD.print("                                       ");
   GLCD.CursorTo(0, 2);
   GLCD.print("         PRESS START TO             ");
   GLCD.CursorTo(0, 3);
   GLCD.print("             JOG WEB                ");
   GLCD.CursorTo(0, 4);
   GLCD.print("                           ");
   
    while(digitalRead(startButton) == HIGH)
  {
      stepper.run();
      stepper.move(100);

  
  }

 }

  
 if(modePushCounter == 3)
 {
  GLCD.SelectFont(Callibri11_bold);
   GLCD.CursorTo(0, 0);
   GLCD.SetFontColor(PIXEL_OFF);
   GLCD.print("         INTERMITTENT      ");
   GLCD.SetFontColor(PIXEL_ON);
   GLCD.SelectFont(Callibri11);
   GLCD.CursorTo(0, 3);
   GLCD.print("                               ");
   GLCD.CursorTo(0, 2);
   GLCD.print("PRINT LEGNTH:            mm");
   GLCD.CursorTo(8,2);
   GLCD.print(indexCounter);
   GLCD.CursorTo(0, 4);
   GLCD.print("STATUS:                         ");

sigState = digitalRead(busySig);

if(sigState != lastSigState)
{
  delay(350);
  stepper.runToPosition();
  stepper.move(indexLength);
  digitalWrite(relay, HIGH);
  delay(25);
  digitalWrite(relay, LOW);
}
lastSigState = sigState; 
     
 }


 
 if(modePushCounter == 4)
 {
  GLCD.SelectFont(Callibri11_bold);
   GLCD.CursorTo(0, 0);
   GLCD.SetFontColor(PIXEL_OFF);
   GLCD.print("          CONTINUOUS     ");
   GLCD.SetFontColor(PIXEL_ON);
   GLCD.SelectFont(Callibri11);
   GLCD.CursorTo(0, 3);
   GLCD.print("# OF PRINTS:                  ");
   GLCD.CursorTo(8,3);
   GLCD.print(printCounter);
   GLCD.CursorTo(0, 2);
   GLCD.print("PRINT LEGNTH:            mm");
   GLCD.CursorTo(8,2);
   GLCD.print(indexCounter);
   GLCD.CursorTo(0, 4);
   GLCD.print("STATUS:                   ");
   
 }
    


 if(modePushCounter == 5)
 {
  (modePushCounter=1);
  (indexCounter=0);
 }

}

did you forget a line

(indexDownState != lastIndexDownState)

what makes it equal ?

I think you copyed the wrong line to 175

lastIndexUpState = indexUpState;//maybe this should be down not up

Since you use State in the names of variables that hold state, it makes sense to use Pin in the names of variables that hold pin numbers. That way, one does not have to scroll back to the top to read the comment that wouldn't be needed if the variable names were better.

gpop1:
did you forget a line

(indexDownState != lastIndexDownState)

what makes it equal ?

I think you copyed the wrong line to 175

lastIndexUpState = indexUpState;//maybe this should be down not up

gpop1, you're awesome. I went through this for a couple hours last night and couldn't find that simple mistake! Thanks a bunch!

And you made the same mistake with PrintDownState....

RayLivingston:
And you made the same mistake with PrintDownState....

Yup yup. That's what I get for copying and pasting my code and not double checking. -_-