// https://forum.arduino.cc/t/msp430f5529lp-double-click/1136526/2
// https://wokwi.com/projects/367172147215229953
// pins
#define RED_LED 5 // LED pin
#define GREEN_LED 3 // LED pin
#define buttonPin2 4
#define buttonPin4 2
// pushbuttons
int buttonPressLeft = 0;
int buttonPressRight = 0;
// states
bool RED_LED_state = HIGH;
bool GREEN_LED_state = HIGH;
// interval timers
int PERIOD = 500; // initial interval
int RED_LED_period = PERIOD;
int GREEN_LED_period = PERIOD;
// event timers
unsigned long RED_LED_previous_time = 0;
unsigned long GREEN_LED_previous_time = 0;
unsigned long currentMillis();
void setup() {
Serial.begin(115200);
welcome();
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
}
void loop() {
unsigned long currentTime = millis(); // start main timer
buttonPressRight = digitalRead(buttonPin2);
buttonPressLeft = digitalRead(buttonPin4);
if ( currentTime - RED_LED_previous_time >= RED_LED_period ) { // check event timer
RED_LED_state = !RED_LED_state; // change LED state
digitalWrite(RED_LED, RED_LED_state); // write to LED pin
RED_LED_previous_time = currentTime; //update new start timer
}
if ( currentTime - GREEN_LED_previous_time >= GREEN_LED_period ) { // check event timer
GREEN_LED_state = !GREEN_LED_state; // change LED state
digitalWrite(GREEN_LED, GREEN_LED_state); // write to LED pin
GREEN_LED_previous_time = currentTime; //update new start timer
}
if (!buttonPressRight) {
delay(150); // debounce the button
GREEN_LED_period = GREEN_LED_period - 50;
if (GREEN_LED_period > 199) {
Serial.print("RED ");
Serial.print(RED_LED_period);
Serial.print(" GREEN ");
Serial.println(GREEN_LED_period);
}
else {
GREEN_LED_period = PERIOD;
Serial.print("RED ");
Serial.print(RED_LED_period);
Serial.print(" GREEN ");
Serial.println(GREEN_LED_period);
}
}
if (!buttonPressLeft) {
delay(150); // debounce the button
RED_LED_period = RED_LED_period - 50;
if (RED_LED_period > 199) {
Serial.print("RED ");
Serial.print(RED_LED_period);
Serial.print(" GREEN ");
Serial.println(GREEN_LED_period);
}
else {
RED_LED_period = PERIOD;
Serial.print("RED ");
Serial.print(RED_LED_period);
Serial.print(" GREEN ");
Serial.println(GREEN_LED_period);
}
}
}
void welcome() {
Serial.print("Frequency for each LED starts at ");
Serial.print(PERIOD);
Serial.println("ms.");
Serial.println("Pressing either button reduces the LED frequency by 50ms.");
Serial.print("Reducing either LED frequency below 200ms resets frequency to ");
Serial.print(PERIOD);
Serial.println("ms.\n");
Serial.print("RED ");
Serial.print(RED_LED_period);
Serial.print(" GREEN ");
Serial.println(GREEN_LED_period);
}