So I set a break point at the line Collide(0,1); and I try to step into the method but it wont go in there. Any ideas why?
Collide(0,1);
if(PosX<(screen->h-40))
{
if(LevelOne[screenCamera.ScreenOffsetX][screenCamera.ScreenOffsetY+1] == 0) //Collision
{
screenCamera.SavePreviousOffests();
screenCamera.ScreenOffsetY += 1;
if(screenCamera.ScreenOffsetY > 30 ||
screenCamera.ScreenOffsetY < 10 ||
screenCamera.PreviosScreenOffsetY == 9)
{
Move(0, 40);
}
}
}
bool Hero::Collide(int xMovement, int yMovement)
{
int nextPositionContents = LevelOne[PosX/40 + xMovement][PosY/40 + yMovement];
if(nextPositionContents == 11) //blue key
{
//LevelOne[PosX/40 + xMovement][PosY/40+ yMovement] == 0;
HasBlueKey = true;
}
if(nextPositionContents == 10 && HasBlueKey)//blue door
{
//LevelOne[PosX/40+ xMovement][PosY/40+ yMovement] == 0;
HasBlueKey = false;
}
nextPositionContents = 0;
return false;
}
Related
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.
I'm having an anti cheat system for my private online game platformed for Windows.
One of the most popular functions I have is memory change detection.
Source code for memory change detections:
bool MEMORY_PROTECTION_INIT() // OK
{
bool ClearFileMapping = 0;
if((FileMappingHandle=CreateFileMapping(INVALID_HANDLE_VALUE,0,PAGE_READWRITE,0,sizeof(HACK_VERIFY_FILE_MAPPING),"Local\\HACK_VERIFY_FILE_MAPPING")) == 0)
{
return 0;
}
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
ClearFileMapping = 0;
}
else
{
ClearFileMapping = 1;
}
if((lpHackVerifyFileMapping=(HACK_VERIFY_FILE_MAPPING*)MapViewOfFile(FileMappingHandle,FILE_MAP_ALL_ACCESS,0,0,sizeof(HACK_VERIFY_FILE_MAPPING))) == 0)
{
return 0;
}
if(ClearFileMapping != 0)
{
lpHackVerifyFileMapping->Clear();
}
MemoryProtectionTime = GetTickCount();
return 1;
}
bool MEMORY_PROTECTION_SCAN() // OK
{
if(gMemoryGuardSwitch == 0 || (gMemoryGuardNumber & MEMORY_GUARD_NUMBER_INJECT) == 0)
{
return 1;
}
for(DWORD n=0;n < lpHackVerifyFileMapping->WriteVirtualMemoryCount;n++)
{
HACK_VERIFY_FILE* lpHackVerifyFile = &lpHackVerifyFileMapping->WriteVirtualMemoryTable[n];
if(lpHackVerifyFile->time >= MemoryProtectionTime)
{
if(lpHackVerifyFile->spid != GetCurrentProcessId())
{
if(lpHackVerifyFile->tpid == GetCurrentProcessId())
{
CHClientDisconnectSend(CLIENT_DISCONNECT_MEMORY_DETECTION,0,lpHackVerifyFile->spid);
return 1;
}
}
}
}
return 1;
}
bool HANDLE_PROTECTION_INIT() // OK
{
CProcessQuery ProcessQuery;
HANDLE HandleValue = OpenProcess(PROCESS_VM_READ,0,GetCurrentProcessId());
if(ProcessQuery.Fetch(SystemExtendedHandleInformation,sizeof(SYSTEM_HANDLE_INFO_EX)) != 0)
{
SYSTEM_HANDLE_INFO_EX* lpSystemHandleInfo = ProcessQuery.GetExtendedHandleInfo();
if(lpSystemHandleInfo != 0)
{
SYSTEM_HANDLE_ENTRY_INFO_EX* lpSystemHandleEntryInfo = lpSystemHandleInfo->Handles;
if(lpSystemHandleEntryInfo != 0)
{
for(DWORD n=0;n < lpSystemHandleInfo->NumberOfHandles;n++,lpSystemHandleEntryInfo++)
{
if(lpSystemHandleEntryInfo->UniqueProcessId == GetCurrentProcessId() && lpSystemHandleEntryInfo->HandleValue == ((DWORD)HandleValue))
{
HandleProtectionNumber = (DWORD)lpSystemHandleEntryInfo->ObjectTypeIndex;
HandleProtectionObject = (DWORD)lpSystemHandleEntryInfo->Object;
ProcessQuery.Close();
return 1;
}
}
}
}
}
ProcessQuery.Close();
return 0;
}
bool HANDLE_PROTECTION_SCAN() // OK
{
if(gMemoryGuardSwitch == 0 || (gMemoryGuardNumber & MEMORY_GUARD_NUMBER_HANDLE) == 0)
{
return 1;
}
static CProcessQuery ProcessQuery;
std::map<DWORD,std::vector<DWORD>> HandleProtectionTable;
if(ProcessQuery.Fetch(SystemExtendedHandleInformation,sizeof(SYSTEM_HANDLE_INFO_EX)) != 0)
{
SYSTEM_HANDLE_INFO_EX* lpSystemHandleInfo = ProcessQuery.GetExtendedHandleInfo();
if(lpSystemHandleInfo != 0)
{
SYSTEM_HANDLE_ENTRY_INFO_EX* lpSystemHandleEntryInfo = lpSystemHandleInfo->Handles;
if(lpSystemHandleEntryInfo != 0)
{
for(DWORD n=0;n < lpSystemHandleInfo->NumberOfHandles;n++,lpSystemHandleEntryInfo++)
{
if(lpSystemHandleEntryInfo->UniqueProcessId != GetCurrentProcessId() && lpSystemHandleEntryInfo->ObjectTypeIndex == HandleProtectionNumber && lpSystemHandleEntryInfo->Object == ((LPVOID)HandleProtectionObject) && (lpSystemHandleEntryInfo->GrantedAccess & PROCESS_VM_WRITE) != 0)
{
std::map<DWORD,std::vector<DWORD>>::iterator it = HandleProtectionTable.find(lpSystemHandleEntryInfo->UniqueProcessId);
if(it == HandleProtectionTable.end())
{
HandleProtectionTable.insert(std::pair<DWORD,std::vector<DWORD>>(lpSystemHandleEntryInfo->UniqueProcessId,std::vector<DWORD>(1,lpSystemHandleEntryInfo->HandleValue)));
continue;
}
else
{
if(it->second.size() >= 10)
{
CHClientDisconnectSend(CLIENT_DISCONNECT_MEMORY_DETECTION,0,lpSystemHandleEntryInfo->UniqueProcessId);
return 0;
}
else
{
it->second.push_back(lpSystemHandleEntryInfo->HandleValue);
continue;
}
}
}
}
}
}
}
return 1;
}
bool MEMORY_CHECK_ATTACH() // OK
{
CCRC32 CRC32;
MODULEINFO ModuleInfo;
memset(&ModuleInfo,0,sizeof(ModuleInfo));
if(GetModuleInformation(GetCurrentProcess(),GetModuleHandle(0),&ModuleInfo,sizeof(ModuleInfo)) == 0)
{
return 0;
}
IMAGE_NT_HEADERS32* lpNtHeader = (IMAGE_NT_HEADERS32*)((DWORD)ModuleInfo.lpBaseOfDll+((IMAGE_DOS_HEADER*)ModuleInfo.lpBaseOfDll)->e_lfanew);
IMAGE_SECTION_HEADER* lpSectionHeader = (IMAGE_SECTION_HEADER*)((DWORD)lpNtHeader+sizeof(IMAGE_NT_HEADERS32));
for(int n=0;n < lpNtHeader->FileHeader.NumberOfSections;n++)
{
MEMORY_CHECK_SOURCE data;
data.VirtualAddress = (DWORD)ModuleInfo.lpBaseOfDll+lpSectionHeader[n].VirtualAddress;
data.VirtualSize = lpSectionHeader[n].Misc.VirtualSize;
data.VirtualChecksum = CRC32.FullCRC((BYTE*)data.VirtualAddress,data.VirtualSize);
MemoryCheckSource.insert(std::pair<DWORD,MEMORY_CHECK_SOURCE>(data.VirtualAddress,data));
break;
}
return 1;
}
bool MEMORY_CHECK_DETACH() // OK
{
CCRC32 CRC32;
for(std::map<DWORD,MEMORY_CHECK_SOURCE>::iterator it=MemoryCheckSource.begin();it != MemoryCheckSource.end();it++)
{
if(it->second.VirtualChecksum != CRC32.FullCRC((BYTE*)it->second.VirtualAddress,it->second.VirtualSize))
{
return 0;
}
}
return 1;
}
For some reasons it works fine on some malwares, but some software (that are doing memory changes too) aren't detected by the function.
After few checks I realized that the difference between both types of cheats is the memory base address.
For those that are detected by the function- the base address that is calling to Heap(id2) is 0x30000. Although the software that aren't detected by the function- the base address that is calling to heap(id2) (- same use) is 0x10000 (the first one).
any ideas what changes should I make to make sure that these cheats are blocked?
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;
}
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');
}
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();
}
}