Hi, I'm working on a puzzle box and one of these items is a button to initiate a timer sequence on a TM1637 display. There are also other parts such as a keypad to input a password into a LCD. The LCD and keypad work perfectly on their own but the second the TM1637 display turns on, it stops working. The same happens for the entire box really. Nothing seems to work when the timer starts.
That code is below
#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <TM1637Display.h>
//button
const int button3 = 30;
int button3State = 0;
int lastState = HIGH;
int currentState;
//TM1637
#define clk1 15
#define dio1 14
#define clk2 16
#define dio2 17
#define numberOfSeconds(_time_) ((_time_ / 1000) & 60)
#define numberOfMinutes(_time_) (((_time_ / 1000) / 60) & 60)
const uint8_t OFF[] = {0, 0, 0, 0};
const uint8_t PLAY[] = {B01110011, B01011111, B01101110};
TM1637Display display(clk1, dio1);
TM1637Display display2(clk2, dio2);
unsigned long timeLimit = 3600000;
//LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Keypad
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','#'}
};
byte pin_rows[ROW_NUM] = {5, 6, 7, 8};
byte pin_column[COLUMN_NUM] = {2, 3, 4};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
//Passwords
const int Password_Length = 5;
String Data;
String Master = "1970#";
char customKey;
byte data_count = 0;
int lockOutput = 7;
void setup(){
pinMode(button3, INPUT_PULLUP);
lcd.init();
lcd.backlight();
display.setBrightness(0x0c);
display.setSegments(OFF);
display2.setBrightness(0x0c);
display2.setSegments(OFF);
}
void countdown(){
unsigned long timeRemaining = timeLimit - millis();
while(timeRemaining > 0) {
int seconds = numberOfSeconds(timeRemaining);
int minutes = numberOfMinutes(timeRemaining);
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
timeRemaining = timeLimit - millis();
}
}
void Timer() {
currentState = digitalRead(button3);
if(lastState == LOW && currentState == HIGH){
countdown();
delay(100);
}
// save the last state
lastState = currentState;
}
void LCD() {
// Initialize LCD and print
lcd.setCursor(0, 0);
lcd.print("Enter Password:");
// Look for keypress
customKey = keypad.getKey();
if (customKey) {
// Enter keypress into array and increment counter
Data += customKey;
lcd.setCursor(data_count, 1);
lcd.print(Data[data_count]);
data_count++;
}
// See if we have reached the password length
if (data_count == Password_Length) {
lcd.clear();
if (Data == Master) {
// Correct Password
delay(300);
lcd.print("Correct:");
delay(300);
lcd.setCursor(0,1);
lcd.print("D2");
delay(10000);
}
else {
// Incorrect Password
lcd.print("Incorrect");
delay(1000);
}
// Clear data and LCD display
lcd.clear();
}
}
void loop(){
Timer();
LCD();
}