Arduino – serratura a combinazione programmabile (sketch)

Qui trovate l’articolo
Il video può essere visualizzato anche su youtube
Supportami su Patreon!
Seguimi anche sulla pagina di facebook! e sul canale di youtube

Di seguito trovate il codice sorgente del progetto


#include
Servo myServo;
const int piezo = A0;
const int switchUnoPin = 2;
const int switchDuePin = 3;
const int switchTrePin = 4;
const int yellowLEDPin = 6;
const int greenLEDPin = 7;
const int redLEDPin = 8;
int switchUnoVal;
int switchDueVal;
int switchTreVal;
const int quietKnock = 10;
const int loudKnock = 100;
boolean locked = false;
int numberOfKnocks = 0;
int progCombinazione[5];
int inserCombinazione[5];
int previousTime = 0;
int currentTime = 0;

void setup() {
myServo.attach(9);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(switchUnoPin, INPUT);
pinMode(switchDuePin, INPUT);
pinMode(switchTrePin, INPUT);
Serial.begin(9600);
myServo.write(180);

digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, HIGH);
delay(300);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
delay(300);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, HIGH);
delay(300);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);

for(int i = 0; i < 5; i++) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
while(switchUnoVal == LOW && switchDueVal == LOW && switchTreVal == LOW) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
}

if(switchUnoVal == HIGH) {
progCombinazione[i] = 1;
digitalWrite(yellowLEDPin, HIGH);
delay(500);
digitalWrite(yellowLEDPin, LOW);
delay(500);
}
if(switchDueVal == HIGH) {
progCombinazione[i] = 2;
digitalWrite(greenLEDPin, HIGH);
delay(500);
digitalWrite(greenLEDPin, LOW);
delay(500);
}
if(switchTreVal == HIGH) {
progCombinazione[i] = 3;
digitalWrite(redLEDPin, HIGH);
delay(500);
digitalWrite(redLEDPin, LOW);
delay(500);
}

Serial.println(progCombinazione[i]);
}
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, HIGH);
delay(300);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
delay(300);

for(int i = 0; i < 5; i++) {
switch(progCombinazione[i]) {
case 1:
digitalWrite(yellowLEDPin, HIGH);
delay(500);
digitalWrite(yellowLEDPin, LOW);
delay(100);
break;
case 2:
digitalWrite(greenLEDPin, HIGH);
delay(500);
digitalWrite(greenLEDPin, LOW);
delay(100);
break;
case 3:
digitalWrite(redLEDPin, HIGH);
delay(500);
digitalWrite(redLEDPin, LOW);
delay(100);
break;
}
}
myServo.write(0);
digitalWrite(greenLEDPin, HIGH);
locked = false;
}

void loop() {

while(locked == false) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
while(switchUnoVal == LOW && switchDueVal == LOW && switchTreVal == LOW) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
}
if(switchUnoVal == HIGH) {
myServo.write(90);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
locked = true;
}
if(switchDueVal == HIGH) {
myServo.write(90);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
locked = true;
}
if(switchTreVal == HIGH) {
myServo.write(90);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
locked = true;
}
delay(500);
}
while(locked == true) {
previousTime = millis();
for(int i = 0; i < 5; i++) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
while(switchUnoVal == LOW && switchDueVal == LOW && switchTreVal == LOW) {
switchUnoVal = digitalRead(switchUnoPin);
switchDueVal = digitalRead(switchDuePin);
switchTreVal = digitalRead(switchTrePin);
currentTime = millis();
if(currentTime – previousTime > 20000) {
inserCombinazione[0] = 0; // se si superano i 15″ viene modificato uno dei valori della combinazione che risulterà quindi errata
inserCombinazione[1] = 0;
inserCombinazione[2] = 0;
inserCombinazione[3] = 0;
inserCombinazione[4] = 0;
i = 4;
}
}

if(switchUnoVal == HIGH) {
inserCombinazione[i] = 1;
digitalWrite(yellowLEDPin, HIGH);
delay(500);
digitalWrite(yellowLEDPin, LOW);
delay(500);
}
if(switchDueVal == HIGH) {
inserCombinazione[i] = 2;
digitalWrite(yellowLEDPin, HIGH);
delay(500);
digitalWrite(yellowLEDPin, LOW);
delay(500);
}
if(switchTreVal == HIGH) {
inserCombinazione[i] = 3;
digitalWrite(yellowLEDPin, HIGH);
delay(500);
digitalWrite(yellowLEDPin, LOW);
delay(500);
}

}

if(array_compare(progCombinazione, inserCombinazione) == true) {
locked = false;
myServo.write(0);
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
} else {
digitalWrite(redLEDPin, LOW);
delay(200);
digitalWrite(redLEDPin, HIGH);
delay(200);
digitalWrite(redLEDPin, LOW);
delay(200);
digitalWrite(redLEDPin, HIGH);
delay(200);
digitalWrite(redLEDPin, LOW);
delay(200);
digitalWrite(redLEDPin, HIGH);
delay(200);
digitalWrite(redLEDPin, LOW);
delay(200);
digitalWrite(redLEDPin, HIGH);
}
}
}

boolean array_compare(int *a, int *b){

for (int n = 0 ; n < 5; n++) {
if (a[n] != b[n]) return false;
}

return true;
}

Ti piaciono i miei tutorial e progetti? Aiutami a realizzarne altri. Supporta Zamein – Tutorials & Projects su Patreon!

2 thoughts on “Arduino – serratura a combinazione programmabile (sketch)

  1. il codice mi da due errori in fase di compilazione , il primo alla stringa 1 :
    “#include” che credo debba essere scritta cosi: “#include ”
    il secondo alla stringa:
    if(currentTime – previousTime) > 20000) {
    non riesco a risolverlo, ti riporto l’errore:

    sketch_nov18a:161: error: expected primary-expression before ‘>’ token
    if(currentTime – previousTime) > 20000) {
    sketch_nov18a:161: error: expected ‘;’ before ‘)’ token
    if(currentTime – previousTime) > 20000) {
    exit status 1
    stray ‘\226’ in program

    Zappo912 Rispondi

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *