Hi!
I am getting really desperate with a project here. It all works fine by powering the project from the usb via the computer - the sketch works as intended. But whenever I wire it through my power supply - [https://www.electrokit.com/uploads/productfile/41014/rd35a.pdf](https://this one) it shuts down after the passcode is entered on the keypad. The power supply has both a 12v and a 5v output so I have my arduino powered through the vin port. I´ve tried both the 12v and the 5v. The correct passcode lights up the green LED and switches the solenoid lock to open for 3 seconds and then it is supposed to start counting down. But when the led and the solenoid lock opens it resets the sketch and starts over. Ill post the wiring and the code below.
I dont know what to do anymore. Totally lost
#include <Arduino_GFX_Library.h>
#include <Keypad.h>
Arduino_DataBus *bus = new Arduino_SWSPI(8 /* DC */, 9 /* CS */, 13 /* SCK */, 11 /* MOSI */, -1 /* MISO */);
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 7 /* RST */, 0 /* rotation */, true /* IPS */);
#define relay 2
#define red1 3
#define green1 4
#define Password_Length 7
const int piezo = 12;
long hour = 23, minute = 59, second = 59;
long countdown_time = (hour * 3600) + (minute * 60) + second;
unsigned long currentMinute = 0;
unsigned long oldMinute = 0;
bool unlocked = true;
char Data[Password_Length];
char Master[Password_Length] = "141477";
byte data_count = 0, master_count = 0;
bool passwordValid;
char customKey;
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A0, A5, A4, A2};
byte colPins[COLS] = {A1, 5, A3};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup() {
// put your setup code here, to run once:
gfx->begin();
gfx->fillScreen(BLACK);
#ifdef TFT_BL
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
#endif
Serial.begin(9600);
pinMode(relay, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green1, OUTPUT);
pinMode(piezo, OUTPUT);
digitalWrite(red1, 0);
digitalWrite(green1, 0);
digitalWrite(relay, 0);
delay(1000); // 1 seconds
gfx->setCursor(50, 100);
gfx->setTextColor(RED);
gfx->setTextSize(2);
gfx->println("initiating...");
gfx->fillScreen(BLACK);
delay(2000);
}
void getpwd()
{
while (data_count < 6)
{
customKey = customKeypad.getKey();
if (customKey) {
Data[data_count] = customKey;
Serial.print(Data[data_count]);
data_count++;
}
}
if (data_count == Password_Length - 1) {
if (!strcmp(Data, Master)) {
Serial.println("\nCorrect");
clearData();
tone(piezo,500);
delay(100);
noTone(piezo);
digitalWrite(relay, 1);
digitalWrite(green1, 1);
digitalWrite(red1, 0);
//gfx->setCursor(50, 120);
// gfx->print("Correct");
delay(3000);
digitalWrite(relay, 0);
digitalWrite(green1, 0);
digitalWrite(red1, 0);
unlocked = false;
gfx->fillScreen(BLACK);
}
else {
Serial.println("\nIncorrect");
digitalWrite(relay, 0);
digitalWrite(green1, 0);
digitalWrite(red1, 1);
tone(piezo,1000);
delay(800);
tone(piezo,1000);
delay(800);
noTone(piezo);
clearData();
delay(5000);
digitalWrite(relay, 0);
digitalWrite(green1, 0);
digitalWrite(red1, 0);
}
}
}
void clearData()
{
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}
// resetTimer will make the timer reset
void resetTimer() {
countdown_time = (hour * 3600) + (minute * 60) + second;
}
void loop() {
// put your main code here, to run repeatedly:
gfx->setCursor(70, 100);
gfx->setTextSize(3);
gfx->setTextColor(RED);
if (unlocked) {
Serial.println("Waiting on user input");
gfx->println("Enter");
gfx->setCursor(50, 120);
gfx->println("Passcode");
// Wait on Keypad to be entered
// and set unlocked to false if correct
getpwd();
} else {
// Block until CountDown timer restarts
resetTimer();
Serial.println("Blocking 24 hours");
lock24Hours();
}
}
// lock24Hours locks the code execution for 24 hours
void lock24Hours() {
while (!unlocked) {
String timeStr = timeLeft();
// Check if we get the exit code here
if (timeStr == "exit") {
// set unlocked to true to escape
unlocked = true;
}
Serial.println(timeStr);
gfx->setTextSize(6);
gfx->setCursor(40, 100);
gfx->println(timeStr);
delay(1000);
}
}
// timeLeft builds the timer string
String timeLeft() {
long countdowntime_seconds = countdown_time - (millis() / 1000);
if (countdowntime_seconds <= 1) {
}
// If Countdown time is less 0 or 0 , return a exit code
if (countdowntime_seconds <= 0) {
return "exit";
}
String result = "";
if (countdowntime_seconds >= 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
// If Hours is less than 10, then append 0 infront
if (countdown_hour < 10) {
result.concat("0");
}
result.concat(countdown_hour);
result.concat(":");
// If Min is less than 10, append 0
if (countdown_minute < 10) {
result.concat("0");
}
result.concat(countdown_minute);
Serial.println("countdown minute is");
Serial.println(countdown_minute);
currentMinute = countdown_minute;
Serial.println("currentMinute is");
Serial.println(currentMinute);
// result.concat(":");
countdown_check ();
// If Sec is less than 10, append 0
if (countdown_sec < 10) {
// result.concat("0");
}
// result.concat(countdown_sec);
Serial.println(result);
return result;
}
}
void countdown_check () {
if (currentMinute != oldMinute) {
gfx->fillScreen(BLACK);
Serial.println("oldMinute is");
Serial.println(oldMinute);
Serial.println("currentMinute is");
Serial.println(currentMinute);
oldMinute = currentMinute;
} else {
Serial.println("Clear Screen");
}
}