Ho un problema riguardante il mio progetto serratura arduino.
non riesco a far si che premendo il pulsante si esegua il ciclo che permette al motorino di sbloccare la porta...
mi era stato suggerito qui nel forum un codice ma se lo inserisco, quando vado a premere il pulsante il tutto da di matto..
allego il codice, grazie in anticipo a chi saprà aiutarmi
#include <Password.h>
#include <Keypad.h>
#include <Servo.h>
Servo myservo; //declares servo
Password password = Password( "1923" ); //password to unlock box, can be changed
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{
'1','2','3','A' } ,
{
'4','5','6','B' } ,
{
'7','8','9','C' } ,
{
'*','0','#','D' }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {
9, 8, 7, 6 };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {
5, 4, 3, 2 };
const int buttonPin = 10; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int sensorValue = digitalRead(10); //NB ho provato a creare un metodo per il bottone, ma ho eliminato
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
Serial.write(254);
Serial.write(0x01);
delay(200);
pinMode(11, OUTPUT); //green light
pinMode(12, OUTPUT); //red light
myservo.attach(13); //servo on digital pin 9 //servo
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
Serial.println(sensorValue);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
pressButton() ;
keypad.getKey();
myservo.write(90);
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Enter: ");
Serial.println(eKey);
delay(10);
Serial.write(254);
switch (eKey){
case 'A':
checkPassword();
delay(1);
break;
case 'C':
password.reset();
delay(1);
break;
default:
password.append(eKey);
delay(1);
}
}
}
void checkPassword(){
if (password.evaluate()){ //if password is right open box
Serial.println("Accepted");
Serial.write(254);
delay(1000);
//Add code to run if it works
myservo.write(50); //160deg
digitalWrite(11, HIGH);//turn on
delay(20000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else{
Serial.println("Denied"); //if passwords wrong keep box locked
Serial.write(254);
delay(10);
//add code to run if it did not work
myservo.write(77);
digitalWrite(12, HIGH); //turn on
delay(500); //wait 5 seconds
digitalWrite(12, LOW);//turn off
}
}
void pressButton(){
if (buttonState == HIGH) {
Serial.write(254);
delay(1000);
//Add code to run if it works
myservo.write(45); //160deg
digitalWrite(11, HIGH);//turn on
delay(20000); //wait 5 seconds
digitalWrite(11, LOW);// turn off
}
else {
Serial.write(254);
delay(10);
//add code to run if it did not work
myservo.write(0);
digitalWrite(12, HIGH); //turn on
delay(500); //wait 5 seconds
digitalWrite(12, LOW);//turn off
}
}