Currently, I am making a project for my school and I am having an issue when the code runs multiple times. I am using an Arduino Mega 2560. The project I am doing is making a safe/box that requires a PIN to open it. In order of how things are supposed to operate it follows:
- The IR receiver receives a signal from a remote that activates the signal. When the system is active it turns on a blue LED. When the system is inactive it does nothing.
- When the system is active, it can read inputs from the 4x4 pin pad. If it gets a wrong code it turns on a red LED then clears the data so it can take in the code again. If the correct code is given then a green LED is turned on and turns a servo motor 90 degrees.
- After the correct code is turned on, a button is pressed to return the motor to 0 degrees and deactivates the system.
The issue is that after the button is pressed I cannot reactivate the system. It basically becomes a 1 and done. The code for the project can be found below.
#include <Keypad.h>
#include <Servo.h>
#include <IRremote.h>
Servo servo;
const byte ROWS = 4;
const byte COLS = 4;
#define Passcode_L 5
char Data[Passcode_L];
char Master[Passcode_L]="14B6";
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {22, 24, 26, 28};
byte colPins[COLS] = {30, 32, 34, 36};
byte data_count = 0;
char customKey;
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const int led_blue = 23;
const int led_green = 25;
const int led_red = 27;
const int BUTTON = 47;
int BUTTONstate = 0;
const int RECV_PIN = 44;
int togglestate = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
servo.attach(38);
servo.write(0);
pinMode(led_blue, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop(){
char customKey = customKeypad.getKey();
BUTTONstate = digitalRead(BUTTON);
if (irrecv.decode(&results)){
switch(results.value){
case 0xFFA25D:
// Toggle BLUE LED On or Off
if(togglestate==0){
digitalWrite(led_blue, HIGH);
togglestate=1;
}
else {
digitalWrite(led_blue, LOW);
togglestate=0;
}
break;
}
irrecv.resume();
}
if (customKey){
Data[data_count] = customKey;
data_count++;
}
if (data_count == Passcode_L - 1 && togglestate==1) {
if (!strcmp(Data, Master)) {
//password is correct
digitalWrite(led_green, HIGH);
delay(500);
servo.write(90);
delay(1500);
digitalWrite(led_green, LOW);
}
else {
// Password is incorrect
digitalWrite(led_red, HIGH);
delay(1000);
digitalWrite(led_red, LOW);
}
clearData();
}
if (BUTTONstate !=0){
servo.write(0);
togglestate=0;
digitalWrite(led_blue, LOW);
}
}
void clearData() {
// Go through array and clear data
while (data_count != 0) {
Data[data_count--] = 0;
}
return;
}
Aucun commentaire:
Enregistrer un commentaire