Hi there,
I have been developing a controller that has a 20x4 LCD on it, and I have been trying to integrate a game I developed in another file. I've pretty much just copy-pasted the values and bytes for characters and borders from the game into the controller code, and then turned the loop(); from the game into a function on the controller. I called the function "game," and it should work. The thing is, whenever I try to call it, it just won't run. I presume there may be something else in the code that may be stopping it from running, but I can't seem to find it.
To clarify, the code should configure the controller as a Bluetooth controller, until one of the buttons is pressed. Then, it starts the game.
Any help would be appreciated.
Sketch:
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <SoftwareSerial.h>
#include <Keypad.h>
#include <Arduino.h>
const byte ROWS = 4;
const byte COLS = 4;
SoftwareSerial BTSerial(10, 11);
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,20,4) for 20x4 LCD.
byte Line[] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100,
B00100
};
byte arrowf[] = {
B00100,
B01110,
B11111,
B11111,
B10101,
B00100,
B00100,
B00100
};
byte arrowb[] = {
B00100,
B00100,
B00100,
B10101,
B11111,
B11111,
B01110,
B00100
};
byte arrowr[] = {
B00000,
B11100,
B01110,
B11111,
B01110,
B11100,
B00000,
B00000
};
byte arrowl[] = {
B00000,
B00111,
B01110,
B11111,
B01110,
B00111,
B00000,
B00000
};
byte press[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
byte iggy[] = {
B00000,
B00000,
B10001,
B11111,
B10101,
B11111,
B01110,
B00000
};
byte gumgum[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B01111
};
byte timeblock[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
int VRx = A0;
int VRy = A1;
int SW = 4;
char hexaKeys[ROWS][COLS] = {
{15, 14, 13, 12},
{11, 10, 9, 8},
{7, 6, 5, 4},
{3, 2, 1, 0}
};
byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;
float player_line = 0;
float player_column = 0;
float item_column = 0;
float item_line = 0;
float left_column_boundary = 0;
float right_column_boundary = 0;
float top_line_boundary = 0;
float bottom_line_boundary = 0;
float score = 0;
unsigned long timepassed;
int running;
void intro (){
lcd.setCursor(1 - 1, 1 - 1);
lcd.print("SECRET MINI GAME:");
lcd.setCursor(1 - 1, 2 - 1);
lcd.print("Iggy's Bizarre");
lcd.setCursor(1 - 1, 3 - 1);
lcd.print("Adventure");
delay(2000);
lcd.clear();
}
void intro1 (){
lcd.clear();
lcd.setCursor(1 - 1, 1 - 1);
lcd.print("Collect All");
lcd.setCursor(1 - 1, 2 - 1);
lcd.print("Coffee Flavoured Gum");
delay(2000);
lcd.setCursor(1 - 1, 3 - 1);
lcd.print("You have 20 seconds");
lcd.clear();
}
void intro2 (){
lcd.clear();
lcd.setCursor(1 - 1, 1 - 1);
lcd.print("You have 20 seconds");
delay(2000);
lcd.clear();
}
void ending (){
lcd.clear();
lcd.setCursor(1 - 1, 1 - 1);
lcd.print("Game Finished");
lcd.setCursor(1 - 1, 2 - 1);
lcd.print(String("Score:") + String(String(String(score).toInt())));
delay(5000);
lcd.clear();
}
void redraw (){
timepassed = millis();
lcd.setCursor(player_column - 1, player_line - 1);
lcd.write(byte(6));
lcd.setCursor(item_column - 1, item_line - 1);
lcd.print("_");
lcd.setCursor(19 - 1, 1 - 1);
lcd.print(String(score).toInt());
lcd.setCursor(19 - 1, 2 - 1);
lcd.print(String((timepassed-7000)/1000));
delay(50);
}
void gamestart(){
intro();
intro1();
intro2();
item_line = random(1, 2 +1);
item_column = random(left_column_boundary, right_column_boundary +1);
redraw();
timepassed = millis();
if(timepassed<=27000){
while(!((player_line == item_line) && (player_column == item_column)))
{
if(timepassed<=20000){
redraw();
// MOVE UP
if(analogRead(A0+0) > 600){
if(player_line == bottom_line_boundary){
player_line += 1;
lcd.clear();
redraw();
}
}
// MOVE DOWN
if(analogRead(A0+0) < 300){
if(player_line == top_line_boundary){
player_line += -1;
lcd.clear();
redraw();
}
}
// MOVE LEFT
if(analogRead(A0+1) > 400){
if(player_column > left_column_boundary){
player_column += -1;
lcd.clear();
redraw();
}
}
// MOVE RIGHT
if(analogRead(A0+1) < 300){
if(player_column < right_column_boundary){
player_column += 1;
lcd.clear();
redraw();
}
}
}
else{
ending();
}
}
score += 1;
}
else{
ending();
}
}
void setup() {
// Initiate the LCD:
lcd.init();
lcd.backlight();
Serial.begin(9600);
BTSerial.begin(38400);
lcd.setCursor(6,0);
lcd.print("IGGY BOT");
lcd.setCursor(3,2);
lcd.print("Initializing");
delay(1000);
lcd.print(".");
delay(1000);
lcd.print(".");
delay(1000);
lcd.print(".");
delay(1000);
pinMode(VRx, INPUT);
pinMode(VRy, INPUT);
pinMode(SW, INPUT_PULLUP);
lcd.createChar(0, Line);
lcd.createChar(1, arrowf);
lcd.createChar(2, arrowb);
lcd.createChar(3, arrowr);
lcd.createChar(4, arrowl);
lcd.createChar(5, press);
lcd.createChar(6, iggy);
lcd.createChar(7, timeblock);
pinMode(A0+1,INPUT);
pinMode(A0+0,INPUT);
pinMode(4,OUTPUT);
left_column_boundary = 1;
right_column_boundary = 17;
bottom_line_boundary = 1;
top_line_boundary = 2;
player_line = 1;
player_column = left_column_boundary;
score = 0;
lcd.clear();
lcd.setCursor(18,0);
lcd.write(0);
lcd.setCursor(18,1);
lcd.write(0);
lcd.setCursor(18,2);
lcd.write(0);
lcd.setCursor(18,3);
lcd.write(0);
}
void loop() {
xPosition = analogRead(VRx);
yPosition = analogRead(VRy);
SW_state = digitalRead(SW);
int customKey = customKeypad.getKey();
if(customKey==15) {
game;
}
else { if (xPosition < 100){
lcd.print(" ");
lcd.setCursor(19,0);
lcd.write(1);
BTSerial.write('w');
}
else if (xPosition > 500){
lcd.print(" ");
lcd.setCursor(19,0);
lcd.write(2);
BTSerial.write('s');
}
else if (yPosition < 300){
lcd.print(" ");
lcd.setCursor(19,0);
lcd.write(3);
BTSerial.write('d');
}
else if (yPosition > 500){
lcd.print(" ");
lcd.setCursor(19,0);
lcd.write(4);
BTSerial.write('a');
}
else{
lcd.print(" ");
lcd.setCursor(19,0);
lcd.print(" ");
}}
delay(100);
}