ronbentley1
Created February 1, 2022

Shift Registers - Tutorial 6, Q&As

A Q&A tutorial to explore the use of the ez_SIPO8_lib in greater depth.

BeginnerFull instructions provided164
Shift Registers - Tutorial 6, Q&As

Things used in this project

Story

Read more

Schematics

Single SIPO Shift Register, wiring diagram

Used throughout the tutorials

Code

Sketch Example – Clock Chasing Second LEDs

C/C++
Demonstration of use of ez_SIPO8_lib to simulate the chasing seconds of a clock face.
//
//   Chasing LEDs -
//   This sketch illuminates a defined sequential array of LEDs connected to
//   SIPO output pins, at a specified frequency (milliseconds).
//   At the end of the LED sequence, the LEDs are reset and the
//   cycle repeated.
//
//   The sketch can be configured for a SIPO array of any length, but this
//   example is configured to demonstrate the chasing LEDs of a clock dial.
//   In this instance the SIPO array length will be 64 output pins (8 x SIPOs)
//   but only the first 60 outputs will be used (0-59 seconds). The frequency
//   will be set to 1 second (1000 millisecs).
//
//   This example uses absolute addressing of the defined SIPO array.
//
//   Ron D Bentley, Stafford, UK
//   April 2021
//
//   This example and code is in the public domain and
//   may be used without restriction and without warranty.
//

#include <ez_SIPO8_lib.h>

#define Max_SIPOs  8 // 8 x SIPOs - provides 64 output pins
#define Max_timers 1 // used to count 1 second elapsing 'beats'

#define data_pin    8
#define clock_pin  10
#define latch_pin   9

#define chase_length  60   // chasing seconds on a clock
#define frequency     1000 // milli seconds - 1 second frequency

// initiate the class for max SIPOs/timers required
SIPO8 my_SIPOs(Max_SIPOs, Max_timers);

int my_LEDs;  // used to keep the SIPO bank id

void setup() {
  Serial.begin(9600);
  // create a bank of 'Max_SIPOs' using create_bank function:
  my_LEDs = my_SIPOs.create_bank(data_pin, clock_pin, latch_pin, Max_SIPOs);
  if (my_LEDs == create_bank_failure) {
    Serial.println(F("\nfailed to create bank"));
    Serial.flush();
    exit(0);
  }
  // start by setting all SIPO outputs to low (off)
  my_SIPOs.set_all_array_pins(LOW);
  my_SIPOs.xfer_array(MSBFIRST);
  // print the bank data for confirmation/inspection
  my_SIPOs.print_SIPO_data();
}

void loop() {
  uint8_t  next_pin = 0;
  my_SIPOs.SIPO8_start_timer(timer0); // kick off the timer
  do {
    if (my_SIPOs.SIPO8_timer_elapsed(timer0, frequency) == elapsed) {
      // 1 second time elapsed, so update next pin in the array
      my_SIPOs.SIPO8_start_timer(timer0); // restart 1 second count for next cycle
      if (next_pin == chase_length) { // wrap around
        my_SIPOs.set_all_array_pins(LOW); // clear all pins
        next_pin = 0;
      } else {
        my_SIPOs.set_array_pin(next_pin, HIGH); // set absolute next_pin pin status
        next_pin++;
      }
      my_SIPOs.xfer_array(MSBFIRST); // update physical SIPOs
    }
  } while (true);
}

Sketch Example - Using an 8-bit SIPO to Display Switch Status

C/C++
Displays status of six toggle switches (on or off) plus a heart beat monitor.
/*
   Ron D Bentley, Stafford, UK
   April 2021

   Example sketch 1 for Arduino Community story board
   The sketch reads a number of toggle switches which are in one of two
   states - on(HIGH) or off(LOW).
   Each switch is mapped to a SIPO bank pin as follows:
   switches 0-5 -> bank pins 0-5
                   bank pin 6 is not used
                   bank pin 7 is used as a sketch running indicator,
                              or 'heart beat'

   Note that the switches are 'read' periodically sampled by a dummy
   function using the SIPO8 timers feature. The code is non-blocking.

   This example and code is in the public domain and
   may be used without restriction and without warranty.

*/

#include <ez_SIPO8_lib.h>

#define Max_SIPOs        1
#define Max_timers       1
#define sample_time    500  // interval period between sampling sensors
#define num_switches     6  // number of sensors requiring testing/reading in each cycle

// initiate the class for max SIPOs/timers required
SIPO8 my_SIPOs(Max_SIPOs, Max_timers);

uint8_t bank_id;

// dummy read switch function - returns a random staus for given switch
// number, based on switch's current status
bool read_switch_status(uint8_t Switch) {
  randomSeed(analogRead(A0)*analogRead(A2)); // keep changing seed
  if (random(0, 4) == 0)return !my_SIPOs.read_bank_pin(bank_id, Switch);
  return my_SIPOs.read_bank_pin(bank_id, Switch);
}

void setup() {
  Serial.begin(9600);
  // create 1 bank of Max_SIPOs
  // params are data pin, clock pin anf latch pin, number SIPOs
  bank_id = my_SIPOs.create_bank(8, 10, 9, Max_SIPOs);
  if (bank_id == create_bank_failure) {
    Serial.println(F("failed to create bank"));
    Serial.flush();
    exit(0);
  }
  my_SIPOs.print_SIPO_data();  // report on global SIPO8 params
}
// Scan all defined switches and set their respective bank pin status, LOW/HIGH.
// Switches/bank pins layout:
// Switch associated bank pins run from 0 to num_switches-1, inclusive (5) and
// are used to indicate respective switch status,
// bank pin 6 is not used and bank pin 7 is used to provide
// a 'heart beat' to indicate that the sketch is running.
void loop() {
  my_SIPOs.set_all_array_pins(LOW); // ensure we start with clear array pool/bank
  my_SIPOs.xfer_array(MSBFIRST);
  my_SIPOs.SIPO8_start_timer(timer0); // start the sample timer
  do {
    if (my_SIPOs.SIPO8_timer_elapsed(timer0, sample_time) == elapsed) {
      my_SIPOs.SIPO8_start_timer(timer0); // reset/restart the timer
      // read each switch and set its virtual SIPO pin in the bank
      my_SIPOs.invert_bank_pin(bank_id, 7); // flash 'heart beat'
      for (uint8_t Switch = 0; Switch < num_switches; Switch ++) {
        my_SIPOs.set_bank_pin(bank_id, Switch, read_switch_status(Switch));
      }
      my_SIPOs.xfer_bank(bank_id, MSBFIRST); // update the physical SIPO pins
    }
  } while (true);
}

Credits

ronbentley1
25 projects • 13 followers
Contact

Comments

Please log in or sign up to comment.