#include <SoftwareSerial.h>
#include <Bounce2.h>
#define BUTTON1_PIN 5
#define BUTTON2_PIN 6
#define BUTTON3_PIN 7
#define BUTTON4_PIN 8
Bounce2::Button button1 = Bounce2::Button();
Bounce2::Button button2 = Bounce2::Button();
Bounce2::Button button3 = Bounce2::Button();
Bounce2::Button button4 = Bounce2::Button();
// define the SoftwareSerial object and their pins
SoftwareSerial SoftSerial(3, 2);
int out1State = LOW;
int out2State = LOW;
int out3State = LOW;
int out4State = LOW;
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
const long interval1 = 1000;
const long interval2 = 2000;
const long interval3 = 3000;
void setup() {
// start communication with baud rate 9600
SoftSerial.begin(9600);
button1.attach( BUTTON1_PIN, INPUT_PULLUP ); // USE EXTERNAL PULL-UP
button2.attach( BUTTON2_PIN, INPUT_PULLUP );
button3.attach( BUTTON3_PIN, INPUT_PULLUP );
button4.attach( BUTTON4_PIN, INPUT_PULLUP );
button1.interval(5);
button2.interval(5);
button3.interval(5);
button4.interval(5);
button1.setPressedState(LOW);
button2.setPressedState(LOW);
button3.setPressedState(LOW);
button4.setPressedState(LOW);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
// wait a moment to allow serial ports to initialize
delay(100);
}
void loop() {
button1.update();
button2.update();
button3.update();
button4.update();
if ( button1.pressed() ) {
out1State = !out1State; // SET outState TO THE OPPOSITE OF outState
//digitalWrite(LED_PIN,outState); // WRITE THE NEW outState
}
if ( button2.pressed() ) {
out2State = !out2State; // SET outState TO THE OPPOSITE OF outState
}
if ( button3.pressed() ) {
out3State = !out3State; // SET outState TO THE OPPOSITE OF outState
//digitalWrite(LED_PIN,outState); // WRITE THE NEW outState
}
if ( button4.pressed() ) {
out4State = !out4State; // SET outState TO THE OPPOSITE OF outState
}
unsigned long currentMillis1 = millis();
if (out1State == LOW) {
SoftSerial.println("pset 1 0");
SoftSerial.println("pset 3 0");
digitalWrite(led1, LOW);
}
else {
SoftSerial.println("pset 1 1");
SoftSerial.println("pset 3 1");
digitalWrite(led1, HIGH);
}
if (out2State == LOW) {
SoftSerial.println("pset 2 0");
SoftSerial.println("pset 4 0");
digitalWrite(led2, LOW);
}
else {
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1){
previousMillis1 = currentMillis1;
SoftSerial.println("pset 2 1");
SoftSerial.println("pset 4 1");
digitalWrite(led2, HIGH);}
}
if (out3State == LOW) {
SoftSerial.println("pset 5 0");
SoftSerial.println("pset 7 0");
digitalWrite(led3, LOW);
}
else {
unsigned long currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= interval2) {
previousMillis2 = currentMillis2;
SoftSerial.println("pset 5 1");
SoftSerial.println("pset 7 1");
digitalWrite(led3, HIGH);}
}
if (out4State == LOW) {
SoftSerial.println("pset 6 0");
SoftSerial.println("pset 8 0");
digitalWrite(led4, LOW);
}
else {
unsigned long currentMillis3 = millis();
if (currentMillis3 - previousMillis3 >= interval2) {
previousMillis3 = currentMillis3;
SoftSerial.println("pset 6 1");
SoftSerial.println("pset 8 1");
digitalWrite(led4, HIGH);}
}
delay(20);
}