Some useful functions - reading input and using shift registers

Hi all,

I’ve been working with my Sparkfun Redboard, getting it ready to look after about four different animations on my model railroad. While programming it, I’ve written a couple of subfunctions to handle bits of code that are used several times. I’d like to share them here, in case others may find them useful, too.

The first is called ‘readPin’:

boolean readPin (int inputPin){
  //This function performs debouncing and input detection for 'pull low' type switches - i.e. with a pull-up resistor.
  int val = 0;                    // variable for reading the pin status
  int bounceCheck = 0;            // variable for debouncing
  
  val = digitalRead(inputPin);
  delay (10);
  bounceCheck = digitalRead(inputPin);
  if (val == bounceCheck) //If button really is pressed
  {
    if (val == LOW){ //If pin is being pulled low, button is pressed - 'pull up' resistor configuration
      delay(100); //Allows for button to be released without multiple triggering
      return true; //Return a value of true - button is being pressed
    } else {
      return false;
    }
  } else {
    return false;
  }
}

It’s adapted from the code in the ‘Button with debounce’ example by DojoDave, and is designed to work with buttons connected with a ‘pull-up’ resistor - i.e., low is when the button is being pushed. There is also a slight delay at the end, to allow for reaction time and release of the button without multiple triggers. The only argument is the pin to be read.

The second function is designed to work with one or two (daisy-chained) HC595 series shift register ICs. The number specified in ‘counter’ is the number of shift register outputs that will be turned on (i.e. all outputs up to that number). The other three arguments are the data, clock and latch pin for the shift register itself.

void registerCount(int counter, int curData, int curClock, int curLatch){
  byte bitsToSend = 0; //the bits to send to the lower shift register (lower 8)
  byte bitsToSend2 = 0; //the second byte to send to the upper shift register (upper 8 - 9-16)
  int upperCount = 0; //Counter for second byte
  
  digitalWrite(curLatch, LOW); //disable outputs while shifting bits out
  if (counter >8){
    upperCount = (counter-8);  //Determine how many higher bits there are
    for (int uR = 0; uR < upperCount; uR++){ //Set the bits in the upper byte accordingly
    bitWrite(bitsToSend2, uR, 1);
    }
    bitsToSend = 255; //Set lower byte to completely full
    shiftOut(curData, curClock, MSBFIRST, bitsToSend2); //Shift upper byte out first
    shiftOut(curData, curClock, MSBFIRST, bitsToSend); //Then shift lower byte out
  } else { //Else, counter will be lower than 8
    for (int lR = 0; lR < counter; lR++){ //Set the bits in the upper byte accordingly
    bitWrite(bitsToSend, lR, 1);
    }
    bitsToSend2 = B00000000;
    shiftOut(curData, curClock, MSBFIRST, bitsToSend2); //Shift upper byte out first
    shiftOut(curData, curClock, MSBFIRST, bitsToSend); //Then shift lower byte out
  }
  digitalWrite(curLatch, HIGH);
}

I have used this in conjunction with a timer (incrementing the counter value each time), to have an LED bar graph ‘count up’ over a given time period.

In a similar vein, I’ve also done a function to clear the contents of two daisy-chained HC595 shift registers. Having them connected up with the OE pin grounded and the MR pin tied to 5V, to save Arduino pins, means that some of the shift register outputs may still be high (from last time) when they’re turned on. This function just writes two bytes of 0 bits to clear these outputs.

void clearRegister(int clrData, int clrClock, int clrLatch){
  byte clearByte = B00000000;
  
  digitalWrite(clrLatch, LOW); //disable outputs while shifting bits out
  shiftOut(clrData, clrClock, MSBFIRST, clearByte); //Shift out a byte of 0s - clears first byte
  shiftOut(clrData, clrClock, MSBFIRST, clearByte); //Shift out a byte of 0s - clears second byte
  digitalWrite(clrLatch, HIGH); //enable outputs  
}

The three arguments are the data, clock and latch pins.

Cheers,
Tbdanny

I'd like to share them here, in case others may find them useful, too.

Thanks for sharing! It is nice to see new people giving back.

Recommendation is to avoid delay() as if it was the plague. Write code using millis (see blink without delay techniques).

Thanks. I'm sure people will find it useful. Be sure to bookmark this Thread so you can find it again yourself in a few weeks time.

It would probably be helpful if you could change the title to reflect the content. That way someone may be more likely to find it with a Google search. You can change the title by clicking modify in your original post.

Because of the diversity of the nature of the functions it may be a good idea to put each one in a Thread of its own with a specific title. They have a wider application than model railways.

...R

Actually, I was hoping that others would share their useful functions here too - that this could become a thread for an exchange of such functions.

Tbdanny: Actually, I was hoping that others would share their useful functions here too - that this could become a thread for an exchange of such functions.

See the Playground page of this site.

Tbdanny: Actually, I was hoping that others would share their useful functions here too - that this could become a thread for an exchange of such functions.

That's a good idea, but I think the reality of how the Forum is used gets in the way. Most Threads degenerate into nonsense pretty quickly :) And there are so many new Threads that yours will find itself on page 37 pretty quickly.

And you would be amazed how few people read other Threads so they would not know that your "repository" is waiting for them.

And I think if people are to be able to find functions some sort of index is needed - which the Forum does not have.

Propitiating Google is the best solution IMHO.

...R