#include <TimerOne.h>
#define LED 13
#define BUTTON 2
//Interrupt Timer
volatile bool tick = false;
volatile unsigned long tickCounter = 0;
//Red Button
byte pressCounter = 0;
bool blinkFlag = false;
//LED-Stuff
//Loop-Stuff
unsigned long lastLoopAction = 0;
void setup()
{
Serial.begin(9600);
Timer1.initialize(10000); //10ms
Timer1.attachInterrupt(timeoutISR);
attachInterrupt(digitalPinToInterrupt(BUTTON), buttonISR, CHANGE);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void buttonISR()
{
Timer1.start();
tickCounter = 0;
}
void timeoutISR()
{
tick = true;
tickCounter ++;
static unsigned long lastAction = 0;
if((tickCounter - lastAction == 2) && (digitalRead(BUTTON) == LOW))
{
pressCounter++;
blinkFlag = true;
}
}
/*void blink (byte expectCounter, unsigned int onTime, unsigned int offTime)
{
static unsigned long lastAction = 0;
static bool ledOn = false;
static bool firstRun = true;
for (unsigned int i = 0; i < expectCounter; i++)
{
if(firstRun && !ledOn)
{
ledOn = true;
firstRun = false;
digitalWrite(LED, HIGH);
Serial.println("first Run");
}
else if((!ledOn) && (tickCounter - lastAction >= offTime))
{
ledOn = true;
lastAction = tickCounter;
digitalWrite(LED, HIGH);
}
if ((ledOn) && (tickCounter - lastAction >= onTime))
{
ledOn = false;
lastAction = tickCounter;
digitalWrite(LED, LOW);
}
}
Serial.println(lastAction);
}*/
void loop()
{
Serial.println(tickCounter);
static unsigned long lastAction = 0;
static bool firstRun = true;
if (blinkFlag == true)
{
static bool ledOn = false;
int onTime = 50;
int offTime = 40;
if(firstRun && !ledOn)
{
ledOn = true;
digitalWrite(LED, HIGH);
Serial.println("First On");
}
else if((!ledOn) && (tickCounter - lastAction >= offTime))
{
ledOn = true;
lastAction = tickCounter;
digitalWrite(LED, HIGH);
Serial.println("Delayed On");
}
if((ledOn) && (tickCounter - lastAction >= onTime))
{
ledOn = false;
lastAction = tickCounter;
digitalWrite(LED, LOW);
Serial.println("Delayed Off");
}
if(firstRun && (lastAction >= (onTime + offTime)))
{
firstRun = false;
Serial.println("First Over");
}
else if(lastAction >= (pressCounter*(onTime + offTime)))
{
lastAction = 0;
firstRun = true;
blinkFlag = false;
Serial.println("End");
}
}
//Serial.println(lastAction);
}
/*void blink (byte expectCounter, unsigned int onTime, unsigned int offTime)
{
static unsigned long lastAction = 0;
static bool ledOn = false;
//for (unsigned int i = 0; i < expectCounter; i++)
{
if((ledOn == false) && (tickCounter - lastAction >= offTime))
{
ledOn = true;
lastAction = tickCounter;
digitalWrite(LED, HIGH);
}
else if ((ledOn == true) && (tickCounter - lastAction >= onTime))
{
ledOn = false;
lastAction = tickCounter;
digitalWrite(LED, LOW);
}
}
Serial.println(lastAction);
}*/
/*void varDelay(byte interval, void(*action)(), bool prewait, int prewaitInterval)
{
static int firstRun = 1;
static unsigned long enterTime = 0;
if (firstRun)
{
if (prewait)
{
if (tickCounter - enterTime >= prewaitInterval)
{
enterTime = tickCounter;
action();
firstRun = false;
}
}
else
{
action();
enterTime = tickCounter;
firstRun = false;
}
}
if (!firstRun && tickCounter - enterTime >= interval)
{
enterTime = tickCounter;
action();
}
}*/