//74HC595
//You should now be able to see the 7-segment display cycle from 0 to F
//Email:[email protected]
//Website:www.sunfounder.com
//2015.5.7
// /*
const int latchPin = 12;//Pin connected to ST_CP of 74HC595
const int clockPin = 8;//Pin connected to SH_CP of 74HC595
const int dataPin = 11; //Pin connected to DS of 74HC595
const int clkPin= 13; //the clk attach to pin2
const int dtPin= 9; //the dt attach to pin3
const int swPin= 10 ;//the number of the button
int encoderVal = 0;
//display 0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F
int datArray[16] = {252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142};
int D[4] = {2,3,4,5};
void setup (){
//set pins to output
pinMode(latchPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(dataPin,OUTPUT);
for(int i = 0; i < 4; ++i){
pinMode(D[i],OUTPUT);
}
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600); // initialize serial communications at 9600 bps
}
void loop()
{
//loop from 0 to 256
int change = getEncoderTurn();//
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)//if button pull down
{
encoderVal = 0;
}
for (int i = 0; i < 4; ++i){
switch(i){
case 0:
digitalWrite(D[0],LOW);
digitalWrite(D[1],HIGH);
digitalWrite(D[2],HIGH);
digitalWrite(D[3],HIGH);
break;
case 1:
digitalWrite(D[1],LOW);
digitalWrite(D[0],HIGH);
digitalWrite(D[2],HIGH);
digitalWrite(D[3],HIGH);
break;
case 2:
digitalWrite(D[2],LOW);
digitalWrite(D[0],HIGH);
digitalWrite(D[1],HIGH);
digitalWrite(D[3],HIGH);
break;
case 3:
digitalWrite(D[3],LOW);
digitalWrite(D[0],HIGH);
digitalWrite(D[1],HIGH);
digitalWrite(D[2],HIGH);
break;
}
// }
// for(int num = 0; num < 16; num++){
digitalWrite(latchPin,LOW); //ground latchPin and hold low for as long as you are transmitting
shiftOut(dataPin,clockPin,MSBFIRST,datArray[encoderVal % 10]);
// //return the latch pin high to signal chip that it
// //no longer needs to listen for information
digitalWrite(latchPin,HIGH); //pull the latchPin to save the data
// delay(1000); //wait for a second
}
}
int getEncoderTurn(void)
{
static int oldA = HIGH; //set the oldA as HIGH
static int oldB = HIGH; //set the oldB as HIGH
int result = 0;
int newA = digitalRead(clkPin);//read the value of clkPin to newA
int newB = digitalRead(dtPin);//read the value of dtPin to newB
if (newA != oldA || newB != oldB) //if the value of clkPin or the dtPin has changed
{
// something has changed
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
// */
/*
// Pin definitions for the rotary encoder
#define CLK_PIN 13 // Rotary encoder clock pin
#define DT_PIN 9 // Rotary encoder data pin
#define SW_PIN 7 // Rotary encoder switch pin (optional)
// Pin definitions for 74HC595 shift register
#define DATA_PIN 11 // DS (Data pin)
#define CLOCK_PIN 8 // SHCP (Clock pin)
#define LATCH_PIN 12 // STCP (Latch pin)
// Store the current time in minutes (e.g., 0 = 00:00)
int timeInMinutes = 0;
// Segment codes for digits 0-9
const byte segmentCodes[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
// Rotary encoder state
int lastCLKState = LOW; // Last clock state
int currentCLKState = LOW; // Current clock state
void setup() {
// Initialize rotary encoder pins
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
pinMode(SW_PIN, INPUT_PULLUP);
// Initialize 74HC595 shift register pins
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
// Start serial monitor for debugging
//Serial.begin(9600);
}
void loop() {
// Read the state of the rotary encoder
currentCLKState = digitalRead(CLK_PIN);
if (currentCLKState != lastCLKState) {
// Check for rotation direction
if (digitalRead(DT_PIN) != currentCLKState) {
// Rotate clockwise: increase time
timeInMinutes++;
} else {
// Rotate counter-clockwise: decrease time
timeInMinutes--;
}
// Update the display every time the encoder moves
displayTime(timeInMinutes);
lastCLKState = currentCLKState;
}
// Check if button is pressed (optional, not used in this example)
if (digitalRead(SW_PIN) == LOW) {
// Do something on button press, e.g., reset time
}
delay(50); // Debounce delay
}
void displayTime(int timeInMinutes) {
// Calculate hours and minutes
int hours = timeInMinutes / 60;
int minutes = timeInMinutes % 60;
// Ensure the time is within a valid range
if (hours < 0) hours = 23;
if (minutes < 0) minutes = 59;
if (hours > 23) hours = 0;
if (minutes > 59) minutes = 0;
// Break the time into individual digits (4 digits total: HH:MM)
int digits[4];
digits[0] = hours / 10; // Tens digit of hours
digits[1] = hours % 10; // Ones digit of hours
digits[2] = minutes / 10; // Tens digit of minutes
digits[3] = minutes % 10; // Ones digit of minutes
// Display each digit on the 7-segment display
for (int i = 0; i < 4; i++) {
clearDisplay();
displayDigit(i, digits[i]);
delay(5); // Delay to create multiplexing effect
}
}
void displayDigit(int digit, int num) {
// Turn on the corresponding digit
digitalWrite(LATCH_PIN, LOW); // Begin latch operation
// Shift data for the current digit
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentCodes[num]);
// Set the corresponding digit's position
byte digitControl = 0b1111; // Turn off all digits by default
digitControl &= ~(1 << digit); // Activate the current digit
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, digitControl);
// Latch the data to the shift register
digitalWrite(LATCH_PIN, HIGH);
}
void clearDisplay() {
// Clear the 7-segment display by sending all segments off
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, 0x00); // Turn off all segments
// Disable all digits (turn off all digits)
byte offDigits = 0xFF;
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, offDigits);
digitalWrite(LATCH_PIN, HIGH); // Latch data
}
*/