~josealberto4444/arduino-quiz

ref: 1c0e82f4df40d98ac189499c0b4f792176ae5f0e arduino-quiz/quiz/quiz.ino -rw-r--r-- 9.0 KiB
1c0e82f4 — josealberto4444 Add pause and resume functions 2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright © 2018 José Alberto Orejuela García (josealberto4444)

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Additional permission under GNU AGPL version 3 section 7
// 
// If you modify this program, or any covered work, by linking or combining it
// with LiquidCrystal_I2C (or a modified version of that library), containing
// parts covered by the terms of LiquidCrystal_I2C's license, the licensors of
// this program grant you additional permission to convey the resulting work.

// Quiz
// 
// This program is intended to be used in an Arduino board (or compatible, or
// simulator). The purpose is to have a screen where some questions will appear,
// and answer them with a remote control.

#include <LiquidCrystal_I2C.h>  // Unlicensed, then non-free, so that's the reason for that additional permission in the preamble.
#include <IRremote.h>           // LGPL-2.1 (great, no problem).
// IMPORTANT: Tweak IRremote library for using timer1 instead of timer2, or it
// will conflict with tone function. In the case of Arduino Uno (or any
// ATmega328), this is done (at the moment of writing) uncommenting line 194 and
// commentind 195 in boarddefs.h file.

// Set-up variables
const byte motionPin = 2;          // Motion sensor pin.
LiquidCrystal_I2C lcd(0x27,16,2);  // LCD address and size.
const byte irrecvPin = 10;         // IR receptor pin.
IRrecv irrecv(irrecvPin);          // Initialisation.
decode_results results;

// State-recording variables
volatile bool motion = false;     // Is there any movement that should turn on the device?
bool screenState = false;         // Is the screen actually on?
bool welcomeToneState = false;    // Is the welcome tone sounding?
bool welcomeScreenState = false;  // Is the welcome screen playing?


// Timing variables
const unsigned short secondsToInactive = 7;
volatile unsigned long prevActiveMillis = 0;
unsigned long prevToneMillis = 0;
unsigned long prevScreenMillis = 0;
const byte toneStep = 100;
unsigned short screenStep = 300; // Not a byte because it is modified to a value of 2000

byte toneCounter = 0;
byte screenCounter = 0;


// Remote-command variabes
const unsigned long remoteSwitchScroll = 0x1fe48b7;
const unsigned long remotePause = 0x1fee817;


// Scrolling class
class Scroller {
  // Receive a message and scroll it through the screen
  // 
  // If scrolling horizontally, charStep is  the amount of characters in every
  // step.
  // 
  // TODO: Pause.
  // TODO: A kind of manual scrolling, using the remote to move the text.
  // TODO: Change in charStep and speed, saving it for every player.
  unsigned short counter;
  const String spaceFilling = "                ";
  String message;
  unsigned long prevMillis;
  unsigned short updateStep;
  byte charStep;
  char direct;
  
  public:
  Scroller(String msg, char drc) {
    message = spaceFilling + msg + spaceFilling;
    counter = 0;
    charStep = 2;
    direct = drc;
    if (direct == 'H') updateStep = 400;
    if (direct == 'V') updateStep = 1000;
  }
  
  void Update() {
    unsigned long currentMillis = millis();
    if (direct == 'H') { // Horizontal scrolling.
      if (counter == 0) {
        lcd.setCursor(0, 0);
        lcd.print(message.substring(counter, counter+16));
        counter += charStep;
        prevMillis = millis();
      } else {
        if (currentMillis - prevMillis >= updateStep) {
          lcd.setCursor(0, 0);
          lcd.print(message.substring(counter, counter+16));
          counter += charStep;
          prevMillis = millis();
          if (counter >= message.length() - 16 + charStep) {
            counter = 0;
          }
        }
      }
    } else if (direct == 'V') { // Vertical scrolling.
      if (counter == 0) {
        lcd.setCursor(0, 0);
        lcd.print(message.substring(counter, counter+16));
        lcd.setCursor(0, 1);
        lcd.print(message.substring(counter+16, counter+32));
        counter += 16;
        prevMillis = millis();
      } else {
        if (currentMillis - prevMillis >= updateStep) {
          lcd.setCursor(0, 0);
          lcd.print(message.substring(counter, counter+16));
          lcd.setCursor(0, 1);
          lcd.print(message.substring(counter+16, counter+32));
          counter += 16;
          prevMillis = millis();
          if (counter >= message.length()) {
            counter = 0;
          }
        }
      }
    }
  }
  
  void switchDirection() {
    if (direct == 'H') {
      direct = 'V';
      updateStep = 1000;
    } else if (direct == 'V') {
      direct = 'H';
      updateStep = 400;
      // Clear the second line of the screen.
      lcd.setCursor(0, 1);
      lcd.print(spaceFilling);
    }
  }
  
  void pauseOrResume () {
    if (isUpperCase(direct)) {
      direct |= B00100000; // To lower-case.
    } else {
      direct &= ~(B00100000); // To upper-case.
    }
  }
};


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





// Set-up
Scroller aMessage("This is quite a long message to show in such a tiny screen. =P", 'H');

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





void loop() {
  // Trigger welcoming messages if there is motion and the screen is off:
  if (motion && !(screenState)) {
    welcomeToneState = true;
    welcomeScreenState = true;
  }
  if (welcomeToneState) {
    // Welcome tone: beep-beep happening at background.
    unsigned long currentMillis = millis();
    if (toneCounter == 0) {
      tone(8, 4978, 40);
      toneCounter++;
      prevToneMillis = millis();
    } else if ((toneCounter == 1) && (currentMillis - prevToneMillis >= toneStep)) {
      tone(8, 4978, 40);
      toneCounter = 0;
      welcomeToneState = false;
    }
  }
  if (welcomeScreenState) {
    unsigned long currentMillis = millis();
    // Welcome screen: the screenlight blinks twice and then a "Hello!" message
    // is shown. This is splitted in several steps to be able to manage other
    // tasks (like the welcome tone) at the same time.
    if (screenCounter == 0) {
      lcd.display();
      lcd.backlight();
      screenState = true;
      screenCounter++;
      prevScreenMillis = millis();
    } else if (currentMillis - prevScreenMillis >= screenStep) {
      switch (screenCounter) {
        case 1:
          lcd.noBacklight();
          screenCounter++;
          prevScreenMillis = millis();
          break;
        case 2:
          lcd.backlight();
          screenCounter++;
          prevScreenMillis = millis();
        break;
        case 3:
          lcd.noBacklight();
          screenCounter++;
          prevScreenMillis = millis();
        break;
        case 4:
          lcd.backlight();
          lcd.setCursor(0, 0);
          lcd.print("Hello!");
          screenStep = 2000;
          screenCounter++;
          prevScreenMillis = millis();
        break;
        case 5:
          lcd.clear();
          screenStep = 300;
          screenCounter = 0;
          welcomeScreenState = false;
        break;
      }
    }
  }

  // Work only if the screen is on.
  if (screenState) {
    // If you are not welcoming, do your job.
    if (!(welcomeToneState || welcomeScreenState)) {
      aMessage.Update();
      if (irrecv.decode(&results)) {
        switch (results.value) {
          case remoteSwitchScroll:
            aMessage.switchDirection();
            prevActiveMillis = millis();
            break;
          case remotePause:
            aMessage.pauseOrResume();
            prevActiveMillis = millis();
            break;
        }
        irrecv.resume();
      }
    }
    
    // Check if it's already time to go to sleep.
    unsigned long currentMillis = millis();
    // Securely save a copy of the volatile variable as it's longer than a byte
    // (so it takes more than a cycle to save it and there can be reading
    // problems).
    detachInterrupt(digitalPinToInterrupt(motionPin));                        // First, stop interruptions
    unsigned long savedPrevActiveMillis = prevActiveMillis;                   // then save the volatile variable and
    attachInterrupt(digitalPinToInterrupt(motionPin), detectMotion, RISING);  // finally recall interruptions.
    if ((currentMillis - savedPrevActiveMillis)/1000 >= secondsToInactive) {
      // Go to sleep and switch all related state-variables.
      lcd.clear();
      lcd.noBacklight();
      lcd.noDisplay();
      screenState = false;
      motion = false;
    }
  }
}