/*
Project: Four digit 7 segment counter
Description: Basic counter sketch
Creation date: 09/20/22
Author: AnonEngineering
https://wokwi.com/projects/343741010016207442
License: Beerware
*/
const int knobPin = A0; // Pot pin, brightness via PWM on the 74HC595 OE pin
const int clockPin = 4; // Clock pin of 74HC595 is connected to Digital pin 4
const int dataPin = 7; // Data pin of 74HC595 is connected to Digital pin 7
const int enablePin = 6; // Enable pin of 74HC595 is connected to PWM pin 6
const int latchPin = 5; // Latch pin of 74HC595 is connected to Digital pin 5
byte leds = 0; // Variable to hold the pattern of which LEDs are currently turned on or off
void setup() {
Serial.begin(9600);
// Set control pins of 74HC595 as OUTPUT
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(enablePin, OUTPUT);
// Set digit pins as OUTPUT / LOW
for(int i = 8; i <= 11; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
}
void loop() {
int pwmValue = analogRead(knobPin);
int mappedVal = map(pwmValue, 0, 1023, 0, 255);
Serial.println(mappedVal);
analogWrite(enablePin, mappedVal);
leds = 0; // Initially turns all the LEDs off, by giving the variable 'leds' the value 0
updateShiftRegister();
delay(50);
for (int i = 7; i >= 0; i--) // Turn all the LEDs ON one by one.
{
bitSet(leds, i); // Set the bit that controls that LED in the variable 'leds'
updateShiftRegister();
delay(250);
}
}
// Toggle Latch, shift bits in leds, toggle back
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
/*
void showDigits(int value) {
int digitsToDisplay = 0;
// Leading zero suppression, not great..
if(value < 10) {
digitsToDisplay = 0;
} else if(value < 100) {
digitsToDisplay = 1;
} else if(value < 1000) {
digitsToDisplay = 2;
} else {
digitsToDisplay = 3;
}
for(int digit = digitsToDisplay; digit >= 0; digit--) { // High digits first, higher power first
driveSegments(int(value/pow(10, digit)) % 10); // setup segment pins
if (digit == dpPos) digitalWrite(DP_PIN, LOW); // turn on DP?
digitalWrite(DIGIT_PINS[digit], HIGH); // turn digit on
digitalWrite(DIGIT_PINS[digit], LOW); // turn digit & DP off
digitalWrite(DP_PIN, HIGH);
}
}
void driveSegments(int digitVal) {
for (int segVal = 0; segVal < (sizeof(SEG_PINS) / sizeof(SEG_PINS[0])); segVal++) {
digitalWrite(SEG_PINS[segVal], digitSegVals[digitVal][segVal]);
}
}
void checkButtons() {
// Read the pushbutton one input pin:
int btn1State = digitalRead(RUN_PIN);
// Compare the button reading to its previous reading
if (btn1State != lastBtn1State) {
// If the reading has changed, update the state based on the reading
if (btn1State == LOW) {
// If the current reading is LOW then the button went from off to on:
stateOne = ! stateOne; //true; // set state one
Serial.println(F("Button one pressed"));
} else {
// If the current state is HIGH then the button went from on to off:
Serial.println(F("Button one released"));
}
// Save the current reading as the last reading, for next time through the loop
lastBtn1State = btn1State;
// Delay a little bit to avoid bouncing
delay(20);
}
// Read the pushbutton two input pin:
int btn2State = digitalRead(RST_PIN);
// Compare the button reading to its previous reading
if (btn2State != lastBtn2State) {
// If the reading has changed, update the state based on the reading
if (btn2State == LOW) {
// If the current reading is LOW then the button went from off to on:
stateTwo = ! stateTwo; //true; // set state two
Serial.println(F("Button two pressed"));
} else {
// If the current state is HIGH then the button went from on to off:
Serial.println(F("Button two released"));
}
// Save the current reading as the last reading, for next time through the loop
lastBtn2State = btn2State;
// Delay a little bit to avoid bouncing
delay(20);
}
}
*/