Arduino LCD game not displaying - c++
I am getting crazy on my Snake LCD game.
I use an Arduino Mega and I'd like to use the LCD screen and the PmodKYPD for my Snake game. The problem is that it will not display anything, no matter which key I will press.
Here is my code:
#include <LiquidCrystal.h>
#include "Keypad.h"
byte mySnake[8][8] =
{
{ B00000,
B00000,
B00011,
B00110,
B01100,
B11000,
B00000,
},
{ B00000,
B11000,
B11110,
B00011,
B00001,
B00000,
B00000,
},
{ B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B01110,
},
{ B00000,
B00000,
B00011,
B01111,
B11000,
B00000,
B00000,
},
{ B00000,
B11100,
B11111,
B00001,
B00000,
B00000,
B00000,
},
{ B00000,
B00000,
B00000,
B11000,
B01101,
B00111,
B00000,
},
{ B00000,
B00000,
B01110,
B11011,
B11111,
B01110,
B00000,
},
{ B00000,
B00000,
B00000,
B01000,
B10000,
B01000,
B00000,
}
};
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'0','F','E','D'}};
byte rowPins[ROWS] = {
37, 36, 35, 34}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
33,32, 31, 30}; // connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean levelz[5][2][16] = {
{{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false},
{false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}},
{{true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true},
{true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,true}},
{{true,false,false,false,true,false,false,false,false,false,false,true,false,false,false,true},
{true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,true}},
{{true,false,true,false,false,false,false,false,false,true,false,false,false,true,false,false},
{false,false,false,false,true,false,false,true,false,false,false,true,false,false,false,true}}
};
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
unsigned long time, timeNow;
int gameSpeed;
boolean skip, gameOver, gameStarted;
int olddir;
int selectedLevel,levels;
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
char key_read;
int oldkey=-1;
boolean x[16][80];
byte myChar[8];
byte nullChar[8] = { 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 };
boolean special;
struct partdef
{
int row,column,dir; //0 - up, 1 - down, 2 - right, 3 - left
struct partdef *next;
};
typedef partdef part;
part *head, *tail;
int i,j,collected;
long pc,pr;
void drawMatrix()
{
int cc=0;
if (!gameOver)
{
x[pr][pc] = true;
//for (i=0;i<8;i++) lcd.createChar(i, nullChar);
for(int r=0;r<2;r++)
{
for(int c=0;c<16;c++)
{
special = false;
for(int i=0;i<8;i++)
{
byte b=B00000;
if (x[r*8+i][c*5+0]) {b+=B10000; special = true;}
if (x[r*8+i][c*5+1]) {b+=B01000; special = true;}
if (x[r*8+i][c*5+2]) {b+=B00100; special = true;}
if (x[r*8+i][c*5+3]) {b+=B00010; special = true;}
if (x[r*8+i][c*5+4]) {b+=B00001; special = true;}
myChar[i] = b;
}
if (special)
{
lcd.createChar(cc, myChar);
lcd.setCursor(c,r);
lcd.write(byte(cc));
cc++;
}
else
{
lcd.setCursor(c,r);
if (levelz[selectedLevel][r][c]) lcd.write(255);
else lcd.write(254);
}
}
}
}
}
void freeList()
{
part *p,*q;
p = tail;
while (p!=NULL)
{
q = p;
p = p->next;
free(q);
}
head = tail = NULL;
}
void gameOverFunction()
{
delay(1000);
lcd.clear();
freeList();
lcd.setCursor(3,0);
lcd.print("Game Over!");
lcd.setCursor(4,1);
lcd.print("Score: ");
lcd.print(collected);
delay(1000);
}
void growSnake()
{
part *p;
p = (part*)malloc(sizeof(part));
p->row = tail->row;
p->column = tail->column;
p->dir = tail->dir;
p->next = tail;
tail = p;
}
void newPoint()
{
part *p;
p = tail;
boolean newp = true;
while (newp)
{
pr = random(16);
pc = random(80);
newp = false;
if (levelz[selectedLevel][pr / 8][pc / 5]) newp=true;
while (p->next != NULL && !newp)
{
if (p->row == pr && p->column == pc) newp = true;
p = p->next;
}
}
if (collected < 13 && gameStarted) growSnake();
}
void moveHead()
{
switch(head->dir) // 1 step in direction
{
case 0: head->row--; break;
case 1: head->row++; break;
case 2: head->column++; break;
case 3: head->column--; break;
default : break;
}
if (head->column >= 80) head->column = 0;
if (head->column < 0) head->column = 79;
if (head->row >= 16) head->row = 0;
if (head->row < 0) head->row = 15;
if (levelz[selectedLevel][head->row / 8][head->column / 5]) gameOver = true; // wall collision check
part *p;
p = tail;
while (p != head && !gameOver) // self collision
{
if (p->row == head->row && p->column == head->column) gameOver = true;
p = p->next;
}
if (gameOver)
gameOverFunction();
else
{
x[head->row][head->column] = true;
if (head->row == pr && head->column == pc) // point pickup check
{
collected++;
if (gameSpeed < 25) gameSpeed+=3;
newPoint();
}
}
}
void moveAll()
{
part *p;
p = tail;
x[p->row][p->column] = false;
while (p->next != NULL)
{
p->row = p->next->row;
p->column = p->next->column;
p->dir = p->next->dir;
p = p->next;
}
moveHead();
}
void createSnake(int n) // n = size of snake
{
for (i=0;i<16;i++)
for (j=0;j<80;j++)
x[i][j] = false;
part *p, *q;
tail = (part*)malloc(sizeof(part));
tail->row = 7;
tail->column = 39 + n/2;
tail->dir = 3;
q = tail;
x[tail->row][tail->column] = true;
for (i = 0; i < n-1; i++) // build snake from tail to head
{
p = (part*)malloc(sizeof(part));
p->row = q->row;
p->column = q->column - 1; //initial snake id placed horizoltally
x[p->row][p->column] = true;
p->dir = q->dir;
q->next = p;
q = p;
}
if (n>1)
{
p->next = NULL;
head = p;
}
else
{
tail->next = NULL;
head = tail;
}
}
void startF()
{
gameOver = false;
gameStarted = false;
selectedLevel = 1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select level: 1");
for(i=0;i<8;i++)
{
lcd.createChar(i,mySnake[i]);
lcd.setCursor(i+4,1);
lcd.write(byte(i));
}
collected = 0;
gameSpeed = 8;
createSnake(3);
time = 0;
}
void setup()
{
levels = 5; //number of lvls
lcd.begin(16, 2);
startF();
}
void loop()
{
if (!gameOver && !gameStarted)
{
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
olddir = head->dir;
if (key==1 && selectedLevel<levels) selectedLevel++;
if (key==2 && selectedLevel>1) selectedLevel--;
if (key==4)
{
lcd.clear();
selectedLevel--;
newPoint();
gameStarted = true;
}
else
{
lcd.setCursor(14,0);
lcd.print(selectedLevel);
}
}
}
}
}
if (!gameOver && gameStarted)
{
skip = false; //skip the second moveAll() function call if the first was made
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
olddir = head->dir;
if (key==0 && head->dir!=3) head->dir = 2;
if (key==1 && head->dir!=1) head->dir = 0;
if (key==2 && head->dir!=0) head->dir = 1;
if (key==3 && head->dir!=2) head->dir = 3;
if (olddir != head->dir)
{
skip = true;
delay(1000/gameSpeed);
moveAll();
drawMatrix();
}
}
}
}
if (!skip)
{
timeNow = millis();
if (timeNow - time > 1000 / gameSpeed)
{
moveAll();
drawMatrix();
time = millis();
}
}
}
if(gameOver)
{
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
key_read = keypad.getKey(); // get key press
key = key_read - '0';
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
startF();
}
}
}
}
}
Does anyone know something about this issue?
Related
Trying to make a custom shortcut keyboard with esp32 with arduino ide
Ok, so as in the title I am trying to make a custom shortcut keyboard with esp32 with Arduino ide and keep running into errors so if someone could fix it all for me that would be appreciated. one of these errors is: no return statement in function returning non-void [-Werror=return-type] Code: #include <BleKeyboard.h> BleKeyboard bleKeyboard; const int buttonPin[] = {36, 39, 34, 35, 32, 33}; int pinCount = 6; int potPin = 2; int prevPotState = -1; int potState = -1; int potTolerance = 1; long potDebounceDelay = 20; int buttonState[] = {1, 1, 1, 1, 1, 1}; int prevButtonState[] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH}; long startedPressing[] = {0, 0, 0, 0, 0, 0}; boolean longPressing[] = {false, false, false, false, false, false}; long lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0}; // 1 more for the pot long debounceDelay = 0; boolean testHardware = false; int keyComb(char key1 = 0, char key2 = 0, char key3 = 0, char key4 = 0) { if (key1 != 0) { bleKeyboard.press(key1); } if (key2 != 0) { bleKeyboard.press(key2); } if (key3 != 0) { bleKeyboard.press(key3); } if (key4 != 0) { bleKeyboard.press(key4); } delay(100); bleKeyboard.releaseAll(); } int sendLine(char const * line) { bleKeyboard.print(line); delay(750); keyComb(KEY_RETURN); } // Output actions. Probably the only part that you need to change int outputAction(int currentButton, int typeOfPress = 0) { // typeOfPress 1: on push; 2: on release; 3: on long press; 4: on lingering press. // actions on release, on long press and lingering press include the action press. Action lingering press cancels action release and long press. if (testHardware) { bleKeyboard.print(currentButton + 1); if (typeOfPress == 1) { bleKeyboard.print(" pressed "); } if (typeOfPress == 2) { bleKeyboard.print(" released "); } if (typeOfPress == 3) { bleKeyboard.print(" long "); } if (typeOfPress == 4) { bleKeyboard.print(" lingering "); } bleKeyboard.print(millis()); bleKeyboard.print("Pressed: "); bleKeyboard.print(lastDebounceTime[currentButton]); keyComb(KEY_RETURN); } else { if (currentButton + 1 == 1) { if (typeOfPress == 1) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'M'); } if (typeOfPress == 3) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'S'); } } if (currentButton + 1 == 2) { if (typeOfPress == 1) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'P'); } } if (currentButton + 1 == 3) { if (typeOfPress == 1) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'M'); } } if (currentButton + 1 == 4) { if (typeOfPress == 2) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'L'); } } if (currentButton + 1 == 5) { if (typeOfPress == 1) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'K'); } } if (currentButton + 1 == 6) { if (typeOfPress == 1) { keyComb(KEY_LEFT_CTRL, KEY_LEFT_ALT, KEY_LEFT_SHIFT, 'N'); } } } } void setup() { Serial.begin(115200); for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { pinMode(buttonPin[thisPin], INPUT); analogWrite(buttonPin[thisPin], HIGH); // In some versions use INPUT_PULLUP to use the built-in pull up resistor } bleKeyboard.begin(); } void loop() { for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) { buttonState[thisPin] = analogRead(buttonPin[thisPin]); // HIGH = state 1 <- button not pressed // LOW = state 0 <- button pressed // On longer press if ((startedPressing[thisPin] == 0) || ((millis() - startedPressing[thisPin]) <= 1200)) { // Debouncing not working properly with current hardware //if (((buttonState[thisPin] != prevButtonState[thisPin])) && ((millis() - lastDebounceTime[thisPin]) > debounceDelay)) { if ((buttonState[thisPin] != prevButtonState[thisPin])) { if (buttonState[thisPin] == 0) { // Standard press action startedPressing[thisPin] = millis(); outputAction(thisPin, 1); } else { if (!longPressing[thisPin]) { if ((millis() - startedPressing[thisPin]) < 500) { // On release (to avoid standard action if is incompatible with Long or Longer action) outputAction(thisPin, 2); } else { // Long action (+standard action already sent) outputAction(thisPin, 3); } } startedPressing[thisPin] = 0; longPressing[thisPin] = false; } lastDebounceTime[thisPin] = millis(); } } else { outputAction(thisPin, 4); longPressing[thisPin] = true; startedPressing[thisPin] = 0; } prevButtonState[thisPin] = buttonState[thisPin]; } // The pot int thisPin = pinCount; // To consider it the last one in the lastDebounceTime array potState = (int) (analogRead(potPin) / 6); if (prevPotState == -1) { prevPotState = potState; } if (((potState > prevPotState + potTolerance) || (potState < prevPotState - potTolerance)) && ((millis() - lastDebounceTime[thisPin]) > potDebounceDelay) ) { if (potState > prevPotState) { keyComb(KEY_UP_ARROW); } else { keyComb(KEY_DOWN_ARROW); } lastDebounceTime[thisPin] = millis(); prevPotState = potState; } } !!!FILLER!!! hehehuheuheuheuhuehdkl;lbkljdfklbfklvjnbgkljnjbgvnjkvjbkvfnvkljklbjk
From the declaration of your function, you declare the function can return an integer in the code. But you don't return an integer. For your example, int sendLine(char const * line) { bleKeyboard.print(line); delay(750); keyComb(KEY_RETURN); } You don't return an integer in the function sendLine. Another function you declared to have the same issue too.
LNK2019 error; what is missing/not defined here? [duplicate]
This question already has answers here: What is an undefined reference/unresolved external symbol error and how do I fix it? (39 answers) Closed 3 years ago. When I try to build out my progam I get this error: error LNK2019: unresolved external symbol "bool __cdecl TextGame::spaceVoid(struct TextGame::Position const &,struct TextGame::RoomData const &)" (?spaceVoid#TextGame##YA_NABUPosition#1#ABURoomData#1##Z) referenced in function "void __cdecl TextGame::UpdateGame(struct TextGame::PlayerState &,struct TextGame::WorldState &)" (?UpdateGame#TextGame##YAXAAUPlayerState#1#AAUWorldState#1##Z) This is for a basic ASCII dungeon crawler. I'm a relative newbie taking a college class. Right now we're just following along with a video 1-for-1, so I'm not entirely sure where the error is. Obviously it seems to want something defined regarding the Position (coordinates of an object in a room) and RoomData (properties of the room). But I'm lost as to what exactly. Sorry I can't be more specific - please let me know if there's extra info I can provide. namespace TextGame { RoomData CreateRoom(const std::string& inName, const std::string& inDescription) { RoomData room = {}; room.Name = inName; room.Description = inDescription; return room; } void InitializeGame(PlayerState& playerState, WorldState& worldState) { printf("Welcome to...\n"); printf("A GAME by SOMEONE\n\n"); printf("TELL THE PLAYER THE GOAL OF THE GAME\n\n"); playerState.WantsDescription = true; playerState.CurrentRoomIndex = 0; playerState.CurrentPosition.X = 3; playerState.CurrentPosition.Y = 4; worldState.Rooms.clear(); // 0 { RoomData room = CreateRoom("ROOM NAME", "ROOM DESCRIPTION"); room.RoomMapWidth = 10; room.RoomPosition = { 0, 0 }; room.RoomMap = "####.#####" "#........#" "#........#" "#........." "#........." "#........#" "#........#" "##########"; room.Inventory.push_back({ ItemType_Key, {5,3} }); room.Inventory.push_back({ ItemType_Sword, {7,5} }); room.LockedDoors.push_back({ {4, 0} }); worldState.Rooms.push_back(room); } // 1 { RoomData room = CreateRoom("ROOM NAME 1", "ROOM DESCRIPTION 1"); room.RoomMapWidth = 10; room.RoomPosition = { 1, 0 }; room.RoomMap = "##########" "#........#" "#........#" ".........#" ".........#" "#........#" "#........#" "##########"; worldState.Rooms.push_back(room); } // 2 { RoomData room = CreateRoom("ROOM NAME 2", "ROOM DESCRIPTION 2"); room.RoomMapWidth = 10; room.RoomPosition = { 0, -1 }; room.RoomMap = "####.#####" "#........#" "#........#" "#........#" "#........#" "#........#" "#........#" "####..####"; worldState.Rooms.push_back(room); } // 3 { RoomData room = CreateRoom("ROOM NAME 3", "ROOM DESCRIPTION 3"); room.RoomMapWidth = 10; room.RoomPosition = { 0, -2 }; room.RoomMap = "####..####" "#........#" "#........#" "#........#" "#........#" "#........#" "#........#" "####..####"; worldState.Rooms.push_back(room); } // 4 { RoomData room = CreateRoom("ROOM NAME 4", "ROOM DESCRIPTION 4"); room.RoomMapWidth = 10; room.RoomPosition = { 0, -3 }; room.RoomMap = "##########" "#....A...#" "#........#" "#........#" "#........#" "#........#" "#........#" "####..####"; worldState.Rooms.push_back(room); } } void GetInput(PlayerState& playerState, const WorldState& worldState) { playerState.WantsToExit = false; playerState.WantsDescription = false; playerState.WantsInventoryListed = false; playerState.DesiredPosition = playerState.CurrentPosition; playerState.WantsToGet = false; //playerState.Inventory.push_back({ ItemType_Key, Position() }); //playerState.Inventory.push_back({ ItemType_Sword, Position() }); //playerState.Inventory.push_back({ ItemType_Key, Position() }); printf("What do you do?\n"); printf("> "); TextAdventureCommand command = ParseAdventureCommand(); if (command.Verb == "quit") { playerState.WantsToExit = true; } else if (command.Verb == "north" || command.Verb == "w") { playerState.DesiredPosition.Y = playerState.DesiredPosition.Y - 1; } else if (command.Verb == "south" || command.Verb == "s") { playerState.DesiredPosition.Y = playerState.DesiredPosition.Y + 1; } else if (command.Verb == "west" || command.Verb == "a") { playerState.DesiredPosition.X = playerState.DesiredPosition.X - 1; } else if (command.Verb == "east" || command.Verb == "d") { playerState.DesiredPosition.X = playerState.DesiredPosition.X + 1; } else if (command.Verb == "look") { playerState.WantsDescription = true; } else if (command.Verb == "inventory" || command.Verb == "inv") { playerState.WantsInventoryListed = true; } else if (command.Verb == "get") { playerState.WantsToGet = true; } else if (command.Verb == "help") { printf("Command List: look, quit, inventory, get, w, a, s, d\n"); printf("Key:\n"); printf(" # - Player\n"); printf(" A - Altar\n"); printf(" i - Item\n"); printf(" . - Floor\n"); printf(" # - Wall\n"); printf(" H - Door (Locked)\n"); } else { printf("I don't understand\n"); } printf("\n"); } void RenderGame(const PlayerState& playerState, const WorldState& worldState) { if (playerState.WantsDescription) { const RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex]; printf("================================================\n"); printf("LOCATION: %s\n", currRoom.Name.c_str()); printf("%s\n\n", currRoom.Description.c_str()); std::string renderedMapString = ""; unsigned int currentSpace = 0; while (currentSpace < currRoom.RoomMap.size()) { char CharacterToDisplay = currRoom.RoomMap[currentSpace]; for (unsigned int i = 0; i < currRoom.Inventory.size(); ++i) { const InventoryItem& currItem = currRoom.Inventory[i]; if (PositionToIndex(currItem.ItemPosition, currRoom.RoomMapWidth) == currentSpace) { CharacterToDisplay = getItemIcon(currItem.Type); } } for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i) { const LockedDoorData& currDoor = currRoom.LockedDoors[i]; if (PositionToIndex(currDoor.DoorPosition, currRoom.RoomMapWidth) == currentSpace) { CharacterToDisplay = 'H'; } } //Set where the player is drawn based on the input X and Y coordinates if (PositionToIndex(playerState.CurrentPosition, currRoom.RoomMapWidth) == currentSpace) { CharacterToDisplay = '#'; } renderedMapString += CharacterToDisplay; if (currentSpace % currRoom.RoomMapWidth == currRoom.RoomMapWidth - 1) { renderedMapString += "\n"; } currentSpace++; } printf("%s\n", renderedMapString.c_str()); printf("\n"); } else if (playerState.WantsInventoryListed) { printf("================================================\n"); printf("INVENTORY:\n"); if (playerState.Inventory.size() == 0) { printf("You're not carrying anything.\n"); } else { for (unsigned int i = 0; i < playerState.Inventory.size(); ++i) { printf("%s\n", GetItemName(playerState.Inventory[i].Type).c_str()); } } printf("\n"); } } void UpdateGame(PlayerState& playerState, WorldState& worldState) { RoomData& currRoom = worldState.Rooms[playerState.CurrentRoomIndex]; if (playerState.DesiredPosition != playerState.CurrentPosition) { TryToUnlockDoor(playerState.DesiredPosition, currRoom, playerState); if (spaceVoid(playerState.DesiredPosition, currRoom)) { Position desiredRoomPosition = currRoom.RoomPosition; int playerPositionType = 0; if (playerState.DesiredPosition.X < 0) { desiredRoomPosition.X--; playerPositionType = 1; } else if(playerState.DesiredPosition.X >= currRoom.RoomMapWidth) { desiredRoomPosition.X++; playerPositionType = 2; } else if (playerState.DesiredPosition.Y < 0) { desiredRoomPosition.Y--; playerPositionType = 3; } else if (playerState.DesiredPosition.Y >= ((int)currRoom.RoomMap.size() / currRoom.RoomMapWidth)) { desiredRoomPosition.Y++; playerPositionType = 4; } bool foundNewRoom = false; for (unsigned int i = 0; i < worldState.Rooms.size(); ++i) { if (worldState.Rooms[i].RoomPosition == desiredRoomPosition) { playerState.CurrentRoomIndex = i; playerState.WantsDescription = true; switch (playerPositionType) { case 1: playerState.CurrentPosition.X = worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth - 1; break; case 2: playerState.CurrentPosition.X = 0; break; case 3: playerState.CurrentPosition.Y = (worldState.Rooms[playerState.CurrentRoomIndex].RoomMap.size() / worldState.Rooms[playerState.CurrentRoomIndex].RoomMapWidth) - 1; break; case 4: playerState.CurrentPosition.Y = 0; break; } foundNewRoom = true; } } if (!foundNewRoom) { printf("It's just the void beyond this point.\n"); } } else if (spaceOpen(playerState.DesiredPosition, currRoom)) { playerState.CurrentPosition = playerState.DesiredPosition; playerState.WantsDescription = true; } else { printf("It's blocked.\n"); } } else if (playerState.WantsToGet) { bool foundItem = false; for (unsigned int i = 0; i < currRoom.Inventory.size(); ++i) { const InventoryItem& currItem = currRoom.Inventory[i]; if (currItem.ItemPosition == playerState.CurrentPosition) { printf("Picked up a %s\n\n", GetItemName(currItem.Type).c_str()); playerState.Inventory.push_back(currItem); currRoom.Inventory.erase(currRoom.Inventory.begin() + i); foundItem = true; break; } } if (!foundItem) { printf("There's nothing her to pick up.\n\n"); } } } void CleanupGame(PlayerState& playerState, WorldState& worldState) { worldState.Rooms.clear(); } int PositionToIndex(const Position& position, int roomWidth) { return position.Y * roomWidth + position.X; } bool spaceOpen(const Position& position, const RoomData& currRoom) { for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i) { const LockedDoorData& currDoor = currRoom.LockedDoors[i]; if (currDoor.DoorPosition == position) { return false; } } int spaceIndex = PositionToIndex(position, currRoom.RoomMapWidth); return currRoom.RoomMap[spaceIndex] == '.'; } void TryToUnlockDoor(const Position& position, RoomData& currRoom, PlayerState& playerState) { for (unsigned int i = 0; i < currRoom.LockedDoors.size(); ++i) { const LockedDoorData& currDoor = currRoom.LockedDoors[i]; if (currDoor.DoorPosition == position) { //Door found, now find key for (unsigned int j = 0; j < playerState.Inventory.size(); ++j) { const InventoryItem& currItem = playerState.Inventory[j]; if (currItem.Type == ItemType_Key) { //Key found currRoom.LockedDoors.erase(currRoom.LockedDoors.begin() + i); playerState.Inventory.erase(playerState.Inventory.begin() + j); return; } } } } } bool spaceVoid(const Position& position, const RoomData& currRoom, PlayerState& playerState) { return position.X < 0 || position.X >= currRoom.RoomMapWidth || position.Y < 0 || position.Y >= ((int)currRoom.RoomMap.size() / currRoom.RoomMapWidth); } std::string GetItemName(ItemType itemType) { switch (itemType) { case ItemType_Key: return "Key"; break; case ItemType_Sword: return "Sword"; break; } } char getItemIcon(ItemType itemType) { switch (itemType) { case TextGame::ItemType_Key: return 'k'; break; case TextGame::ItemType_Sword: return 's'; break; } } }
The linker is complaining about not finding the spacevoid method taking two parameters: bool __cdecl TextGame::spaceVoid(struct TextGame::Position const &,struct TextGame::RoomData const &) because in your code there is only a spacevoid method with three parameters defined: bool spaceVoid(const Position& position, const RoomData& currRoom, PlayerState& playerState) You'll need to decide which one is the right one or add an additional spacevoid method taking two parameters only.
Error in Xcode: Thread 1: EXC_BAD_ACCESS (code=1, address=0xe8)
This is supposed to be a c++ game which works by reading in files. How ever, I am not really sure what is the problem in the code below. When I enter the file name it gives me this error (code is below, problematic line is also shown). If you can't fix it can you at least tell me why this seems to work fine in another computer, but when I try to compile in Xcode it goes wrong? #include "AdvRoom.h" AdvRoom::AdvRoom() { } bool AdvRoom::readRoom(ifstream &roomFile) { bool success = true; char data[64]; int pointer; while ((roomFile.get(data[0])) && data[0] == '\n') {} roomFile.unget(); if (success && !roomFile.eof() && roomFile.good()) { roomFile.get(data, 64); this->number = atoi(data); roomFile.get(data[0]); } else success = false; if (success && !roomFile.eof() && roomFile.good()) { roomFile.get(data, 64); this->name = data; roomFile.get(data[0]); } else success = false; if (success && !roomFile.eof() && roomFile.good()) { roomFile.get(data, 64); while (data[0] != '-' && success) { this->description.push_back(data); if (success && !roomFile.eof() && roomFile.good()) { roomFile.get(data[0]); roomFile.get(data, 64); } else success = false; } roomFile.get(data[0]); } else success = false; if (success && !roomFile.eof() && roomFile.good()) { // Check for \n string direction; int destination = 0; string key; while (roomFile.peek() != '\n' && success) { if (success && !roomFile.eof() && roomFile.good()) { roomFile.get(data, 64, ' '); direction = data; while ((roomFile.get(data[0])) && data[0] == ' ') {} roomFile.unget(); int i = 0; while ((roomFile.get(data[i])) && data[i] != '\n' && data[i] != ' ') { ++i; } if (data[i] == ' ') { data[i] = '\0'; destination = atoi(data); roomFile.get(data, 64, '\n'); key = data; roomFile.get(data[0]); } else if (data[i] == '\n') { data[i] = '\0'; destination = atoi(data); key = ""; //roomFile.get(data[0]); } AdvMotionTableEntry entry(direction, destination, key); this->motionTable.push_back(entry); } else success = false; } } else success = false; return success; } vector<string> AdvRoom::getDescription() { vector<string> copyDesc = this->description; return copyDesc; } string AdvRoom::getName() { string copyName = this->name; return copyName; } void AdvRoom::addObject(AdvObject obj) { // This function should add the obj to the room. // It however, should not add the object to the room // if the room already contains the object. this->objects.push_back(obj); } AdvObject AdvRoom::removeObject(string objName) { AdvObject deletedObj; // This function should remove the object with objName. for (int i = 0; i < this->objects.size(); ++i) { if (this->objects[i].getName() == objName) { deletedObj = this->objects[i]; this->objects.erase(this->objects.begin() + i); } } return deletedObj; } bool AdvRoom::containsObject(string objName) { // Returns true if object with objName is in the room. bool success = false; for (int i = 0; i < this->objects.size(); ++i) { if (this->objects[i].getName() == objName) { success = true; } } return success; } int AdvRoom::objectCount() { return this->objects.size(); } AdvObject AdvRoom::getObject(int index) { return this->objects.at(index); } bool AdvRoom::hasBeenVisited() { bool truth = this->isVisited; return truth; } void AdvRoom::setVisited(bool flag) { this->isVisited = flag; } vector<AdvMotionTableEntry> AdvRoom::getMotionTable() { vector<AdvMotionTableEntry> copyMotionTable = this->motionTable; return copyMotionTable; } int AdvRoom::getRoomNumber() { int copyNumber = this->number; return copyNumber; }
How can I adjust cursor position without changing screens?
I have a project I'm working on with an LCD screen, two buttons, and a potentiometer, it's a binary calculator. When I reach screenState == 3 I am attempting to change the digits so i can keep track of them and add an 8 bit number with another 8 bit number, while tracking overflow. However, whenever I begin to use the button which the input == 0 it moves the cursor, and then my next button cycles through the screens again. Input == 0 should only change the value between 0 and 1 for whatever lcd.cursor value I'm. How can i adjust that one value without changing screens? #include <LiquidCrystal.h> LiquidCrystal lcd(11, 10, 5, 4, 3, 2); const int numInputs = 2; const int inputPins[numInputs] = {13, 12}; // tracks screen state int screenState = 0; int prevScreenState = 0; int binaryValue = 0; // tracks if we are on the right screen to acceptInput and if our value is 0 or 1 bool canAcceptInput = false; bool valIsZero = false; int inputState[numInputs]; int lastInputState[numInputs] = {LOW,LOW}; bool inputFlags[numInputs] = {LOW,LOW}; int inputCounter[numInputs]; int cursorPosX = 0; int cursorPosY = 0; int blinkingPosX = cursorPosX; int blinkingPosY = cursorPosY; unsigned long lastDebounceTime[numInputs] = {0,0}; long debounceDelay = 50; void setup() { for(int i = 0; i < numInputs; i++) { pinMode(inputPins[i], INPUT); digitalWrite(inputPins[i], HIGH); // pull-up 20k } Serial.begin(9600); lcd.begin(16, 2); } void loop() { setInputFlags(); resolveInputFlags(); } void setInputFlags() { for(int i = 0; i < numInputs; i++) { int reading = digitalRead(inputPins[i]); if (reading != lastInputState[i]) { lastDebounceTime[i] = millis(); } if ((millis() - lastDebounceTime[i]) > debounceDelay) { if (reading != inputState[i]) { inputState[i] = reading; if (inputState[i] == HIGH) { inputFlags[i] = HIGH; } } } lastInputState[i] = reading; } } void resolveInputFlags() { for(int i = 0; i < numInputs; i++) { if(inputFlags[i] == HIGH) { // Input Toggle Logic // all invoked upon bttn press inputCounter[i]++; updateScreenState(i); printString(i); inputFlags[i] = LOW; } } } void updateScreenState(int input) { // input 0 = State 0 and 1 if(input == 0) { if(screenState > 2 & screenState < 19) { canAcceptInput = true; binaryValue = 1; updateScreenValues(); } else if (screenState == 1) { //screenState = 0; binaryValue = 0; updateScreenValues(); } else { //screenState = 2; binaryValue = 1; } // input 1 = State 2 to 6 }else if(input == 1) { // we have 20 max screen states, this cycles through them using button 2 if(screenState == 0 || screenState == 1 || screenState > 19) { screenState = 2; updateScreen(); }else{ screenState++; updateScreen(); } } } void setCursorValues( int cursorX, int cursorY) { lcd.setCursor(cursorX, cursorY); cursorPosX = cursorX; cursorPosY = cursorY; } void updateScreenValues() { if(canAcceptInput == true) { if(binaryValue == 0 ){ lcd.print("0"); } if(binaryValue == 1) { lcd.print("1"); binaryValue = 0; } } } // void update ----------------------------------------------------------------------------- void updateScreen() { // welcome screen ----------------------------- if(screenState == 2){ canAcceptInput = false; lcd.clear(); setCursorValues(0, 0); lcd.print("Welcome Screen"); setCursorValues(0, 1); lcd.print("Press Bttn 1"); } // start of byte 1 screen ---------------------------------------------------------------- if(screenState == 3){ canAcceptInput = true; lcd.clear(); setCursorValues(0, 0); lcd.print("Byte 1 Screen"); setCursorValues(0, 1); lcd.print(" 0000 0000 "); //move cursor to MSD and blink setCursorValues(3, 1); lcd.blink(); } //if screenState changes, so will the cursorValues----------- if(screenState == 4) { setCursorValues(4, 1); } if(screenState == 5) { setCursorValues(5, 1); } if(screenState == 6) { setCursorValues(6, 1); } if(screenState == 7) { setCursorValues(9, 1); } if(screenState == 8) { setCursorValues(10, 1); } if(screenState == 9) { setCursorValues(11, 1); } if(screenState == 10) { setCursorValues(12, 1); } //start of byte 2 screen ------------------------------------------------------- if(screenState == 11){ lcd.clear(); setCursorValues(0, 0); lcd.print("Byte 2 Screen"); setCursorValues(0, 1); lcd.print(" 0000 0000 "); setCursorValues(3, 1); lcd.blink(); } //if screenState changes, so will the cursorValues----------- if(screenState == 12) { setCursorValues(4, 1); } if(screenState == 13) { setCursorValues(5, 1); } if(screenState == 14) { setCursorValues(6, 1); } if(screenState == 15) { setCursorValues(9, 1); } if(screenState == 16) { setCursorValues(10, 1); } if(screenState == 17) { setCursorValues(11, 1); } if(screenState == 18) { setCursorValues(12, 1); } if(screenState == 19){ lcd.clear(); setCursorValues(0, 0); lcd.print("Solution Screen"); lcd.noCursor(); } if(screenState == 20){ lcd.clear(); setCursorValues(0, 0); lcd.print("Contrast Screen"); } } void printString(int output) { Serial.print("Input "); Serial.print(output); Serial.print(" was pressed "); Serial.print(inputCounter[output]); Serial.println(" times."); Serial.print("screenState = "); Serial.println(screenState); Serial.print("binaryValue = "); Serial.println(binaryValue); Serial.print("cursorPosX = "); Serial.print(cursorPosX); Serial.print(" cursorPosY = "); Serial.println(cursorPosY); Serial.print('\n'); }
Verification code doesn't work in Arduino
I am new to Arduino and I'm trying to make a program that receives IR codes from a TV remote, uses them as a 4 number pass code lighting up a LED as you press each button. And then comparing the code to a hard-coded one. In this case 1234. I made a function to verify that the value entered is equal to the pass. If so, light up a green LED and else, light up a red one. However, even if I input the correct code, only the red led lights up. Here is my whole code as I'm not sure which part of it is the one causing problems: const int pass[4] = {1, 2, 3, 4}; int value[4] = {}; int digitNum = 0; int input; void loop() { value[digitNum] = input; //where input is a number between 0 and 9 digitNum++; if(digitNum == 1){ lightFirstLed(); } else if(digitNum == 2){ lightSecondLed(); } else if(digitNum == 3){ lightThirdLed(); } else if(digitNum == 4){ lightFourthLed(); verify(); } } void verify() { bool falseCharacter = false; for(int i = 0; i <= 4; i++){ if(value[i] != pass[i]){ falseCharacter = true; } } if(!falseCharacter){ lightGreenLed(); } else{ lightRedLed(); } } Functions in the form of light*Led actually do what they're supposed to do. I tried changing the verify function around, that ended up making the green LED the one that always shone. I've been doing this for hours and I'm starting to feel disparate. I would really appreciate any help. And please tell me if anything I'm doing does not comply with best practices even if it's out of the scope of this question. For full code and design, here's a link to autodesk's simulator: https://www.tinkercad.com/things/0keqmlhVqNp-mighty-leelo/editel?tenant=circuits?sharecode=vVUD2_4774Lj4PYXh6doFcOqWUMY2URIfW8VXGxutRE= EDIT: Now reset doesn't work
Your for loop in verify is accessing outside the array: const int pass[4] = {1, 2, 3, 4}; int value[4] = {}; for(int i = 0; i <= 4; i++){ if(value[i] != pass[i]){ falseCharacter = true; } } Change i <= 4 to i < 4. Also, when falseCharacter is set to true, break from the loop: for(int i = 0; i < 4; i++) { if(value[i] != pass[i]) { falseCharacter = true; break; } } Update You need an else statement in loop: void loop(void) { if(irrecv.decode(&results)) { if (results.value == powBtn) { reset(); } else if (results.value == zeroBtn) { input = 0; } else if (results.value == oneBtn) { input = 1; } else if (results.value == twoBtn) { input = 2; } else if (results.value == threeBtn) { input = 3; } else if (results.value == fourBtn) { input = 4; } else if (results.value == fiveBtn) { input = 5; } else if (results.value == sixBtn) { input = 6; } else if (results.value == sevenBtn) { input = 7; } else if (results.value == eightBtn) { input = 8; } else if (results.value == nineBtn) { input = 9; } else { return; /*** !!! Unrecognized Value !!! ***/ } value[digitNum] = input; digitNum++; if(digitNum == 1) { digitalWrite(LED1, HIGH); } else if(digitNum == 2) { digitalWrite(LED2, HIGH); } else if(digitNum == 3) { digitalWrite(LED3, HIGH); } else if(digitNum == 4) { digitalWrite(LED4, HIGH); verify(); } else { if (results.value == powBtn) { reset(); } } // Receive the next value irrecv.resume(); } }