#include <SevSegShift.h>
// Sources:
// https://wokwi.com/projects/367007283236072449
// https://arduino.stackexchange.com/questions/28395/how-to-convert-bool-array-to-byte
// https://github.com/bridystone/SevSegShift/blob/ShiftRegister/examples/SevSegShiftOne_Counter/SevSegShiftOne_Counter.ino
// https://stackoverflow.com/questions/28887617/arduino-fill-array-with-values-from-analogread
// https://stackoverflow.com/questions/50028821/best-way-to-read-from-a-sensor-that-doesnt-have-interrupt-pin-and-requires-some/50032992#50032992
// 165 shift register > arduino pins (buttons/switches)
const int dataPin1 = 2; /* Q7 */
const int clockPin1 = 3; /* CP */
const int latchPin1 = 4; /* PL */
// Variables for the first 8 bit shift register (8x 7Segment)
int dataPin = 5; // DS
int latchPin = 6; // STCP
int clockPin = 7; // SHCP
// Variables for the second 8 bit shift register (8x LED)
int dataPin2 = 8; // DS
int latchPin2 = 9; // STCP
int clockPin2 = 10; // SHCP
// Variables for the 3 digit 7 segment display
int dataPin3 = 11; // DS
int latchPin3 = 12; // STCP
int clockPin3 = 13; // SHCP
int zeroOneSegments = 0;
/* 1 = number of shift registers there is only 1 Shiftregister
used for all Segments (digits are on Controller)
default value = 2 (see SevSegShift example)
*/
/* true = Digits are connected to Arduino directly
default value = false (see SevSegShift example)
*/
SevSegShift sevsegshift(dataPin3, clockPin3, latchPin3, 1, true); //Instantiate a seven segment controller object
void setup() {
// 165 shift resgister
pinMode(dataPin1, INPUT);
pinMode(clockPin1, OUTPUT);
pinMode(latchPin1, OUTPUT);
// https://forums.parallax.com/discussion/115896/74hc165-random-errors-on-initial-read
digitalWrite(clockPin1, LOW);
// Set pins for the first 8 bit shift register (8x 7segment)
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
// Set pins for the second 8 bit shift register (8x led)
pinMode(latchPin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
// Pins for the 3 digit 7 segment display 8 bit shift register
// is handled directly by the SevSegShift library
setupSevSeg();
//Serial.begin(115200);
}
void loop() {
//calcValue();
readButtonStates();
segmentLine();
ledLine();
sevSegWrite();
}
// ###############################
// Own methods/functions goes here
// ###############################
void setupSevSeg() {
byte numDigits = 3;
byte digitPins[] = {A1, A2, A3}; // These are the PINS of the ** Arduino **
byte segmentPins[] = {0, 1, 2, 3, 4, 5, 6, 7}; // these are the PINs of the ** Shift register **
bool resistorsOnSegments = true; // 'false' means resistors are on digit pins
byte hardwareConfig = COMMON_CATHODE; // See README.md for options
bool updateWithDelays = false; // Default 'false' is Recommended
bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
bool disableDecPoint = true; // Use 'true' if your decimal point doesn't exist or isn't connected
sevsegshift.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevsegshift.setBrightness(90);
}
void readButtonStates() {
// Always reset
zeroOneSegments = 0;
// Prepare
digitalWrite(latchPin1, LOW);
digitalWrite(latchPin1, HIGH);
// Read
//Serial.print("Bits: ");
for (int i = 0; i < 8; i++) {
int state = digitalRead(dataPin);
zeroOneSegments |= state << i;
digitalWrite(clockPin, HIGH); // Shift out the next bit
digitalWrite(clockPin, LOW);
}
//Serial.println(zeroOneSegments);
}
void segmentLine() {
// Take the latchPin low so
// the LEDs don't change while you're sending in bits
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, zeroOneSegments);
// Return the latch pin high to signal chip that it
// no longer needs to listen for information
digitalWrite(latchPin, HIGH);
}
void ledLine() {
// Take the latchPin low so
// the LEDs don't change while you're sending in bits
digitalWrite(latchPin2, LOW);
// Negate each bit with ~
shiftOut(dataPin2, clockPin2, MSBFIRST, ~zeroOneSegments);
// Return the latch pin high to signal chip that it
// no longer needs to listen for information
digitalWrite(latchPin2, HIGH);
}
void sevSegWrite() {
sevsegshift.setNumber(255 - zeroOneSegments);
sevsegshift.refreshDisplay(); // Must run repeatedly
}