While this isn’t currently using Blynk, I think it could be easily enough added at a later time. This project started as a simple visual timer, and took an aggressive turn towards the complex as my creativeness added features my abilities couldn’t quite keep up with.
The idea is to use an LED tower so to speak that counts down from a preset time. Only one button is to be used. When the unit is OFF, pressing the button will enter the timer SET option where subsequent presses within a few seconds of each other will increment a counter which represents the time. After a few second of no input, the unit will switch to ON mode, where the countdown begins. If the button is pressed while the timer is ON, it will go into a cancel confirmation mode, at which time if the button is pressed again within another few seconds, the unit cancels the timer and changes back to OFF mode.
My code is early stage development, so there are things included that probably shouldn’t be, as well as parts that have yet to be written.
I feel like I’m taking the long way around accomplishing what I set out to do, so I’m asking for any suggestions or better/ more concise ways of doing it. Have a look, modify, use it for another project, make comments/suggestions or whatever.
const int buttonPin = 10;
const int setLedPin = 9;
const int cancelLedPin = 11;
const int dataPin = 5;
const int clockPin = 7;
const int latchPin = 6;
byte ledLowStatus = 255;
byte ledHighStatus = 255;
int buttonState = 0;
int lastButtonState = 1;
int buttonStateSet = 0;
//int ledState = LOW;
bool timerOFF = true;
bool timerSET = false;
bool timerON = false;
bool timerCANCEL = false;
unsigned long currentSetTime = 0;
unsigned long lastSetTime;
const int setTimeDelay = 300;
int setTimeCounter = 0;
unsigned long currentCancelTime = 0;
unsigned long lastCancelTime;
const int cancelTimeDelay = 300;
void updateRegister(){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, ledHighStatus);
shiftOut(dataPin, clockPin, MSBFIRST, ledLowStatus);
digitalWrite(latchPin, HIGH);
}
//////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(setLedPin, OUTPUT);
pinMode(cancelLedPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(dataPin,OUTPUT);
pinMode(clockPin,OUTPUT);
pinMode(latchPin,OUTPUT);
digitalWrite(setLedPin,LOW);
digitalWrite(cancelLedPin,LOW);
Serial.begin(9600);
Serial.println(ledLowStatus);
Serial.println(ledHighStatus);
updateRegister();
Serial.println("Timer Mode = OFF Startup");
}
////////////////////////////////////////////////////////////////////////
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH && timerOFF == true)
{
timerOFF = false;
timerSET = true;
lastSetTime = millis();
Serial.println("Timer Mode = SET");
setTime();
}
if (buttonState == LOW && lastButtonState == HIGH && timerON == true)
{
timerON = false;
timerCANCEL = true;
lastCancelTime = millis();
Serial.println("Timer Mode = CANCEL");
cancelTime();
}
///////////////////////////////////ADD next if statements here
lastButtonState = buttonState;
Serial.println("Loop Restarted");
delay(50);
}
//////////////////////////////////////////////////////////////////////////////////////////////
void setTime()
{
digitalWrite(setLedPin,HIGH);
currentSetTime = millis();
int lastButtonStateSet = LOW;
while(currentSetTime-lastSetTime < setTimeDelay)
{
buttonStateSet = digitalRead(buttonPin);
if (buttonStateSet == LOW && lastButtonStateSet == HIGH) //possibly add
{
setTimeCounter++;
lastSetTime = millis();
Serial.print("Set Time Counter = ");
Serial.println(setTimeCounter);
}
lastButtonStateSet = buttonStateSet;
currentSetTime = millis();
Serial.println(currentSetTime - lastSetTime);
}
timerSET = false;
timerON = true;
digitalWrite(setLedPin,LOW);
Serial.println("Timer Mode = ON");
startTime();
}
//////////////////////////////////////////////////////////////////////////////
void startTime()
{
Serial.println(setTimeCounter);
int newSetTimeCounter;
int i;
if (setTimeCounter > 8)
{
newSetTimeCounter = setTimeCounter - 8;
for (i=0; i<newSetTimeCounter; i++)
{
bitClear(ledHighStatus,i);
}
setTimeCounter -= newSetTimeCounter;
}
for(i=0; i<setTimeCounter; i++)
{
bitClear(ledLowStatus,i);
}
updateRegister();
setTimeCounter = 0;
lastButtonState = 0;
//timerON = false;
//timerOFF = true;
//Serial.println("Timer Mode = OFF");
//////////////////ADD what to do once timer is starte
}
////////////////////////////////////////////////////////////////
void cancelTime()
{
digitalWrite(cancelLedPin,HIGH);
currentCancelTime = millis();
int lastButtonStateCancel = LOW;
bool canceled = false;
while(currentCancelTime-lastCancelTime < cancelTimeDelay && canceled == false)
{
int buttonStateCancel = digitalRead(buttonPin);
if (buttonStateCancel == LOW && lastButtonStateCancel == HIGH)
{
ledHighStatus = 255;
ledLowStatus = 255;
updateRegister();
timerCANCEL = false;
timerOFF = true;
digitalWrite(cancelLedPin,LOW);
lastButtonState = LOW;
Serial.println("Timer Mode = OFF");
canceled = true;
}
lastButtonStateCancel = buttonStateCancel;
currentCancelTime = millis();
Serial.println(currentCancelTime - lastCancelTime);
}
digitalWrite(cancelLedPin,LOW);
timerON = true;
lastButtonState = LOW;
Serial.print(buttonState);
Serial.print(" ");
Serial.print(lastButtonState);
Serial.print(" ");
Serial.println(timerON);
}