Just some code help

I keep getting a message that says I didn't define the variable "second", "hour" and "minute" but I did already defined it earlier. Any help would be appreciated. Thank you!

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//#include <iostream>
//#include <string>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#define BUZZER_PIN 10
#define BUTTON_PIN 11

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'0','0','0'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

void setup(){
  Serial.begin(9600);
  lcd.init(); // initialize the lcd
  lcd.init();
  lcd.backlight();
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lcd.setCursor(2,0);
  lcd.setCursor(2,0);
  lcd.print("   :    :   ");
  lcd.setCursor(4,1);
  lcd.print("Set timer");

 
}
  boolean typable = true;
  int currentNum = 2;
  int currentRow = 0;
  int passnum1 = 0;
  int lastState = HIGH;
  int currentState;
  void(* resetFunc) (void) = 0;
  boolean exploded = false;
  boolean settingtime = true;
  boolean firstSetup = false;

  





void loop(){


  int currentState = digitalRead(BUTTON_PIN);
  Serial.println(currentState);
  //delay(100);
  
  //if(typable){
    char key = keypad.getKey();
  //}
  if (settingtime && currentNum == 4){
    currentNum = 7;
  }
  if (settingtime && currentNum == 9){
    currentNum = 12;
  }
  if (settingtime && currentNum == 14){
    settingtime = false;
    passnum1 = 0;
    currentNum = 4;
    currentRow = 1;
    lcd.setCursor(2,0);
    lcd.print("Set password");
    lcd.setCursor(0,1);
    lcd.print("                    ");
  }
  

  

  if (key && typable){
    if (exploded){
      resetFunc();
    }
    if (!(key == "*") && !(key == "#")){      
      lcd.setCursor(currentNum,currentRow);
      lcd.print(key);
      currentNum++;
      beep(4000, random(40,10));
      passnum1++; 


      int second = 30;
      int minute = 0;
      int hour = 0;

      char firsthournum;
      char firstminutenum;
      char firstsecondnum;
      char secondhournum;
      char secondminutenum;
      char secondsecondnum;
      
      //timer value gatherer
      if (settingtime && currentNum == 2){
      firsthournum = (key);
      }
      
      if (settingtime && currentNum == 3){
      secondhournum = (key);
      
      hour = (int(firsthournum + secondhournum));
      }
      
      if (settingtime && currentNum == 7){
      firstminutenum = (key);
      }
      if (settingtime && currentNum == 8){
      secondminutenum = (key);
      minute = (int(firstminutenum + secondminutenum));
      }

      if (settingtime && currentNum == 12){
      firstsecondnum = (key);
      }
      if (settingtime && currentNum == 13){
      secondsecondnum = (key);
      second = (int(firstsecondnum + secondsecondnum));
      }
      
    }
  }
  if(settingtime == false){


  if(firstSetup == false){
    
  }
  
    
  if(passnum1 == 8){
      passnum1 = 9;
      typable = false;
      lcd.setCursor(2,0);
      lcd.print("Password Set");
      beep(100, random(250)); beep(100, random(250)); beep(100, random(250)); beep(100, random(250)); beep(100, random(250)); beep(100, random(250)); beep(100, random(250));
      lcd.setCursor(4,1);
      lcd.print("********");
      delay(2000);


      // count down

      lcd.setCursor(2,0);
      lcd.print("   :    :   ");
      
      //set time
      

      int delayamount = 1000;
      


      //timer loop
      boolean countdown = true;
       while(countdown){
        
        delay(10);

         
         
         second--;
         
          analogWrite(BUZZER_PIN, 200);
          delay(delayamount / 4); 
          analogWrite(BUZZER_PIN, 0);
          delay((delayamount / 4) * 3); 

         if (second == 0){
          if (!(minute == 0)){
            minute--;
            second = 59;
          }
          
         }
         if (minute == 0){
          if (!(hour == 0)){
            hour--;
            minute = 59;
          }
         }
         
         lcd.setCursor(2,0);
         lcd.print(String(hour));

         lcd.setCursor(7,0);
         lcd.print(String(minute));

         lcd.setCursor(12,0);
         lcd.print(String(second));

         lcdcheck(second, 13);
         lcdcheck(minute, 8);
         lcdcheck(hour, 3);
         if((hour == 0) && (minute == 0) && (second == 0)){
          countdown = false;
         }

       }    

       if (countdown == false){
        lcd.setCursor(0,0);
        lcd.print("                           ");
        lcd.setCursor(0,1);
        lcd.print("                           ");
        lcd.setCursor(2,0);
        lcd.print("The bomb has");
        lcd.setCursor(4,1);
        lcd.print("exploded");
        analogWrite(BUZZER_PIN, 250);
        delay(5000); 
        analogWrite(BUZZER_PIN, 0);
        exploded = true;
        typable = true;
        
  }
  }
  
}
}
void beep(unsigned char delayms, int intensity){
 analogWrite(BUZZER_PIN, intensity);
 delay(delayms); 
 analogWrite(BUZZER_PIN, 0);
 delay(delayms); 
}  
void lcdcheck(int num, int pos){
  if (num < 10){
    lcd.setCursor(pos, 0);
    lcd.print(" ");
  }
}

These

      int second = 30;
      int minute = 0;
      int hour = 0;

are only visible within the enclosing { braces }.

If you mean to have one variable for each of those three things, you can use global varaibles, those wou,d be defined once, usually at the top of a program.

Then just use them, no “int” necessary, that’s what turns it into something if limited scope (visibility).

google

    c++ variable scope

and

   c++ variable local global static  

and read a bit.

a7

1 Like

Sorry, super used to java :sweat_smile: I'll give it a spin

It worked! Thanks so much, I'm so dumb lol

NP.

At a glance, Java variables have all the same concepts more or less, although I did see some questions about how to do global variables.

So if you know Java, mapping that to how things are expressed and accomplish in C/C++ shoukd be easy.

a7

1 Like

Yeah, Java doesn't really use global variables or anything like that much, just public and private classes and things like that.

Here is a good page on scope.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.