Good Morning / Afternoon / Evening peeps!
I was wondering if I could get some (more) help. I have this piece of code that works as the master unit for a garage door opener. What happens is there is are a master and slave units that are connected via bluetooth (HC-05.) The master is in the garage and the slave is in the house. When I press a button on the slave, a relay at the master unit is closed for as long as the button is pressed. As it stands everything works great.
I recently redesigned the project to add another relay so when the button is pressed at the slave unit, the second relay at the master unit is closed for a pre-determined amount of time (turning a light on) and then opening again. I thought about using millis but I have no clue how to implement that, or if there's a better way. I don't think I need to have this controlled by the button at the slave, I thought that maybe when the reed switch opens or closes that it could trigger the second relay. Anyhoo below is my code and parts list. Fire any questions at me, I tried to be as thorough as possible. Thanks in advance.
Relay
Arduino Nano
HC-05 Bluetooth Module
TP4056 Charging Module
Buck Converter
Step-Up Converter
2N2222 Transistors
LED's
Diodes
Capacitors
Resistors
MASTER CODE
#include <TimerOne.h>
#include <SoftwareSerial.h>
#define redLed 6
#define greenLed 9
#define blueLed 10
#define GarSwitch 4
#define relay1 5
#define relay2 11
int garageState = 0;
int lastGarageState = 0;
char ch;
String HC05_Awake = "ON";
SoftwareSerial mySerial(7, 8); // Rx | Tx
void setup() {
Timer1.initialize(40000000);
Timer1.attachInterrupt(KeepAlive);
Serial.begin(115200);
mySerial.begin(38400);
pinMode(GarSwitch, INPUT_PULLUP);
lastGarageState = digitalRead(GarSwitch);
pinMode(relay, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(greenLed, HIGH);
}
int counter;
void loop() {
// Sending
// read input once
garageState = digitalRead(GarSwitch); // LOW = pressed
if (garageState != lastGarageState) {
if (garageState == LOW) { // switch got pressed
Serial.print(counter);
counter++;
Serial.println(" Print an 'a'");
mySerial.print('a');
digitalWrite(blueLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
} else { // switch got released
Serial.print(counter);
counter++;
Serial.println(" Print a 'c'");
mySerial.print('c');
digitalWrite(blueLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
}
}
if (mySerial.available()) {
char ch = mySerial.read();
Serial.write(ch);
if (ch == 'b') {
Serial.print(counter);
counter++;
Serial.println(" Print a 'b'");
mySerial.print('b');
digitalWrite(blueLed, LOW);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
digitalWrite(relay1, LOW);
delay(1000);
} else {
Serial.print(counter);
counter++;
Serial.println(" Print a 'd'");
mySerial.print('d');
digitalWrite(blueLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
digitalWrite(relay1, HIGH);
}
}
lastGarageState = garageState;
delay(20); // poor man's debouncing
}
void KeepAlive() {
if (HC05_Awake = "ON") {
mySerial.print('m');
Serial.print('m');
}
}
SLAVE CODE
// slave
# include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8); // Rx | Tx
# define redLed 5
# define greenLed 6
# define blueLed 10
# define Button 3
# define pwrLed 9
char ch;
int buttonState = 0;
int lastButtonState = 0;
int counter;
void setup() {
Serial.begin(115200);
mySerial.begin(38400);
pinMode(pwrLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(Button, INPUT_PULLUP);
analogWrite(pwrLed, 3);
}
void loop() {
// read input once
buttonState = digitalRead(Button); // LOW = pressed
if (buttonState != lastButtonState) {
mySerial.print('a');
if (buttonState == LOW) { // switch got pressed
Serial.print(counter);
counter++;
Serial.println(" Print a 'b'");
mySerial.print('b');
digitalWrite(blueLed, HIGH);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
} else { // switch got released
Serial.print(counter);
counter++;
Serial.print(" Print a 'd'");
mySerial.print('d');
digitalWrite(blueLed, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(redLed, HIGH);
}
} //...
// Receiving
if (mySerial.available()) {
char ch = mySerial.read();
Serial.write(ch);
if (ch == 'a') {
Serial.print(counter);
counter++;
Serial.println(" Print an 'a'");
analogWrite(redLed, 0);
analogWrite(greenLed, 50);
analogWrite(blueLed, 0);
}
else if (ch == 'c') {
Serial.print(counter);
counter++;
Serial.println(" Print a 'c'");
mySerial.print('c');
analogWrite(redLed, 255);
analogWrite(greenLed, 0);
analogWrite(blueLed, 0);
}
}
lastButtonState = buttonState;
delay(20); // poor man's debouncing
}