trouble incrementing variable

I am having trouble cycling through my shift register. Basically what I’m looking for is an individual output from the shift register to be high when pin 2 on the arduino is receiving 5v. Basically cycling through the pins on the 595 with each button press. I have it working for the initial value but once the value goes low again it stops working. Any suggestions are greatly appreciated. Here is my code:

#include <Shifter.h>

#define SER_Pin 8 //DATA
#define RCLK_Pin 9 //LATCH
#define SRCLK_Pin 10 //CLOCK

#define NUM_REGISTERS 1 

const int buttonPin = 2;
int buttonState = LOW;
int n = 0;

Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS); 

void setup(){

}

void loop()
{
  shifter.clear(); 
  shifter.write();

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) 
  {
  shifter.setPin(n, HIGH); 
  shifter.write(); 
  delay(150);
  n++;
  }
}

In setup make buttonPin an input.

LarryD:
In setup make buttonPin an input.

I/O pins default to inputs.

mwawrzyniec:
I have it working for the initial value but once the value goes low again it stops working. Any suggestions are greatly appreciated.

I don't see any code to set n back to zero after you have reached the maximum number of pins. I would expect to see something like (assuming NUM_REGISTERS is the number of pins that you want to cycle through):

n = (n + 1) % NUM_REGISTERS;

Basically cycling through the pins on the 595 with each button press.

Your first step, then, is to detect when the switch BECOMES pressed, not when the switch IS pressed.

Look at the state change detection example.

I took a look at the state change detection example and modified my code, I won’t be able to test this until tomorrow. I’ll report back if it was successful:

#include <Shifter.h>


#define SER_Pin 8 //DATA
#define RCLK_Pin 9 //LATCH
#define SRCLK_Pin 10 //CLOCK

#define NUM_REGISTERS 1 

const int  buttonPin = 2;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
Shifter shifter(SER_Pin, RCLK_Pin, SRCLK_Pin, NUM_REGISTERS);

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  shifter.clear(); 
  shifter.write();
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      shifter.setPin(buttonPushCounter, HIGH); 
      shifter.write(); 
      delay(150);
    } 

  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

}

You need to debounce the button as well, look in the "playground".

Mark

You need to debounce the button as well, look in the "playground".

You think there is some possibility that the switch will bounce for more than the 150 millisecond delay already there? I doubt it.

missed the delay()

Mark

I loaded that sketch and it is not lighting up in order, seem to skip around at random. Any ideas or obvious flaws in my sketch?

mwawrzyniec:
I took a look at the state change detection example and modified my code, I won't be able to test this until tomorrow. I'll report back if it was successful:

I don't think that code will work because of the influence of th delay() on the button detection. I would separate the button detection from the shift output so it looks something like this (not tested, and not complete)

boolean buttonPressed = false;
byte n = 0;
byte maxShift = 8;
// other global declarations

void setup() {
   // usual stuff
}

void loop() {
  readButton();
  writeToShift();
}

void readButton() {
   if (buttonPressed == false) {
       buttonPressed = ! digitalRead(buttonPin); // assumes pin goes low when button is pressed
   }
}

void writeToShift() {
  if (buttonPressed) {
       shifter.setPin(n, HIGH); 
        shifter.write(); 
         delay(150);
         buttonPressed = false; // reset ready for next push
         n++;
         if (n >= maxShift) {
            n = 0;
        }
  }
}

...R