OK, found the problem: I was updating currentTimeact1 instead of previousTimeact1 in the button-press section. Seems to work now:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BTN_PRESSED HIGH //logic level of pin when button is pressed
// Pin assignement
const uint8_t ledPin = 7; //LED_BUILTIN; //built-in for debug
const uint8_t btnPin = 8;
//LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
LiquidCrystal_I2C lcd(0x3f, 16, 2);
enum fcnMode
{
OFF=0,
BLINK1,
BLINK2,
NBSTATE
}; // OFF = 0 and NBSTATE=7
uint8_t
ledState = LOW; // ledState used to set the led
uint8_t
btnLast;
uint8_t
funcState = OFF;
uint32_t
currentTime1,
currentTime2,
currentTimebtn,
currentTimeact1,
currentTimeact2; // will store current time
uint32_t
previousTime1,
previousTime2,
previousTimebtn,
previousTimeact1,
previousTimeact2; // will store last time led was updated
const uint32_t
PERIOD1 = 25000ul, // interval at which to blink (microseconds) = 20 Hz
PERIOD2 = 12500ul, // interval at which to blink (microseconds) = 40 Hz
PERIODact = 5000ul, // amount of time to keep blinking (milliseconds)
PERIODbtn = 50ul; // button read interval (milliseconds)
/******************************************************************\
SETUP
\******************************************************************/
void setup()
{
Serial.begin(9600); //debug
lcd.begin(16, 2); // LCD
pinMode(btnPin, INPUT);
btnLast = digitalRead( btnPin);
pinMode(ledPin, OUTPUT);
}
/******************************************************************\
Main Function of the code
\******************************************************************/
void loop()
{
buttonPressed();
setMode();
}
/******************************************************************
SUBFUNCTIONS
\******************************************************************/
void buttonPressed()
{
currentTimebtn = millis();
if (currentTimebtn - previousTimebtn >= PERIODbtn)
{
// push button delay to prevent fast toggling between funcstates when pressed
previousTimebtn = currentTimebtn;
uint8_t btnNow = digitalRead( btnPin );
if( btnNow != btnLast )
{
btnLast = btnNow;
if( btnNow == BTN_PRESSED )
{
funcState += 1;
if (funcState >= NBSTATE) //changed from '4'
funcState = 0;
//Serial.print( "Func state: " ); Serial.println( funcState );
lcd.clear();
switch( funcState )
{
case OFF:
digitalWrite(ledPin, LOW);
lcd.setCursor(4,0);
lcd.print("*Idle*"); // LCD main idle screen
break;
case BLINK1:
lcd.setCursor(2,0);
lcd.print("Mode: 20 Hz"); // LCD shows selected mode
previousTimeact1 = millis(); //activity timer
previousTime1 = micros(); //blink timer
break;
case BLINK2:
lcd.setCursor(2,0);
lcd.print("Mode: 40 Hz"); // LCD shows selected mode
previousTimeact2 = millis();
previousTime2 = micros();
break;
default:
break;
}//switch
}//if button is pressed
}//if now does not equal last
}//if time for a read
}//buttonPressed
void setMode()
{
// All Off
switch (funcState)
{
case OFF:
//do nothing
break;
case BLINK1:
blinkled1();
break;
case BLINK2:
blinkled2();
break;
}
}
void blinkled1()
{
currentTime1 = micros();
if( (currentTime1 - previousTime1) >= PERIOD1 )
{
// defines the blinking frequency
// save the last time you blinked the led
previousTime1 = currentTime1;
// if the led is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the led with the ledState of the variable:
digitalWrite(ledPin, ledState);
}//if
if( (millis() - previousTimeact1) >= PERIODact )
{
// checks if time after the start is equal or longer than 5 seconds to stop blinking
ReturnToIdleState();
}//if
}//blinkled1
void blinkled2()
{
currentTime2 = micros();
if( (currentTime2 - previousTime2) >= PERIOD2 )
{
// defines the blinking frequency
// save the last time you blinked the led
previousTime2 = currentTime2;
// if the led is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the led with the ledState of the variable:
digitalWrite(ledPin, ledState);
}//if
if( (millis() - previousTimeact2) >= PERIODact )
{
// checks if time after the start is equal or longer than 5 seconds to stop blinking
ReturnToIdleState();
}//if
}//blinkled2
void ReturnToIdleState( void )
{
lcd.clear();
funcState = OFF; //go back to iddle screen
digitalWrite(ledPin, LOW);
lcd.setCursor(4,0);
lcd.print("*Idle*"); // LCD main idle screen
}//ReturnToIdleState