Output of LED array from 74HC595

Switch (SW1) to shiftOut an LED sequence from a looping array, another switch (SW2) to halt the sequence and output whatever part of the array the program is on.

Overview

Explain each section of your project

        const int ledPin =  LED_BUILTIN;
int ledState = LOW;
const int SW1 = 2;
const int SW2 = 3;
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int count = 0;
int index = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
const long interval = 500;
volatile byte state = 0;
byte LED[] =
{
  B11111110,
  B11111101,
  B11111011,
  B11110111,
  B11101111,
  B11011111,
  B10111111,
  B01111111,
  B10111111,
  B11011111,
  B11101111,
  B11110111,
  B11111011,
  B11111101
};
void setup() {
  // put your setup code here, to run once:
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(SW1, INPUT);
//RISING to trigger when the pin goes from low to high
//BLINK is the function to call when the interrupt occurs
attachInterrupt(digitalPinToInterrupt(SW1),blinkSW1,RISING);
pinMode(SW2, INPUT);
//RISING to trigger when the pin goes from low to high
//BLINK is the function to call when the interrupt occurs
attachInterrupt(digitalPinToInterrupt(SW2),blinkSW2,RISING);
pinMode(ledPin, OUTPUT);
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,255);
digitalWrite(latchPin,HIGH);
}

void loop() {
blinkSW1;
blinkSW2;
}

void blinkSW1(){
while(state == 0) //this WHILE will only switch LED_BUILTIN state if different from current state
{
  delay(1);
  if(digitalRead(SW1)) {state=0;}
  if(digitalRead(SW2)) {state=1;}
  digitalWrite(ledPin,state);
}
  if(digitalRead(SW1)) {state=0;}
  if(digitalRead(SW2)) {state=1;}
  digitalWrite(ledPin,state);
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    digitalWrite(latchPin,LOW);
    shiftOut(dataPin,clockPin,MSBFIRST,LED[index]);
    digitalWrite(latchPin,HIGH);
    index++;
    if(index >= count)
    {
    index = 0;
    }
  }
}
void blinkSW2(){
while(state == 1) //this WHILE will only switch LED_BUILTIN state if different from current state
{
  delay(1);
  if(digitalRead(SW1)) {state=1;}
  if(digitalRead(SW2)) {state=0;}
  digitalWrite(ledPin,state);
}
  if(digitalRead(SW1)) {state=1;}
  if(digitalRead(SW2)) {state=0;}
  digitalWrite(ledPin,state);
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    digitalWrite(latchPin,LOW);
    shiftOut(dataPin,clockPin,MSBFIRST,LED[index]);
    digitalWrite(latchPin,HIGH);
    index++;
    if(index >= count)
    {
    index = 0;
    }
  }
}
    
Tom M

Categories