//https://deepbluembedded.com/arduino-timer-calculator-code-generator/
// void startTimer1(unsigned int value)
// {
// TCCR1A = 0b00000000;
// TCCR1B = 0b00000100; //set to prescaler of 64
// TCCR1C = 0;
// TCNT1H = 0;
// TCNT1L = 0;
// OCR1A = value;
// TIMSK1 = _BV(OCIE1A);
// }
void startTimer1(unsigned int value)
{
TCCR2A = 0; // Init Timer2A
TCCR2B = 0; // Init Timer2B
TCCR2B |= B00000100; // Prescaler = 64
OCR2A = value; // Timer Compare2A Register
TIMSK2 |= B00000010; // Enable Timer COMPA Interrupt
}
int current_period = 250;
// ISR(TIMER1_COMPA_vect)
// {
// startTimer1(current_period); //start timer again so in 1 ms this function will be called
// //...toggle step pin state here, if needed
// digitalWrite(10, !digitalRead(10));
// }
ISR(TIMER2_COMPA_vect)
{
// OCR2A += 250; // Advance The COMPA Register
startTimer1(current_period);
// Handle The Timer Interrupt
digitalWrite(10, !digitalRead(10));
//...
}
const int yellowLEDpin = 12;
void blinkYellow(){
static unsigned long yellowLEDmillis = millis();
if (millis() - yellowLEDmillis > 1000) {
digitalWrite(yellowLEDpin, !digitalRead(yellowLEDpin));
yellowLEDmillis = millis();
}
}
void setup() {
// put your setup code here, to run once:
pinMode(10, OUTPUT);
pinMode(yellowLEDpin, OUTPUT);
startTimer1(250); //(1000 is one toggle per 500 microseconds, so 1 step per millisecond).
}
void loop() {
// put your main code here, to run repeatedly:
blinkYellow();
}