~josealberto4444/arduino-quiz

ref: 047489fc3658192c94ebe42ae4a8c4864598d70c arduino-quiz/questionsAndAnswers/questionsAndAnswers.ino -rw-r--r-- 3.2 KiB
047489fc — josealberto4444 Ditch all delay functions in welcome 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

void scrollH(String message) {
  // Show a message scrolling horizontally along the first line of the screen
  String spaceFilling = "                ";
  message = spaceFilling + message + spaceFilling;
  for (int i = 0; i < message.length() - 14; i+=2) {
    lcd.setCursor(0, 0);
    lcd.print(message.substring(i, i+16));
    delay(400);
  }
}

void scrollV(String message) {
  // Show a message scrolling vertically along the screen
  String spaceFilling = "                ";
  message = spaceFilling + message + spaceFilling;
  for (int i = 0; i < message.length() - 16; i+=16) {
    lcd.setCursor(0, 0);
    lcd.print(message.substring(i, i+16));
    lcd.setCursor(0, 1);
    lcd.print(message.substring(i+16, i+32));
    delay(1000);
  }
}

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

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

void detectMotion() {
  motion = true;
}

void setup() {
  attachInterrupt(0, 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;

void loop() {
  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");
}