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');
}
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.
#define CLK 2
#define DT 3
#define SW 4
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
LiquidCrystal_PCF8574 lcd(0x27);
int counter = 0;
int currentStateCLK;
int lastStateCLK;
int lastCLK,cnt=0,btnState,lastPress=0;
String currentDir ="";
unsigned long lastButtonPress = 0;
char *mainmenu[] ={"SET MODE","SET TEMP","SET HUMD","SERVO","RESET"};
char *setmode[] ={"INCUBATOR","HATCHER","BACK"};
void setup() {
// Set encoder pins as inputs
Wire.begin();
Wire.beginTransmission(0x27);
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.home(); lcd.clear();
Serial.begin(9600);
lastStateCLK = digitalRead(CLK);
delay(100);
if(EEPROM_READ(0)==NULL){
SET_MODE();
}
Serial.print(EEPROM_READ(0));
}
void loop(){
disp();
rot();
}
void disp(){
lcd.setCursor(0,0);
lcd.print(" KGF");
}
void rot() {
int lim=sizeof(mainmenu)/2;
Serial.print(lim);
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
lcd.clear();
lcd.setCursor(0, 1);
if (digitalRead(DT) != currentStateCLK) {
counter --;
if(counter<0){
counter=lim-1;
}
}
else {
// Encoder is rotating CW so increment
counter ++;
if(counter>lim-1){
counter=0;
}
lcd.print(mainmenu[counter]);
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
if(counter==0){
SET_MODE();
}
}
}
lastButtonPress = millis();
}
delay(1);
}
void SET_MODE(){
int lim=sizeof(setmode)/2;
int currentCLK = digitalRead(CLK);
if (currentCLK != lastCLK && currentCLK == 1){
lcd.clear();
lcd.setCursor(0, 1);
if (digitalRead(DT) != currentCLK) {
cnt --;
if(cnt<0){
cnt=lim-1;
}
}
else {
// Encoder is rotating CW so increment
cnt ++;
if(cnt>lim-1){
cnt=0;
}
}
lcd.print(setmode[cnt]);
}
lastCLK = currentCLK;
btnState = digitalRead(SW);
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
if(setmode[cnt]=="BACK"){
exit(0);
}
lcd.clear();
lcd.setCursor(0, 1);
EEPROM_WRITE(0,setmode[cnt]);
lcd.print("DONE");
}
lastPress = millis();
}
delay(1);
}
void EEPROM_WRITE(int addrOffset, const String &strToWrite)
{
byte len = strToWrite.length();
EEPROM.write(addrOffset, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(addrOffset + 1 + i, strToWrite[i]);
}
}
String EEPROM_READ(int addrOffset)
{
int newStrLen = EEPROM.read(addrOffset);
char data[newStrLen + 1];
for (int i = 0; i < newStrLen; i++)
{
data[i] = EEPROM.read(addrOffset + 1 + i);
}
data[newStrLen] = '\0';
return String(data);
}
I want to call the SET_MODE() function in the loop from rot() function, I am building a menu based program so the SET MODE menu should redirect to the SET_MODE() function, and as I will be adding more menu and sub-menus how can I perform this task.
The SET_MODE() function doesn't work in loop I do not know why, it only works when I all it under void loop() directly.
I am a third year student, the first arduino classes started this year - I'm having trouble executing a for loop with delay and without stopping the program. My task is to create a game similar to a dinosaur in chrome, and the problem is when I want to separate each cycle for 100ms of time for the cacti to move sideways.
Can someone help me? :)
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// punktacja
int score = 0;
int lastScore = 0;
// czy gracz wygral?
bool won = false;
// zmienna do lcd clear po zwycięstwie
bool blocker = true;
int buttonPin = 6;
int buttonState = 0;
// Obliczanie czasu do spawnu obstacli
//unsigned long aktualnyCzas = millis();
//unsigned long zapamietanyCzas = 0;
//unsigned long roznicaCzasu = 0;
byte dino[8] = {
0b00000,
0b00111,
0b00111,
0b10110,
0b11111,
0b01010,
0b01010,
0b00000
};
byte cactus[8] = {
0b00100,
0b00101,
0b10101,
0b10101,
0b10111,
0b11100,
0b00100,
0b00000
};
byte stone[8] = {
0b00000,
0b00100,
0b01110,
0b01110,
0b01110,
0b11111,
0b11111,
0b11111
};
byte clear[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup() {
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(11, 0);
lcd.print("s:");
lcd.createChar(0, dino);
lcd.createChar(1, cactus);
lcd.createChar(2, stone);
lcd.createChar(9, clear);
}
void loop() {
//Obliczanie czasu
//aktualnyCzas = millis();
// Odczyt z guzika
buttonState = digitalRead(buttonPin);
//Główna pętla gry
if(won == false)
{
// Spawn kamieni
if(lastScore < score - 5)
{
lastScore = score;
for(int i = 15; i >= 0; i--)
{
SpawnStone(i);
wait(100);
}
}
// Punktacja
score = millis() / 100;
lcd.setCursor(13, 0);
lcd.print(score);
if(buttonState == HIGH)
{
JumpDino();
}
else if (buttonState == LOW)
{
SpawnDino();
}
}
//LCD Clear po wygranej
if(score == 1000 && blocker == true)
{
blocker = false;
lcd.clear();
}
//Informacja o zwycięstwie
if(score > 999)
{
won = true;
lcd.setCursor(0, 0);
lcd.print("YOU WON!");
return;
}
}
void SpawnDino()
{
lcd.setCursor(0,1);
lcd.write(byte(0));
lcd.setCursor(0,0);
lcd.write(byte(9));
}
void JumpDino()
{
lcd.setCursor(0,0);
lcd.write(byte(0));
lcd.setCursor(0,1);
lcd.write(byte(9));
}
int SpawnStone(int i)
{
lcd.setCursor(i,1);
lcd.write(byte(2));
lcd.setCursor(i+1,1);
lcd.write(byte(9));
}
void wait(long duration)
{
int wait = 0;
long last = millis();
while (wait == 0)
{
long now = millis();
if (now - last >= duration)
{ wait = 1;}
else {}
}
}
#Edit
I tried this:
int SpawnStone(int i)
{
if(actualTime - rememberTime > differenceTime)
{
rememberTime = actualTime;
lcd.setCursor(i,1);
lcd.write(byte(2));
lcd.setCursor(i+1,1);
lcd.write(byte(9));
SpawnStone(i-1);
}
}
but it just made my stones appear at starting frame like this
:(
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// punktacja
int score = 0;
int lastScore = 0;
// czy gracz jeszcze gra
bool isPlaying = false;
// zmienna do lcd clear po zwycięstwie
bool blocker = true;
int buttonPin = 6;
int buttonState = 0;
// Obliczanie czasu do spawnu obstacli
long actualTime = 0;
long rememberTime = 0;
long differenceTime = 100;
int checkScore;
int stonePosition;
byte dino[8] = {
0b01111,
0b01111,
0b00111,
0b10110,
0b11111,
0b01010,
0b01010,
0b01011
};
byte stoneDown[8] = {
0b00000,
0b00100,
0b01110,
0b01110,
0b01110,
0b11111,
0b11111,
0b11111
};
byte clear[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup() {
Serial.begin(9600);
checkScore = 0;
pinMode(buttonPin, INPUT);
lcd.begin(16, 2);
lcd.setCursor(11, 0);
lcd.print("s:");
rememberTime = millis();
differenceTime = 100;
lcd.createChar(0, dino);
lcd.createChar(2, stoneDown);
lcd.createChar(9, clear);
stonePosition = 15;
}
void loop() {
//Obliczanie czasu
actualTime = millis();
// Odczyt z guzika
buttonState = digitalRead(buttonPin);
//Główna pętla gry
if(isPlaying == false)
{
// Dodatkowy poziom trudności
if(score >= 1 && score <= 99)
{
differenceTime = 100;
}
if(score >= 100 && score <= 199)
{
differenceTime = 50;
lcd.setCursor(2, 0);
lcd.print("Faster!");
}
if(score >= 200 && score <= 299)
{
differenceTime = 30;
lcd.setCursor(2, 0);
lcd.print("FASTER!");
}
// Obsługa guzika
if(buttonState == HIGH)
{
JumpDino();
}
else if (buttonState == LOW)
{
SpawnDino();
}
// Spawn kamieni
if(actualTime - rememberTime >= differenceTime)
{
rememberTime = actualTime;
stonePosition--;
SpawnStone(stonePosition);
if(stonePosition < 0)
{
stonePosition = 15;
}
}
// Punktacja
score = millis() / 100;
lcd.setCursor(13, 0);
lcd.print(score);
}
//LCD Clear po wygranej
if(score == 301 && blocker == true)
{
blocker = false;
lcd.clear();
}
//Informacja o zwycięstwie lub porażce
if(stonePosition == 0 && buttonState == LOW)
{
isPlaying = true;
lcd.setCursor(0, 0);
lcd.print("YOU LOST!");
return;
}
if(score > 300)
{
isPlaying = true;
lcd.setCursor(0, 0);
lcd.print("YOU WON!");
return;
}
}
// Dinozaur na dole lub u góry
void SpawnDino()
{
lcd.setCursor(0,1);
lcd.write(byte(0));
lcd.setCursor(0,0);
lcd.write(byte(9));
}
void JumpDino()
{
lcd.setCursor(0,0);
lcd.write(byte(0));
if(stonePosition != 0)
{
lcd.setCursor(0,1);
lcd.write(byte(9));
}
}
// Spawn kamieni - funkcja
int SpawnStone(int i)
{
lcd.setCursor(i,1);
lcd.write(byte(2));
lcd.setCursor(i+1,1);
lcd.write(byte(9));
}
I did this that way for all time travellers. Hope you enjoy :)
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();
}
}
I am using keypad 4*4 with an Arduino Mega, I have sensors connected to the Arduino I want to press a key from the keypad and have response in the same time, I don't want to wait till the loop continue, I want to save up to 10 numbers entered from the keypad, so is there a way to make the keypad as an interrupt for my code?
void loop()
{
char key = kpd.getKey();
if (key != NO_KEY) {
if (key == 'C') //clear the array
{
index = 0;
numbers[index] = '\0';
}
else if (key == '.') {
numbers[index++] = '.';
numbers[index] = '\0';
}
else if (key >= '0' && key <= '9') {
for (int z = 0; z == 10; z++) {
numbers[index++] = key;
numbers[index] = '\0';
}
}
else if (key == 'A') {
float len = atof(numbers); //conversion from string to numeric
if (fona.callPhone(numbers)) {
Serial.println(F("RING!"));
Serial.print(F("Phone Number: "));
Serial.println(numbers); //print out on serial monitor
}
}
else if (key == 'D') {
if (fona.pickUp()) {
Serial.println(F("Failed"));
}
}
else if (key == '#') {
if (fona.hangUp()) {
Serial.println(F("Failed"));
}
}
}
gas = analogRead(gasPin);
dtostrf(gas, 4, 2, gass);
Serial.println(gas);
delay(1000); // Print value every 1 sec.
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(150);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
delay(300);
if (pirState == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 550) {
digitalWrite(led, HIGH);
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(led, LOW);
}
valee = analogRead(tempPin);
float cel = (valee * 0.48828125) / 2.12304;
dtostrf(cel, 4, 2, temp);
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);