~josealberto4444/arduino-quiz

047489fc3658192c94ebe42ae4a8c4864598d70c — josealberto4444 2 years ago d5eaefd
Ditch all delay functions in welcome

Now, the melody and the screen blinking happen simultaneously when the motion
sensor is triggered, and most of the code is moved to the loop.
1 files changed, 75 insertions(+), 26 deletions(-)

M questionsAndAnswers/questionsAndAnswers.ino
M questionsAndAnswers/questionsAndAnswers.ino => questionsAndAnswers/questionsAndAnswers.ino +75 -26
@@ 2,7 2,7 @@

LiquidCrystal_I2C lcd(0x27,16,2);

void scrollHMessage(String message) {
void scrollH(String message) {
  // Show a message scrolling horizontally along the first line of the screen
  String spaceFilling = "                ";
  message = spaceFilling + message + spaceFilling;


@@ 13,7 13,7 @@ void scrollHMessage(String message) {
  }
}

void scrollVMessage(String message) {
void scrollV(String message) {
  // Show a message scrolling vertically along the screen
  String spaceFilling = "                ";
  message = spaceFilling + message + spaceFilling;


@@ 26,40 26,89 @@ void scrollVMessage(String message) {
  }
}

volatile bool motion = 0;
volatile bool motion = false;
bool motionTone = false;
bool motionScreen = false;

void detectMotion() {
  motion = true;
void checkMotionState() {
  if (!(motionTone || motionScreen)) {
    motion = false;
  }
}

void welcome() {
  tone(8, 4978, 40);
  delay(100);
  tone(8, 4978, 40);
  lcd.clear();
  lcd.noBacklight();
  delay(300);
  lcd.backlight();
  delay(300);
  lcd.noBacklight();
  delay(300);
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello!");
  delay(1000);
  motion = false;
void detectMotion() {
  motion = true;
}

void setup() {
  attachInterrupt(0, detectMotion, RISING);
  lcd.init();
  lcd.backlight();
  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;

void loop() {
  if (motion) welcome();
  delay(100);
//  scrollVMessage("This is quite a long message to show in this tiny screen. =P");
//  scrollHMessage("This is quite a long message to show in this tiny screen. =P");
  if (motion && !(motionTone || motionScreen)) {
    motionTone = true;
    motionScreen = true;
  }
  if (motionTone) {
    unsigned long currentMillis = millis();
    if (toneCounter == 0) {
      tone(8, 4978, 40);
      toneCounter++;
      previousToneMillis = millis();
    } else if ((toneCounter == 1) && (currentMillis - previousToneMillis >= toneStep)) {
      tone(8, 4978, 40);
      toneCounter = 0;
      motionTone = false;
      checkMotionState();
    }
  }
  
  if (motionScreen) {
    unsigned long currentMillis = millis();
    if (screenCounter == 0) {
      lcd.display();
      lcd.backlight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 1) && (currentMillis - previousScreenMillis >= screenStep)) {
      lcd.noBacklight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 2) && (currentMillis - previousScreenMillis >= screenStep)) {
      lcd.backlight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 3) && (currentMillis - previousScreenMillis >= screenStep)) {
      lcd.noBacklight();
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 4) && (currentMillis - previousScreenMillis >= screenStep)) {
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Hello!");
      screenStep = 2000;
      screenCounter++;
      previousScreenMillis = millis();
    } else if ((screenCounter == 5) && (currentMillis - previousScreenMillis >= screenStep)) {
      lcd.clear();
      lcd.noBacklight();
      lcd.noDisplay();
      screenStep = 300;
      screenCounter = 0;
      motionScreen = false;
      checkMotionState();
    }
  }
  //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");
}