I cant get my head around how to solve my issue here. I have 3 buttons to control LED brushlesh motor and relay.
Im using “OneButton.h” to support my functions here.
sw1 to turn on and off led with motor
sw2 and sw3 up and down speed of motor
I need another function where sw1 button double pressed LED will start blink for 60sec and also relay will stay on 60s and after 60s I need LED to stop blinking and stay on but relay go off.
the issue I have it that I dont know how to do it without delaying other functions. In final code will be more sensors and more buttons, so I need other part of code to be functional while LED is blinking.
void doubleclick1() here where I need to make this function to be
here is my code as an example
#include "OneButton.h"
OneButton SW1(2, true); // Setup a new OneButton on pin 2. ON/OFF
OneButton SW2(3, true); // Setup a new OneButton on pin 3. DOWN
OneButton SW3(4, true); // Setup a new OneButton on pin 4. UP
// constants won't change. Used here to set a pin number:
const int SW2_LED = 5; // the number of the Smoke_LED pin
int SW2_LED_State = LOW; // ledState used to set the Smoke_LED
const int RELAY = 6; // the number of the Smoke_LED pin
int RELAY_State = LOW; // ledState used to set the Smoke_LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 100; // interval at which to blink (milliseconds)
int ESC_S = 9; // ESC Signal Pin 9 ESC settings
int STATE = 1; // ESC settings
int Arming_Time = 0; // ESC settings
int Pulse = 1000; // ESC settings
int maxRot = 1850; // ESC settings
int minRot = 1300; // ESC settings
float RPM_S = 1300; // ESC settings
// setup code here, to run once:
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
// initialize the SW pin as an output:
pinMode(SW2_LED, OUTPUT); //LED settings
pinMode(ESC_S, OUTPUT); // ESC settings
for (Arming_Time = 0; Arming_Time < 500; Arming_Time += 1) // ESC settings
{
digitalWrite(ESC_S, HIGH); // Arming ESC settings
delayMicroseconds(1100); // Arming ESC settings
digitalWrite(ESC_S, LOW); // Arming ESC settings
delay(20 - (Pulse / 1000)); // Arming ESC settings
}
// link the button 1 functions.
SW1.attachClick(click1);
SW1.attachDoubleClick(doubleclick1);
// SW1.attachLongPressStart(longPressStart1);
// SW1.attachLongPressStop(longPressStop1);
// SW1.attachDuringLongPress(longPress1);
// link the button 2 functions.
SW2.attachClick(click2);
// SW2.attachDoubleClick(doubleclick2);
// SW2.attachLongPressStart(longPressStart2);
// SW2.attachLongPressStop(longPressStop2);
// SW2.attachDuringLongPress(longPress2);
// link the button 3 functions.
SW3.attachClick(click3);
// SW3.attachDoubleClick(doubleclick3);
// SW3.attachLongPressStart(longPressStart3);
// SW3.attachLongPressStop(longPressStop3);
// SW3.attachDuringLongPress(longPress3);
} // setup
// main code here, to run repeatedly:
void loop() {
// keep watching the push buttons:
SW1.tick();
SW2.tick();
SW3.tick();
Serial.println(RPM_S);
{
digitalWrite(ESC_S, HIGH);
delayMicroseconds(RPM_S);
digitalWrite(ESC_S, LOW);
}
{
delay(10);
} // loop
}
// click1
void click1() {
Serial.println("ON/OFF click");
{
// if the LED is off turn it on and vice-versa:
if (SW2_LED_State == LOW || RPM_S <= minRot ) {
SW2_LED_State = HIGH;
RPM_S = 1500;
} else {
SW2_LED_State = LOW;
RPM_S = minRot;
}
// set the LED with the ledState of the variable:
digitalWrite(SW2_LED, SW2_LED_State);
}
}
// click1
// click2
void click2() {
Serial.println("UP click");
{
// if the LED is off turn it on and vice-versa:
if (SW2_LED_State == HIGH ) {
RPM_S = constrain (RPM_S + 10, minRot, maxRot);
}
else {
RPM_S = constrain (RPM_S + 0, minRot, maxRot);
}
// set the LED with the ledState of the variable:
digitalWrite(ESC_S, HIGH);
delayMicroseconds(RPM_S);
digitalWrite(ESC_S, LOW);
}
}
// click2
// click3
void click3() {
Serial.println("DOWN click");
{
// if the LED is off turn it on and vice-versa:
if (SW2_LED_State == HIGH ) {
RPM_S = constrain (RPM_S - 10, minRot, maxRot);
}
else {
RPM_S = constrain (RPM_S + 0, minRot, maxRot);
}
// set the LED with the ledState of the variable:
digitalWrite(ESC_S, HIGH);
delayMicroseconds(RPM_S);
digitalWrite(ESC_S, LOW);
}
}
// doubleclick1
void doubleclick1() {
Serial.println(" doubleclick...");
// read the state of the pushbutton value:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (SW2_LED_State == LOW) {
SW2_LED_State = HIGH;
} else {
SW2_LED_State = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(SW2_LED, SW2_LED_State);
}
} // doubleclick1