Hello,
I've created a project using two Nextion intelligent displays and the EasyNextionLibrary, but I'm having difficulty getting one of the screens to switch pages. With the project, someone has to hit a button on the first screen (myNex1) to trigger a sound and random LED. After that, I want the second display (myNex2) to change from Page 0 (a black screen) to Page 1 which shows two buttons. The subject has to pick the correct button which is associated with the LED to trigger a reward via pump. The second screen will then switch back to Page0 and the button on the first screen will become active again.
Everything is working without the page switch (i.e., the second display always shows the two buttons).
Any help would be appreciated--code is shown below (right now the mechanism to check if the choice is correct/incorrect is commented out to make testing easier).
Thanks!
#include "EasyNextionLibrary.h";
EasyNex myNex(Serial1);
EasyNex myNex2(Serial2);
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int SpeakerPin = 8;
const int RedLEDPin = 22;
const int GreenLEDPin = 23;
const int PumpPin = 7;
// int PumpMillis = 0; //variable for reward pump on/off
int LightSequence[50]; //array with sequence of light events triggered by Start Button
int Choice[50]; //array with choice of the player
int Trial = 0; //number of trials
int NumCorrect = 0; //number of correct trials
int NumIncorrect = 0; //number of incorrect trials
enum class GameState : uint8_t {
STATE_IDLE,
STATE_CHOICE,
STATE_VERIFY,
STATE_CORRECT,
STATE_INCORRECT
};
GameState gameState;
//Declaring function to start game
void StartGame(){
myNex.NextionListen();
}
void Reward(){
digitalWrite(PumpPin,HIGH);
delay(2000);
digitalWrite(PumpPin,LOW);
Serial.println("Delivering Reward");
}
void trigger1(){ //Push small touchscreen button
int RandLED = 0;
RandLED = random(22,24);
Serial.print("Trial: ");
Serial.println(Trial + 1);
//Play sound (Pin, Hz of sound, duration)
tone(8, 262, 1000);
//store LED to trial index in LightSequence array
digitalWrite(RandLED, HIGH);
LightSequence[Trial] = RandLED;
Serial.print("Random LED from array: ");
Serial.println(LightSequence[Trial]);
gameState = GameState::STATE_CHOICE;
if(digitalRead(GreenLEDPin) == HIGH){
myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
myNex.writeStr("b0.txt", "GREEN"); // Set button b0 text to "ON"
}else if(digitalRead(RedLEDPin) == HIGH){
myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
myNex.writeStr("b0.txt", "RED"); // Set button b0 text to "ON"
}
Serial2.begin(9600);
myNex2.begin(9600);
myNex2.writeStr("page page1"); //Change from page 0 to page 1--screen with buttons
delay(50);
Serial.println("changing page");
Serial1.end();
}
//Declaring function for recording the choice of the player
void PlayerChoice(){
myNex2.NextionListen();
}
void trigger2(){
Serial.println("Red button pressed!");
int RedChoice = RedLEDPin;
Choice[Trial] = RedChoice;
Serial.print("Player Choice: ");
Serial.println(Choice[Trial]);
tone(8, 415, 1000);
Reward();
digitalWrite(LightSequence[Trial], LOW);
myNex2.writeStr("page page0"); //switch screen two back to page0--black screen
delay(50);
Serial2.end();
Serial1.begin(9600);
gameState = GameState::STATE_IDLE;
}
void trigger3(){
Serial.println("Green button pressed!");
int GreenChoice = GreenLEDPin;
Choice[Trial] = GreenChoice;
Serial.print("Player Choice: ");
Serial.println(Choice[Trial]);
tone(8, 370, 900);
Reward();
digitalWrite(LightSequence[Trial], LOW);
myNex2.writeStr("page page0"); //switch to page 0, black screen
delay(50);
Serial2.end();
Serial1.begin(9600);
gameState = GameState::STATE_IDLE;
}
// //declare function to verify choice as correct/incorrect
// void VerifyChoice(){
// if(LightSequence[Trial] == Choice[Trial]){
// Serial.println("Correct!");
// gameState = GameState::STATE_CORRECT;
// }else{
// Serial.println("Incorrect...");
// gameState = GameState::STATE_INCORRECT;
// }
// }
// //declare function when choice is correct
// void CorrectChoice(){
// Reward();
// digitalWrite(RedLEDPin,HIGH);
// digitalWrite(GreenLEDPin,HIGH);
// delay(1000);
// digitalWrite(RedLEDPin,LOW);
// digitalWrite(GreenLEDPin,LOW);
// Trial = Trial + 1;
// NumCorrect = NumCorrect + 1;
// gameState = GameState::STATE_IDLE;;
// }
// //declare function when choice is incorrect
// void IncorrectChoice(){
// Trial = Trial + 1;
// NumIncorrect = NumIncorrect + 1;
// gameState = GameState::STATE_IDLE;;
// }
void setup() {
Serial.begin(9600);
myNex.begin(9600);
myNex2.begin(9600);
pinMode(RedLEDPin,OUTPUT);
pinMode(GreenLEDPin,OUTPUT);
pinMode(PumpPin,OUTPUT);
gameState = GameState::STATE_IDLE;
}
void loop() {
switch(gameState){
case GameState::STATE_IDLE: //waiting for button push
StartGame();
break;
case GameState::STATE_CHOICE: //Player must choose RedButton or GreenButton, store in Choice array
PlayerChoice();
break;
// case GameState::STATE_VERIFY: //Check index of Choice versus index of LightSequence
// Serial.println("Checking guess");
// VerifyChoice();
// break;
// case GameState::STATE_CORRECT: //Player choice is correct, record # correct on LCD, trigger reward via Rexx
// Serial.println("Correct");
// CorrectChoice();
// break;
// case GameState::STATE_INCORRECT: //Player choice is incorrect, record # incorrect on LCD, no reward
// Serial.println("Incorrect");
// IncorrectChoice();
// break;
}
}