I'm confused by the whole millis thing. I understand why it's better than delay in this instance, but I don't know how to implement it.
I tried doing this, but It would switch over to the next screen and back to the 1st before I could react, so something is definitely wrong with the millis function.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
//Timer
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long ScreenRefresh = 5000;
boolean ScreenTwo = LOW;
boolean doorState = LOW;
//Radio
int msg[1];
RF24 radio(2, 4);
const uint64_t pipes[2] = {
0xF0F0F0F000LL, 0xF0F0F0F0FFLL
};
//LCD
LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);
//Keypad
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {3, 5, 6, 7};
byte colPins[COLS] = {8, 9, 10};
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setChannel(100);
radio.setRetries(15, 15);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
//Timer
startMillis = millis();
//Screen 1
lcd.begin(16, 2);
MainMenu();
Serial.begin(9600);
}
void loop() {
//Timing
currentMillis = millis();
//Keypad
char customKey = customKeypad.getKey();
if (customKey) {
Serial.println(customKey);
}
//Radio Code
if (radio.available()) {
bool done = false;
while (!done) {
done = radio.read(msg, 1);
if (msg[0] == 111) {
delay(10);
doorState = HIGH;
}
else if (msg[0] == 112) {
doorState = HIGH;
}
}
}
// Screen 2
if (customKey == '*' ) {
currentMillis = millis();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("The Coop Door Is");
ScreenTwo = HIGH;
if (startMillis - currentMillis >= ScreenRefresh) {
ScreenTwo = LOW;
lcd.clear();
MainMenu();
}
}
if (ScreenTwo == HIGH) {
CoopDoor();
}
}
void CoopDoor() {
if (doorState == HIGH) {
lcd.setCursor(0, 1);
lcd.print("Open");
}
else if (doorState == LOW) {
lcd.setCursor(0, 1);
lcd.print("Closed");
}
}
void MainMenu () {
lcd.setCursor(0, 0);
lcd.print("* for Coop");
lcd.setCursor(0, 1);
lcd.print("# for Security");
}
Just FYI I also decide to make screen 1 a function called MainMenu, so I could go back to the start without having to press reset.