OK, I'm assuming that posting my code will not have others criticize me for my programming prowess and solutions thus far.
As I said earlier, I'm here to learn and be educated. So any recommendations. Just be aware that it is not optimized but more a rough pass to prototype my project.
The full code is posted below for what is currently working. Here I have a lock timer (door example) and then you will see the start of a few variables that I was beginning labeled as relay (these would be the lights).
I know the speaker tones in the unlock function could be put in an array and loop it, i recently added just to hear a different tone when the unlocked function was called.
Note, this is all done today on an ESP8266 NodeMCU Development board with an external speaker, 1.5" OLED Display, door locking is via a 12v magnetic lock, and the future light on relay functions will be done via one or two relays.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(128, 64, &Wire, -1);
int LED2 = 16; // Assign LED1 to pin GPIO16
int SPEAKER = D7;
int MaxRandomNunber = 120; // Max Random Number in Minutes
int MinRandomNumber = 30; // Min Random Number in Minutes
int PWMA = D1; // Lock 1 on D1
int PWMB = D2; // Lock 2 on D2
int Relay1 = D3; // Relay #1 on D3
int Relay2 = D4; // Relay #2 on D3
unsigned long LockTimer = 60000; // LockTime in Milliseconds
int MinuteTimer = 10; // LockTime in Minutes
unsigned long DeviceStartTime; // Mills value at start of locking
int LockStatus = 0; // LockStatus 0= unlocked 1=locked
int Relay1Timer = 0; // Relay1 Timer Set to Zero
int Relay2Timer = 0; // Relay2 Timer Set to Zero
int Relay1Status = 0; // Relay1 Status Set to OFF
int Relay2Status = 0; // Relay2 Status Set to OFF
int MinutesRemaining = 0; // Be able to flash LED when ready to unlock
int counter = 0; // Loop Counter
int beeped = 0; // Varible to say if the beep was done.
//
// Start Setup Loop
//
void setup() {
Wire.begin(14,12); // Change I2C pins to D5 / D6
pinMode(LED2, OUTPUT); // Init Onboard LED
pinMode(SPEAKER, OUTPUT); // Init Speaker Output
pinMode(PWMA, OUTPUT); // Init Lock 1
pinMode(PWMB, OUTPUT); // Init Lock 2
pinMode(Relay1, OUTPUT); // Init Relay #1
pinMode(Relay2, OUTPUT); // Init Relay #2
Serial.begin(9600); // Enable serial logging
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Init the Display
display.clearDisplay(); // Clear the Display
MinuteTimer = getRandom(MinRandomNumber, MaxRandomNunber); // Generate Random Number based on MIN/MAX Assigned
LockTimer = MinuteTimer * 60000; // Set the duration from of lock from Minutes to Milliseconds
Relay1Timer = MinuteTimer;
Serial.print("Timer Amount:");
Serial.println(MinuteTimer);
DeviceStartTime = millis(); // This is the time the device started up in Milliseconds
digitalWrite(LED2, HIGH); // Set Locked LED Signal OFF
digitalWrite(Relay1, HIGH); // Init Relay1 off
digitalWrite(Relay2, HIGH); // Init Relay2 off
lock(); // Ater all settings, Start the Lock process
}
//
// The loop function runs forever
//
void loop() {
MinutesRemaining = ((LockTimer - millis())/60000+1); // Calculate and store thre Minutes locked remaining
display.clearDisplay(); // Clear the display in the loop
if (LockStatus == 1) { // If LockStatus is 1 display LOCKED message
display.setCursor(0,0); // Set Cursor to print message
display.print("LOCKED "); // Print "LOCKED" to OLED
display.setTextSize(2); // Setup Minutes Remaining Message
display.setTextColor(WHITE);
display.setCursor(0, 16);
display.print("Remain: ");
display.setCursor(82, 16);
display.print(MinutesRemaining);
} else { // If LockStatus is 0 Display UNLOCKED message
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0); // Set the cursor for the message
display.print("UNLOCKED"); // Print "UNLOCKED" to OLED
}
display.display();
if ((MinutesRemaining == 1) && (LockStatus == 1)) { // If the minutes remaining is 1 Flash the Onboard LED FAST
FlashLED(300);
}
if ((MinutesRemaining <= 5) && (MinutesRemaining > 1) && (LockStatus == 1)) { // If the minutes remaining is 5 Flash the Onboard LED SLOW
FlashLED(1000);
}
if ((millis() >= LockTimer) && (LockStatus == 1)) { // If the Lock Timer runs out AND the lock is locked, call unlock
unLock();
}
}
//
//
//
int getRandom(int min, int max) { // FUNCTION to generate the Random minutes between MIN/MAX values
int x;
x = random(MinRandomNumber,MaxRandomNunber);
return x;
}
//
//
//
void lock() { // FUNCTION to engage the lock(s)
digitalWrite(LED2, LOW); // Onboard LED On
digitalWrite(PWMA, HIGH); // Lock1 Locked
digitalWrite(PWMB, HIGH); // Lock2 Locked
LockStatus = 1; // Set Locked Status to TRUE (1)
Serial.println("LOCKED");
return;
}
//
//
//
void unLock() { // FUNCTION to unlock the lock(s)
digitalWrite(LED2, HIGH); // Onboard LED Off
digitalWrite(PWMA, LOW); // Lock1 Unlocked
digitalWrite(PWMB, LOW); // Lock2 Unlocked
LockStatus = 0; // Set Locked Status to FALSE (0)
tone(SPEAKER, 360);
delay(300);
tone(SPEAKER, 365);
delay(300);
tone(SPEAKER, 370);
delay(300);
tone(SPEAKER, 375);
delay(300);
tone(SPEAKER, 380);
delay(300);
tone(SPEAKER, 385);
delay(300);
noTone(SPEAKER); // Once unlocked stop speaker tone
return;
}
//
//
//
void FlashLED(int x) { // FUNCTION to Flash Onboard LED and beep Speaker (if used)
digitalWrite(LED2, (millis() / x ) % 2); // Toggle on or off the LED based on Modulus of Millis and Duration (x)
if (MinutesRemaining <= 1) { // One minute and below, warn with a beep if unable to see flash
if (((millis() / x ) % 2) == 0) {
tone(SPEAKER, 349);
} else noTone(SPEAKER);
}
return;
}