Last active
February 26, 2019 18:23
-
-
Save glyons/7804ad8a82cda104a2082c35f48f2933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Display | |
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) | |
#include <SPI.h> // For SPI comm (needed for not getting compile error) | |
#include <Wire.h> // For I2C comm, but needed for not getting compile error | |
// LED Matrix | |
#include <Max72xxPanel.h> | |
// Add Encoder Support | |
#include <Encoder.h> | |
// PIN Definitions | |
// ======================================= | |
#define OLED_RESET 16 // Pin 15 -RESET digital signal | |
#define BEEPER_PIN D2 | |
#define ENCODER_PINA D4 | |
#define ENCODER_PINB D3 | |
#define ENCODER_PINBUTTON D0 | |
#define RELAY D8 | |
int CS = D6; | |
// Display LED Matrix | |
// D5 = CLK / SCK (WeMOS) | |
// D6 CS | |
// D7 = DIN / MOSI (WeMOS) | |
int numberOfHorizontalDisplays = 4; | |
int numberOfVerticalDisplays = 1; | |
Max72xxPanel matrix = Max72xxPanel(CS, numberOfHorizontalDisplays, numberOfVerticalDisplays); | |
// Timer | |
// https://playground.arduino.cc/Main/CountDownTimer | |
unsigned long Watch, _micro, timeMicros = micros(); | |
unsigned int Clock = 0, R_clock; | |
boolean Reset = false, Stop = false, Paused = false,IsInitial=true; | |
volatile boolean timeFlag = false; | |
int INITIAL_DURATION =10; // duration of development in seconds | |
int TIMER_INCREMENT = 1; // seconds | |
int TIMER_INCREMENT2 = 15; // seconds | |
int durationT = 0; | |
// Encoder Values | |
Encoder myEnc(ENCODER_PINA, ENCODER_PINB); | |
long encoderPrevValue = 0; | |
long encoderValue = 0; | |
bool buttonConfirm = false; | |
bool SetupTimerMode = true; | |
void setup() | |
{ | |
// Start Serial | |
Serial.begin(115200); | |
// LED Matrix Init | |
matrix.setIntensity(1); // Use a value between 0 and 15 for brightness | |
matrix.setRotation(0, 1); // The first display is position upside down | |
matrix.setRotation(1, 1); // The first display is position upside down | |
matrix.setRotation(2, 1); // The first display is position upside down | |
matrix.setRotation(3, 1); // The first display is position upside down | |
pinMode(RELAY, OUTPUT); | |
StartUpDisplay(); | |
StartUpSound(); | |
} | |
void loop() | |
{ | |
// CheckButtonPress(); | |
if (SetupTimerMode) | |
{ | |
digitalWrite(RELAY, LOW); | |
durationT = SetupCountdownTimer(TIMER_INCREMENT); | |
if (durationT==0 && IsInitial) | |
{ | |
durationT=INITIAL_DURATION; | |
} | |
SetTimer(durationT); | |
Serial.println(durationT); | |
} | |
else | |
{ | |
CountDownTimer(); // Run Timer | |
} | |
int seconds = ShowSeconds(); | |
int minutes = ShowMinutes(); | |
int microSeconds=ShowMicroSeconds(); | |
if (!Stop) | |
{ | |
digitalWrite(RELAY, HIGH); | |
if (TimeHasChanged()) | |
{ | |
UpdateDisplay(minutes, seconds,microSeconds, true); | |
} | |
// Interval Tasks | |
DoIntervalTask(minutes, seconds); | |
if (minutes==0 && seconds==59) | |
{ | |
LastminuteSound(); | |
} | |
} | |
else // Finished | |
{ | |
UpdateDisplay(0, 0,0,false); | |
digitalWrite(RELAY, LOW); | |
if (!buttonConfirm) | |
{ | |
// BeepSound(); | |
Clock=0; | |
//Reset Encoder | |
// myEnc.write(duration/TIMER_INCREMENT); | |
} | |
} | |
} | |
// ENCODER | |
// ========================== | |
int GetEncoderValue() | |
{ | |
long _newPosition = myEnc.read(); | |
if (_newPosition != encoderPrevValue) { | |
encoderPrevValue = _newPosition; | |
Serial.println(_newPosition); | |
encoderValue=_newPosition/4; | |
encoderValue=abs(encoderValue); | |
} | |
return encoderValue; | |
} | |
// BUTTON | |
// ========================== | |
void CheckButtonPress() | |
{ | |
int buttonState = digitalRead(ENCODER_PINBUTTON); | |
if (buttonState == LOW) { | |
DoButtonLogic(); | |
IsInitial=false; | |
} | |
} | |
void DoButtonLogic() | |
{ | |
if (SetupTimerMode) | |
{ | |
BeepSound(); | |
InitTimer(); | |
SetupTimerMode=false; // Start timer; | |
} | |
else | |
{ | |
// Running Mode | |
if (Stop) | |
{ | |
// Finished | |
InitTimer(); | |
buttonConfirm=true; | |
SetupTimerMode =true; | |
delay(500); | |
buttonConfirm=false; | |
} | |
else | |
{ | |
Serial.println("Button Press"); | |
// Pause / Play | |
// Paused = !Paused; | |
if (Paused) | |
{ | |
BeepSound(); | |
} | |
delay(500); | |
} | |
} | |
} | |
int SetupCountdownTimer(int increment) | |
{ | |
int totalSeconds = GetEncoderValue()*increment; | |
int mins = (totalSeconds / 60) % 60; | |
int secs= totalSeconds - mins*60; | |
UpdateDisplay(mins, secs, 0, false); | |
return totalSeconds; | |
} | |
// BEEPER SOUNDS | |
// ========================== | |
// Play tune on start up | |
void StartUpSound() | |
{ | |
tone(BEEPER_PIN, 2000, 400); | |
delay(600); | |
tone(BEEPER_PIN, 800, 400); | |
delay(600); | |
tone(BEEPER_PIN, 2000, 400); | |
} | |
void LastminuteSound() | |
{ | |
tone(D4,3000, 200); | |
} | |
// Default Beep | |
void BeepSound() | |
{ | |
tone(D4, 4200, 100); | |
} | |
// DISPLAY | |
// ========================== | |
void StartUpDisplay() | |
{ | |
Serial.print ("Startup"); | |
} | |
// Display the Countdown Time | |
void UpdateDisplay(int minutes, int seconds,int microSeconds, bool showArrows) | |
{ | |
// Debug | |
Serial.print(ShowHours()); | |
Serial.print(":"); | |
Serial.print(minutes); | |
Serial.print(":"); | |
Serial.print(seconds); | |
Serial.print(":"); | |
Serial.print(ShowMilliSeconds()); | |
Serial.print(":"); | |
Serial.println(ShowMicroSeconds()); | |
String tape ; | |
if (minutes==0) | |
{ | |
tape.concat(seconds); | |
tape.concat("."); | |
unsigned mS= (microSeconds / 10U) % 10; | |
tape.concat(mS); | |
tape.concat("s"); | |
} | |
else | |
{ | |
tape.concat(minutes); | |
tape.concat(":"); | |
tape.concat(seconds); | |
} | |
int wait = 10; // In milliseconds | |
int spacer = 1; | |
int width = 5 + spacer; // The font width is 5 pixels | |
int i = width * tape.length() - spacer; | |
matrix.fillScreen(LOW); | |
int letter = i / width; | |
int x = (matrix.width() - 1) - i % width; | |
int y = (matrix.height() - 8) / 2; // center the text vertically | |
while ( x + width - spacer >= 0 && letter >= 0 ) { | |
if ( letter < tape.length() ) { | |
matrix.drawChar(x, y, tape[letter], HIGH, LOW, 1); | |
} | |
letter--; | |
x -= width; | |
} | |
matrix.write(); // Send bitmap to display | |
} | |
// Tasks | |
// ========================== | |
// Change servo direction on every interval | |
void DoIntervalTask(int minutes, int seconds) | |
{ | |
// Add Interval Code in here | |
} | |
// Timer | |
// ========================== | |
boolean CountDownTimer() | |
{ | |
static unsigned long duration = 1000000; // 1 second | |
timeFlag = false; | |
if (!Stop && !Paused) // if not Stopped or Paused, run timer | |
{ | |
// check the time difference and see if 1 second has elapsed | |
if ((_micro = micros()) - timeMicros > duration ) | |
{ | |
Clock--; | |
if (Clock == 0) // check to see if the clock is 0 | |
Stop = true; // If so, stop the timer | |
// check to see if micros() has rolled over, if not, | |
// then increment "timeMicros" by duration | |
_micro < timeMicros ? timeMicros = _micro : timeMicros += duration; | |
} | |
if ((_micro = micros()) - timeMicros > 10000) | |
{ | |
timeFlag = true; | |
} | |
} | |
return !Stop; // return the state of the timer | |
} | |
void ResetTimer() | |
{ | |
SetTimer(R_clock); | |
Stop = false; | |
} | |
void InitTimer() | |
{ | |
Watch = micros(); // get the initial microseconds at the start of the timer | |
timeMicros = micros(); // hwd added so timer will reset if stopped and then started | |
Stop = false; | |
Paused = false; | |
} | |
void StopTimer() | |
{ | |
Stop = true; | |
} | |
void StopTimerAt(unsigned int hours, unsigned int minutes, unsigned int seconds) | |
{ | |
if (TimeCheck(hours, minutes, seconds) ) | |
Stop = true; | |
} | |
void PauseTimer() | |
{ | |
Paused = true; | |
} | |
void ResumeTimer() // You can resume the timer if you ever stop it. | |
{ | |
Paused = false; | |
} | |
void SetTimer(unsigned int seconds) | |
{ | |
// StartTimer(seconds / 3600, (seconds / 3600) / 60, seconds % 60); | |
Clock = seconds; | |
R_clock = Clock; | |
Stop = false; | |
} | |
int ShowHours() | |
{ | |
return Clock / 3600; | |
} | |
int ShowMinutes() | |
{ | |
return (Clock / 60) % 60; | |
} | |
int ShowSeconds() | |
{ | |
return Clock % 60; | |
} | |
unsigned long ShowMilliSeconds() | |
{ | |
return (_micro - Watch)/ 1000.0; | |
} | |
unsigned long ShowMicroSeconds() | |
{ | |
return _micro - Watch; | |
} | |
boolean TimeHasChanged() | |
{ | |
return timeFlag; | |
} | |
// output true if timer equals requested time | |
boolean TimeCheck(unsigned int hours, unsigned int minutes, unsigned int seconds) | |
{ | |
return (hours == ShowHours() && minutes == ShowMinutes() && seconds == ShowSeconds()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment