Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Button press short-long
- #define Button1 A0
- #define Button2 A1
- #define Button3 A2
- #define Led1 A3
- #define Led2 A4
- #define Relay1 2
- #define Relay2 3
- unsigned int currentTimer1, currentTimer2;
- unsigned int timeTimer1=10, timeTimer2=15;
- unsigned long debounceDelay=50; // the debounce time; increase if the output flickers
- unsigned long longPressDelay=1000;
- unsigned long lastDebounceTime1=0; // the last time the output pin was toggled
- unsigned long lastDebounceTime2=0;
- unsigned long lastDebounceTime3=0;
- unsigned long currentMillis=0, previousMillis=0;
- bool readingButton1, readingButton2, readingButton3;
- bool buttonShort1=false, buttonShort2=false, buttonShort3=false;
- bool buttonLong1=false, buttonLong2=false, buttonLong3=false;
- bool buttonShortPress1=false, buttonShortPress2=false, buttonShortPress3=false;
- bool buttonLongPress1=false, buttonLongPress2=false, buttonLongPress3=false;
- bool reading1=true, reading2=true, reading3=true;
- bool Relay1Status=false, Relay2Status=false;
- bool Led1Status=false, Led2Status=false;
- void setup() {
- Serial.begin(115200);
- Serial.println("Start ...");
- pinMode (Button1, INPUT_PULLUP );
- pinMode (Button2, INPUT_PULLUP );
- pinMode (Button3, INPUT_PULLUP );
- pinMode (Led1, OUTPUT );
- pinMode (Led2, OUTPUT );
- pinMode (Relay1, OUTPUT );
- pinMode (Relay2, OUTPUT );
- }
- void loop() {
- currentMillis = millis();
- if (currentMillis - previousMillis >= 1000) { // Timer every 1 second due to tick() refresh
- previousMillis = currentMillis;
- tick();
- }
- buttonCheck1();
- buttonCheck2();
- buttonCheck3();
- if (buttonShort1==true and readingButton1==HIGH){
- reading1=false;
- buttonShort1=false;
- Serial.println("Button1 Short Press");
- Led1Status=false;
- if (Relay1Status==false) {
- Relay1Status=true;
- currentTimer1=timeTimer1;
- } else {
- Relay1Status=false;
- currentTimer1=0;
- }
- }
- if (buttonLong1==true){
- reading1=false;
- buttonLong1=false;
- Serial.println("Button1 Long Press");
- Relay1Status=true;
- Led1Status=true;
- currentTimer1=0;
- }
- if (buttonShort2==true and readingButton2==HIGH){
- reading2=false;
- buttonShort2=false;
- Serial.println("Button2 Short Press");
- Led2Status=false;
- if (Relay2Status==false) {
- Relay2Status=true;
- currentTimer2=timeTimer2;
- } else {
- Relay2Status=false;
- currentTimer2=0;
- }
- }
- if (buttonLong2==true){
- reading2=false;
- buttonLong2=false;
- Serial.println("Button2 Long Press");
- Relay2Status=true;
- Led2Status=true;
- currentTimer2=0;
- }
- if (buttonShort3==true and readingButton3==HIGH){
- reading3=false;
- buttonShort3=false;
- Serial.println("Button3 Short Press");
- Led1Status=false;
- Led2Status=false;
- if (Relay1Status==Relay2Status) {
- if (Relay1Status==false) {
- Relay1Status=true;
- currentTimer1=timeTimer1;
- Relay2Status=true;
- currentTimer2=timeTimer2;
- } else {
- Relay1Status=false;
- currentTimer1=0;
- Relay2Status=false;
- currentTimer2=0;
- }
- } else {
- Relay1Status=true;
- currentTimer1=timeTimer1;
- Relay2Status=true;
- currentTimer2=timeTimer2;
- }
- }
- if (buttonLong3==true){
- reading3=false;
- buttonLong3=false;
- Serial.println("Button3 Long Press");
- Led1Status=true;
- Relay1Status=true;
- currentTimer1=0;
- Led2Status=true;
- Relay2Status=true;
- currentTimer2=0;
- }
- digitalWrite(Relay1,Relay1Status);
- digitalWrite(Relay2,Relay2Status);
- digitalWrite(Led1,Led1Status);
- digitalWrite(Led2,Led2Status);
- }
- void tick() {
- Serial.print(currentTimer1);
- Serial.print(" - ");
- Serial.println(currentTimer2);
- if (currentTimer1 != 0) {
- currentTimer1--;
- if (currentTimer1 == 0) {
- Relay1Status=false;
- }
- }
- if (currentTimer2 != 0) {
- currentTimer2--;
- if (currentTimer2 == 0) {
- Relay2Status=false;
- }
- }
- }
- void buttonCheck1() {
- readingButton1 = digitalRead(Button1);
- if (readingButton1==LOW and reading1==true) {
- if ((millis() - lastDebounceTime1) > debounceDelay) {
- buttonShort1=true;
- }
- if ((millis() - lastDebounceTime1) > longPressDelay) {
- buttonLong1=true;
- buttonShort1=false;
- }
- }
- if (readingButton1==HIGH) {
- lastDebounceTime1=millis();
- reading1=true;
- }
- }
- void buttonCheck2() {
- readingButton2 = digitalRead(Button2);
- if (readingButton2==LOW and reading2==true) {
- if ((millis() - lastDebounceTime2) > debounceDelay) {
- buttonShort2=true;
- }
- if ((millis() - lastDebounceTime2) > longPressDelay) {
- buttonLong2=true;
- buttonShort2=false;
- }
- }
- if (readingButton2==HIGH) {
- lastDebounceTime2=millis();
- reading2=true;
- }
- }
- void buttonCheck3() {
- readingButton3 = digitalRead(Button3);
- if (readingButton3==LOW and reading3==true) {
- if ((millis() - lastDebounceTime3) > debounceDelay) {
- buttonShort3=true;
- }
- if ((millis() - lastDebounceTime3) > longPressDelay) {
- buttonLong3=true;
- buttonShort3=false;
- }
- }
- if (readingButton3==HIGH) {
- lastDebounceTime3=millis();
- reading3=true;
- }
- }
- /*
- void tmp() {
- readingButton1 = digitalRead(Button1);
- if (readingButton1==LOW ) {
- if ((millis() - lastDebounceTime1) > debounceDelay) {
- buttonShort1=true;
- }
- if ((millis() - lastDebounceTime1) > longPressDelay) {
- buttonLong1=true;
- buttonShort1=false;
- }
- } else {
- lastDebounceTime1=millis();
- if (buttonShort1==true) {
- buttonShortPress1=true;
- buttonShort1=false;
- }
- if (buttonLong1==true) {
- buttonLongPress1=true;
- buttonLong1=false;
- }
- }
- if (buttonShortPress1==true) {
- buttonShortPress1=false;
- Serial.println("Short Press");
- }
- if (buttonLongPress1==true) {
- reading1=true;
- //lastDebounceTime=millis();
- buttonLongPress1=false;
- Serial.println("Long Press");
- }
- }
- */
- // END
Add Comment
Please, Sign In to add comment