~josealberto4444/arduino-quiz

578263ad5f8af02e461ab9135b41047f198b2374 — josealberto4444 3 years ago 047489f
Add a timeout for becoming inactive

There is also a mock main task which is displaying the number of seconds since
the board was plugged in.
1 files changed, 55 insertions(+), 37 deletions(-)

M questionsAndAnswers/questionsAndAnswers.ino
M questionsAndAnswers/questionsAndAnswers.ino => questionsAndAnswers/questionsAndAnswers.ino +55 -37
@@ 27,87 27,105 @@ void scrollV(String message) {
}

volatile bool motion = false;
bool motionTone = false;
bool motionScreen = false;
bool welcomeToneState = false;
bool welcomeScreenState = false;
bool screenState = false;
const unsigned short secondsToInactive = 10;

void checkMotionState() {
  if (!(motionTone || motionScreen)) {
    motion = false;
  }
}
volatile unsigned long prevActiveMillis = 0;

void detectMotion() {
  motion = true;
  prevActiveMillis = millis();
}

const byte motionPin = 2;

void setup() {
  attachInterrupt(0, detectMotion, RISING);
  pinMode(motionPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(motionPin), detectMotion, RISING);
  lcd.init();
  lcd.noDisplay();
}

short int toneCounter = 0;
short int screenCounter = 0;
unsigned long previousToneMillis = 0;
unsigned long previousScreenMillis = 0;
short int toneStep = 100;
short int screenStep = 300;
byte toneCounter = 0;
byte screenCounter = 0;
unsigned long prevToneMillis = 0;
unsigned long prevScreenMillis = 0;
byte toneStep = 100;
unsigned short screenStep = 300;

void loop() {
  if (motion && !(motionTone || motionScreen)) {
    motionTone = true;
    motionScreen = true;
  if (motion && !(screenState)) {
    welcomeToneState = true;
    welcomeScreenState = true;
  }
  if (motionTone) {
  if (welcomeToneState) {
    unsigned long currentMillis = millis();
    if (toneCounter == 0) {
      tone(8, 4978, 40);
      toneCounter++;
      previousToneMillis = millis();
    } else if ((toneCounter == 1) && (currentMillis - previousToneMillis >= toneStep)) {
      prevToneMillis = millis();
    } else if ((toneCounter == 1) && (currentMillis - prevToneMillis >= toneStep)) {
      tone(8, 4978, 40);
      toneCounter = 0;
      motionTone = false;
      checkMotionState();
      welcomeToneState = false;
    }
  }
  
  if (motionScreen) {
  if (welcomeScreenState) {
    unsigned long currentMillis = millis();
    if (screenCounter == 0) {
      lcd.display();
      lcd.backlight();
      screenState = true;
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 1) && (currentMillis - previousScreenMillis >= screenStep)) {
      prevScreenMillis = millis();
    } else if ((screenCounter == 1) && (currentMillis - prevScreenMillis >= screenStep)) {
      lcd.noBacklight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 2) && (currentMillis - previousScreenMillis >= screenStep)) {
      prevScreenMillis = millis();
    } else if ((screenCounter == 2) && (currentMillis - prevScreenMillis >= screenStep)) {
      lcd.backlight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 3) && (currentMillis - previousScreenMillis >= screenStep)) {
      prevScreenMillis = millis();
    } else if ((screenCounter == 3) && (currentMillis - prevScreenMillis >= screenStep)) {
      lcd.noBacklight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 4) && (currentMillis - previousScreenMillis >= screenStep)) {
      prevScreenMillis = millis();
    } else if ((screenCounter == 4) && (currentMillis - prevScreenMillis >= screenStep)) {
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Hello!");
      screenStep = 2000;
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 5) && (currentMillis - previousScreenMillis >= screenStep)) {
      prevScreenMillis = millis();
    } else if ((screenCounter == 5) && (currentMillis - prevScreenMillis >= screenStep)) {
      lcd.clear();
      lcd.noBacklight();
      lcd.noDisplay();
      screenStep = 300;
      screenCounter = 0;
      motionScreen = false;
      checkMotionState();
      welcomeScreenState = false;
    }
  }

  if (screenState) {
    if (!(welcomeToneState || welcomeScreenState)) {
      lcd.setCursor(0, 0);
      lcd.print(millis()/1000);
    }
    unsigned long currentMillis = millis();
    detachInterrupt(digitalPinToInterrupt(motionPin));
    unsigned long savedPrevActiveMillis = prevActiveMillis;
    attachInterrupt(digitalPinToInterrupt(motionPin), detectMotion, RISING);
    if ((currentMillis - savedPrevActiveMillis)/1000 > secondsToInactive) {
      lcd.clear();
      lcd.noBacklight();
      lcd.noDisplay();
      screenState = false;
      motion = false;
    }
  }
  
  //scrollV("This is quite a long message to show in this tiny screen. =P");
  //scrollH("This is quite a long message to show in this tiny screen. =P");
}