Safe box with timer

Hello, I recreated this projectof safe box for school:

but I changed a little in the schematic and the code cause my LCD has an I2C ,unfortunately It doesn't work properly.
I'm absolutely beginner but I think the problem would be in my schematic
``
#include <LiquidCrystal_I2C.h>


#include <Time.h> 
#include <TimeLib.h> 
  
 
#include <EEPROM.h> 
#include <Servo.h> 
 
LiquidCrystal_I2C lcd(0x27, 16, 2); 
 
Servo myServo; 
 
const int BackButtonPin = 6; 
const  int StartButtonPin = 7; 
const int MinusButtonPin = 8; 
const int PlusButtonPin  = 9; 
 
int settingHours = 0;    
int settingMinutes = 0;   
int settingSeconds  = 0;   
time_t settingTime = 0; 
 
int activeHours = 0; 
int activeMinutes  = 0; 
int activeSeconds = 0; 
time_t activeTime = 0;  
 
 
time_t  startTime = 0; 
time_t elapsedTime = 0; 
 
int BackButtonState = LOW; 
long  BackButtonLongPressTime = 0; 
int StartButtonState = LOW; 
int PlusButtonState  = LOW; 
int MinusButtonState = LOW; 
int BackButtonPrevState = LOW; 
int  StartButtonPrevState = LOW; 
int PlusButtonPrevState = LOW; 
int MinusButtonPrevState  = LOW; 
bool BackButtonPress = false; 
bool BackButtonLongPress = false; 
bool  StartButtonPress = false; 
bool PlusButtonPress = false; 
bool MinusButtonPress  = false; 
 
bool locked = false; 
 
const int READY = 0; 
const int  SETTINGS = 1; 
const int LOCKED = 2; 
const int UNLOCKED = 3; 
 
int  activeState = READY;     
 
int timeData = 0;   
 
void setup()  { 
 
  lcd.begin(); 
  pinMode(BackButtonPin, INPUT); 
  pinMode(StartButtonPin,  INPUT); 
  pinMode(PlusButtonPin, INPUT); 
  pinMode(MinusButtonPin, INPUT);  
  myServo.attach(10); 
  Serial.begin(9600); 
  myServo.write(90); 
  
} 
 
void loop() { 
   
  StartButtonPress = false; 
  PlusButtonPress  = false; 
  MinusButtonPress = false; 
 
  BackButtonPress = false; 
  BackButtonLongPress = false; 
  BackButtonState = digitalRead(BackButtonPin);  
  if(BackButtonState != BackButtonPrevState) 
  { 
    BackButtonPress  = BackButtonState == HIGH; 
    BackButtonPrevState = BackButtonState;  
  
  } 
  else 
  { 
    if(BackButtonState == HIGH) 
    { 
      BackButtonLongPressTime++;  
      if(BackButtonLongPressTime == 100) 
      { 
        BackButtonPress  = false; 
        BackButtonLongPress = true; 
        BackButtonLongPressTime  = 0; 
      } 
    } 
    else 
    { 
      BackButtonLongPressTime  = 0; 
      BackButtonPress = false; 
      BackButtonLongPress = false; 
    } 
  } 
 
  StartButtonPress = false; 
  StartButtonState = digitalRead(StartButtonPin);  
  if(StartButtonState != StartButtonPrevState) 
  { 
    StartButtonPress  = StartButtonState == HIGH; 
    StartButtonPrevState = StartButtonState; 
  }  
 
 
  MinusButtonPress = false; 
  MinusButtonState = digitalRead(MinusButtonPin);  
  if(MinusButtonState != MinusButtonPrevState) 
  { 
    MinusButtonPress  = MinusButtonState == HIGH; 
    MinusButtonPrevState = MinusButtonState; 
  } 
 
  PlusButtonPress = false; 
  PlusButtonState = digitalRead(PlusButtonPin);  
  if(PlusButtonState != PlusButtonPrevState) 
  { 
    PlusButtonPress  = PlusButtonState == HIGH; 
    PlusButtonPrevState = PlusButtonState; 
  }  
 
  switch(activeState) 
  { 
    case READY: 
    myServo.write(90);  
      if(BackButtonPress) 
      { 
        Reset(); 
      } 
      if(BackButtonLongPress) 
      { 
        activeState = SETTINGS;  
  
      } 
      if(StartButtonPress) 
      { 
        activeState =  activeState == READY ? LOCKED : READY; 
        if(activeState == LOCKED) 
        { 
          startTime = now(); 
        } 
      } 
      break;  
 
    case SETTINGS: 
    myServo.write(90); 
      if(BackButtonPress)  
      { 
        settingTime = settingSeconds + (60 * settingMinutes) + (3600  * settingHours); 
        activeHours = settingHours; 
        activeMinutes  = settingMinutes; 
        activeSeconds = settingSeconds; 
        timeData  = 0; 
        activeState = READY; 
      } 
      if(StartButtonPress)  
      { 
        timeData++; 
        if(timeData == 3) 
        {  
          timeData = 0; 
 
        } 
      } 
      if(MinusButtonPress)  
      { 
        switch(timeData) 
        { 
          case 0:  
            settingHours--; 
            if(settingHours == -1) 
            {  
              settingHours = 99; 
            } 
            break; 
          case 1:  
            settingMinutes--; 
            if(settingMinutes  == -1) 
            { 
              settingMinutes = 59; 
            }  
            break; 
          case 2:  
            settingSeconds--;  
            if(settingSeconds == -1) 
            { 
              settingSeconds  = 59; 
            } 
            break; 
        }  
 
      } 
      if(PlusButtonPress) 
      { 
        switch(timeData) 
        {  
          case 0:  
            settingHours++; 
            if(settingHours  == 100) 
            { 
              settingHours = 0; 
            }  
            break; 
          case 1:  
            settingMinutes++;  
            if(settingMinutes == 60) 
            { 
              settingMinutes  = 0; 
            } 
            break; 
          case 2:  
            settingSeconds++;  
            if(settingSeconds == 60) 
            { 
              settingSeconds  = 0; 
            } 
            break; 
        } 
      } 
 
      break; 
     
    case LOCKED: 
    myServo.write(0); 
      if(StartButtonPress)  
      { 
        activeState = READY; 
      } 
      if(BackButtonPress)  
      { 
        Reset(); 
        activeState = READY; 
      } 
      break; 
 
    case UNLOCKED: 
    myServo.write(90); 
      if(BackButtonPress  || StartButtonPress || MinusButtonPress || PlusButtonPress) 
      { 
        activeState  = READY; 
      } 
      break; 
  } 
 
  switch(activeState) 
  { 
    case READY: 
    case SETTINGS:  
 
      break; 
    case  LOCKED: 
      activeTime = settingTime - (now() - startTime); 
      if(activeTime  <= 0) 
      { 
        activeState = UNLOCKED; 
      } 
      break;  
  } 
 
  lcd.setCursor(0, 0); 
  switch(activeState) 
  { 
    case  READY: 
      
      lcd.print("Ready: Start    "); 
      lcd.setCursor(0, 1);  
      lcd.print(activeHours); 
      lcd.print("  "); 
      lcd.print(activeMinutes);  
      lcd.print("  "); 
      lcd.print(activeSeconds); 
      lcd.print("     "); 
      break; 
    case SETTINGS: 
      lcd.print("Set Timer:  "); 
      switch(timeData) 
      { 
        case 0: 
 
          lcd.print("HRS  "); 
          break; 
        case 1: 
          lcd.print("MINS");  
          break; 
        case 2: 
          lcd.print("SECS"); 
          break; 
      } 
      lcd.setCursor(0, 1); 
      lcd.print(settingHours);  
      lcd.print("  "); 
      lcd.print(settingMinutes); 
      lcd.print("  "); 
      lcd.print(settingSeconds); 
      lcd.print("     "); 
      break; 
    case LOCKED: 
      lcd.print("Waiting...       "); 
      lcd.setCursor(0, 1); 
      if(hour(activeTime) < 10) lcd.print("0");  
      lcd.print(hour(activeTime)); 
      lcd.print(": "); 
      if(minute(activeTime)  < 10) lcd.print("0"); 
      lcd.print(minute(activeTime)); 
      lcd.print(":  "); 
      if(second(activeTime) < 10) lcd.print("0"); 
      lcd.print(second(activeTime));  
   
 
      break; 
    case UNLOCKED: 
      lcd.print("   UNLOCKED!   "); 
      lcd.setCursor(0, 1); 
      lcd.print("              ");  
      break; 
  } 
  delay(10); 
} 
 
void Reset() 
{ 
  activeState = READY; 
  activeHours = settingHours; 
  activeMinutes = settingMinutes;  
  activeSeconds = settingSeconds; 
}

Please help me with any sort of solutions :pray:

what do you mean? i see text on display

What schematic? Please post.

Hello lateenmichel73

Take some time and describe the desired function of the sketch in simple words and this very simple.

I want to create a safe box with timer
Set the time with the 4 buttons then the servomotor will turn to close the lock
When the count down is done the servo motor turns again to unlock. This is the flow chart of the original project:


Thank you for your time

Sorry I mean circuit.
If I need to add any connection or remove it .
Especially the 4 buttons cause I changed many times the places of wires and still can't work. Thanks for your interest

The first text is the only thing that worked . All the following steps still stucked . Even the LCD doesn't react to any push button.
Thank you for your interest

Hello

I´ve made a small code review with the following recommendation:

This sketch seems to have grown organically through nested and dupilcate function calls.
This leads to spaghetti code and nodes in the logic that are functionally undesirable.

What to do.
There are two options.
Debug, using multiple Serial.println()'s to see what happens under different conditions. Nodes can be solved this way, but new ones can also be created in the process.
The second option is to rearrange the sketch. Based on the IPO model, structure the desired function of the sketch into basic functions. Take a sheet of paper and a pencil and draw a programme structure to identify the required functions, e.g. button, timer and display functions. All these functions can be coded and tested separately. To complete the project, you also need to design a control structure that uses events to call the above functions. In this way keep in mind to design objects and related services thus handle input/output actions.
That is all that needs to be done. Try them out.

Have a nice day and enjoy coding in C++.

1 Like

зображення
rectangle block can not have two outputs, only diamond block (you have it as skew quadrangle).

1 Like

Any update? I would like to know.

OP did not use pull down resistors, so the button inputs were floating, resulting in random triggering of the inputs.

It would have been better if the buttons were wired between pins and ground,
with pull up enabled on the pins with pinMode (no resistors needed).

Note that logic is now reversed. Pin is normally HIGH, and LOW when button is pushed.
Leo..

2 Likes

I THINK the buttons are wired as changeover, with one side maybe to +5 and the other maybe to ground. Out of focus pic and untidy wiring doesnt help.

So start by wiring the buttons correctly, and checking they work.
You've gone from walking to ferrari and missed out the "learning to drive" step!

HI, @lateenmichel73

Can you please post a schematic of your project?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Do you have a DMM, to check if the tactile switches are actually connecting with the protoboard?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

I though the poster assumed that too, which tact switches are not.
Leo..

1 Like

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