I'm new at this and stuck on how to have text changing pages automatically, and the device waiting for a button press to move on to the next step.
I've got the code structured so each button press moves on to a new "scene", but know that using delays to have more than one page of text displayed kills the button from being respondable. I like the overall structure of the code, just need to figure out how to have more text refreshed in each scene while it waits for a button push.
Thanks. heres a snippet of a "scene":
void displayResults(){
while(scene == 4){
buttonCheck();
for(int j = 0; j < 17; j++){
// phase 1 text
lcd.setCursor(0, 0);
lcd.print("4 display results");
lcd.setCursor(0, 1);
lcd.print("");
}
}
lcd.clear(); // it's always good to clear the screen before movonh onto a new print
}
Heh, it was out of consideration to spare readers the clutter...here it is:
/*
@ www.DanielAndrade.net
*/
int button = 2;
int buttonLight = 3;
int val;//variable to read the pin status
int timer=10;//milliseconds we need to debounce the button
int val2;//variable to read pin status again after a short delay.
const int buttonPin = 2;
const int analogPin = 1; // the pin that the potentiometer is attached to for calibration
const int sayCount = 5; // the number of sayings to be read
int scene = 0; //defines which part of application to go to
//BUTTON COUNT
int debounceVal1; //variable to read the pin status
int debounceVal2; //variable to read pin status again after a short delay.
int debounceTimer = 10; //milliseconds we need to debounce the button
int buttonState; //this holds the state of the button
int statusCheck = 0;
//QUOTES
String quoteA[] = {"Sober.","Saying 2","Saying 3","Saying 4", "Saying 5"};
String quoteB[] = {"","Saying 2","Saying 3","Saying 4", "Saying 5"};
int drunkResult = 0;
//LEDS
const int ledButton = 3; // button pulsing
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const int ledBox = 5; // box illumination
int brightness2 = 0; // how bright the LED is
int fadeAmount2 = 5; // how many points to fade the LED by
#include <Adafruit_CharacterOLED.h>//include the OLED library
// initialize the library with the numbers of the interface pins
Adafruit_CharacterOLED lcd(OLED_V2, 6, 7, 8, 9, 10, 11, 12);
//-------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); // open serial at 9600 bps
lcd.begin(16, 2);// Initialize the LCD with 16 characters and 2 lines
pinMode (buttonPin,INPUT);
buttonState = digitalRead(button); //This is key to be read in setup because it is only read once
}
//-------------------------------------------------------------------------------------------
void loop() {
lcd.clear();
Serial.println(scene);
start();
Serial.println(scene);
introScreen();
Serial.println(scene);
readSensor();
Serial.println(scene);
displayResults();
Serial.println(scene);
}
//-------------------------------------------------------------------------------------------
void buttonCheck() {
val=digitalRead(button);//read the button
delay(timer);// time for button to stop bouncing.
val2=digitalRead(button);//read the button again
Serial.print("Val = ");
Serial.println(val);
if (val == val2){ // ensures no flicker in button press
if(val !=buttonState){ // if something has changed the button has been pressed
if (val==LOW){ // button has been pressed
if(scene > 3){
scene = 0;
}
scene++;
}
}
buttonState=val;// save the button state in the variable
}
}
//-------------------------------------------------------------------------------------------
void start() {
buttonCheck();
// for(int j = 0; j < 17; j++){
// lcd.setCursor(0, 0);
// lcd.print("Welcome");
// lcd.setCursor(0, 1);
// lcd.print("");
// }
}
//-------------------------------------------------------------------------------------------
void introScreen(){
while(scene == 1){
// lightButton(); //light up box
buttonCheck();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("Blow into sensor.");
lcd.setCursor(0, 1);
lcd.print("Push button when ready");
}
}
lcd.clear(); // it's always good to clear the screen before movonh onto a new print
}
//-------------------------------------------------------------------------------------------
void readSensor(){
while(scene == 2){
buttonCheck();
int sensorReading = analogRead(analogPin); //Take reading
Serial.println(""); //line break
Serial.println(sensorReading);
int drunkLevel = map(sensorReading, 0, 1023, 0, sayCount);
for (int thisLevel = 0; thisLevel < sayCount; thisLevel++) {
if (drunkLevel == thisLevel) {
Serial.println(quoteA[drunkLevel]); //does nothing bc its not the level
Serial.println(thisLevel);
Serial.println(drunkLevel);
// lcd.clear();
// for(int j = 0; j < 17; j++)
// {
// lcd.setCursor(0, 0);
// lcd.print(quoteA[thisLevel]);
// lcd.setCursor(0, 1);
// lcd.print(quoteB[thisLevel]);
// }
}
else {
}
}
drunkResult = drunkLevel;
lcd.clear();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("calculating...");
lcd.setCursor(0, 1);
lcd.print("");
}
delay(1000);
lcd.clear();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("alcohol");
lcd.setCursor(0, 1);
lcd.print("sensed");
}
delay(1000);
scene++;
}
lcd.clear();
}
//-------------------------------------------------------------------------------------------
void displayResults(){
while(scene == 3){
buttonCheck();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print(quoteA[drunkResult]);
lcd.setCursor(0, 1);
lcd.print(quoteB[drunkResult]);
}
delay(2000);
lcd.clear();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("call to action");
lcd.setCursor(0, 1);
lcd.print("call to action2");
}
delay(2000);
lcd.clear();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("Sorry, I have to");
lcd.setCursor(0, 1);
lcd.print("reset");
}
delay(2000);
lcd.clear();
for(int j = 0; j < 17; j++){
lcd.setCursor(0, 0);
lcd.print("waiting");
lcd.setCursor(0, 1);
lcd.print(".");
}
delay(2000);
scene++;
}
lcd.clear();
}
a couple things that I like to do, but it may just be me
void TooktoLong()
{
Serial.println("Time out - system reset");
soundSad();
LCDScreenupdate(" Sorry you "," took too long ");
}
void LCDScreenupdate(String line1, String line2)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);
}
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
int buttonpressed = 0;
while(buttonpressed < 1){
timer = millis();
buttonState[thisPin] = digitalRead(buttonPins[thisPin]);
buttonpressed += buttonState[thisPin];
}
if(timer - mil > 10000) {buttonpressed = 1;}
}
if(timer - mil < 10000){
if (step == 1) {
LCDScreenupdate(" Step 1 "," line 2");
delay(1500);
}
if (step == 2) {
LCDScreenupdate(" Step 2 "," line 2");
delay(1500);
}
if (step == 3) {
LCDScreenupdate(" Step 3 "," line 2");
step = 1;
delay(1500);
}
else
{
TooktoLong()
step =1;
delay(1500);
}
}
step++;
}
Something like that, its not perfect, but it should cut your code down, you probably want to do a case on the steps so that you
Thanks, the timeout definitely makes sense.
Not really sure where the second code comes in but do see the use of the Milli which I struggle with so will give it a shot.