Skip to content

Instantly share code, notes, and snippets.

@vschlegel
Last active May 29, 2018 16:49
Show Gist options
  • Save vschlegel/a93cfdce2a78ab183ab476cb7e3eb4ce to your computer and use it in GitHub Desktop.
Save vschlegel/a93cfdce2a78ab183ab476cb7e3eb4ce to your computer and use it in GitHub Desktop.
7 Segment test
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int enablePin = 10;
int decrementTime = 2; //minus
int startStop = 3; // start
int incrementTime = 4; //plus
bool runCd = false;
int numbers[11] = {0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111, 0b00000000};
unsigned long previousMillis = 0;
unsigned long countdown = 0; //countdown in sekunden
int tasterdelay = 500;
unsigned int ZEIT_BIS_EINE_SEKUNDE_FERTIG_IST =1000;
void setup() {
Serial.begin(115200);
pinMode(latchPin, OUTPUT); // RCK
pinMode(clockPin, OUTPUT); // SRCK
pinMode(dataPin, OUTPUT); // SerIn
pinMode(enablePin, OUTPUT); // G
pinMode(decrementTime, INPUT_PULLUP);
pinMode(startStop, INPUT_PULLUP);
pinMode(incrementTime, INPUT_PULLUP);
analogWrite(enablePin, 0); // Helligkeit (0 = max. ; 255 = aus)
digitalWrite(latchPin, HIGH); // Init
digitalWrite(latchPin, LOW);
writeToMatrix(0, 0, 0, 0);
countdown = 0;
}
void loop() {
if (digitalRead(decrementTime) == 0) { //reduziere Variable Countdown um 60 Sekunden, falls sie kleiner ist als 60 , auf null setzen
if (countdown >= 60) {
countdown = countdown - 60;
}
else {
countdown = 0;
}
convertCountdownToMatrix();
delay(tasterdelay);
}
if (digitalRead(incrementTime) == 0) {
countdown = countdown + 60;
Serial.println("Eingestellte zeit: ");
Serial.println(countdown);
convertCountdownToMatrix();
delay(tasterdelay);
}
if (digitalRead(startStop) == 0) {
runCd =!runCd;
Serial.println(runCd);
delay(tasterdelay);
}
if (runCd) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= ZEIT_BIS_EINE_SEKUNDE_FERTIG_IST) {
previousMillis = currentMillis;
// Code der sekündlich ausgeführt wird
if (countdown > 0) {
countdown--;
}
convertCountdownToMatrix();
if (countdown == 0) {
//wenn auf 0, solange nuller blinken lassen bis ein tater gedrückt wird
byte cnt=15;
while (digitalRead(startStop) == 1 && digitalRead(incrementTime) == 1 && digitalRead(decrementTime) == 1 && cnt>0) {
writeToMatrix(10, 10, 10, 10);
delay(100);
writeToMatrix(0, 0, 0, 0);
delay(100);
cnt--;
}
delay(300);
runCd=false;
}
Serial.print("Countdown ");
Serial.println(countdown);
}
}
}
void writeToMatrix(int a, int b, int c, int d) {
shiftOut(dataPin, clockPin, MSBFIRST, numbers[a]); // 1000er
shiftOut(dataPin, clockPin, MSBFIRST, numbers[b]); // 100er
shiftOut(dataPin, clockPin, MSBFIRST, numbers[c]); // 10er
shiftOut(dataPin, clockPin, MSBFIRST, numbers[d]); // 1er
digitalWrite(latchPin, HIGH); // geschriebene Daten anzeigen
digitalWrite(latchPin, LOW);
}
void convertCountdownToMatrix() {
//minuten splitten
int m = countdown / 60;
int mz = m / 10;
int me = m - mz * 10;
//sekunden splitten
int s = countdown - m * 60;
int sz = s / 10;
int se = s - sz * 10;
writeToMatrix(mz, me, sz, se);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment