Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Livrable 4
- //pins declaration
- const int PINS_DIPSWITCH[] = {2, 3, 4, 5};
- const int GREEN_LED = 13;
- const int RED_LED = 12;
- const int BUZZER = 9;
- //dipswith reading function
- int read_level() {
- int read_switch[4];
- int level;
- for (int i = 0; i < 4; i++) {
- read_switch[i] = digitalRead(PINS_DIPSWITCH[i]);
- }
- //recomposing a decimal number
- level = 1000*read_switch[0] + 100*read_switch[1] + 10*read_switch[2] + read_switch[3];
- return level;
- }
- bool select_lvl(int level) {
- bool result;
- Serial.print("level : ");
- Serial.println(level);
- //levels select and function exec
- switch(level) {
- case 0:
- //level 0
- result = true;
- break;
- case 1:
- //level 1
- result = question();
- break;
- case 10:
- //level 2
- result = code(level);
- break;
- case 11:
- //level 3
- result = (question() && code(level));
- break;
- case 100:
- //level 4
- result = biometric_scan();
- break;
- case 101:
- //level 5
- result = (question() && biometric_scan());
- break;
- case 110:
- //level 6
- result = (code(level) && biometric_scan());
- break;
- case 111:
- //level 7
- result = (question() && code(level) && biometric_scan());
- break;
- default :
- //invalid level (pour le switch n1 car on cable que les switch 2 3 4)
- Serial.println("Enter a right combinaison on the dipswitchs");
- result = false;
- //blinking one time the red led
- digitalWrite(RED_LED, HIGH);
- delay(500);
- digitalWrite(RED_LED, LOW);
- delay(500);
- }
- return result;
- }
- bool question() {
- bool question_bon;
- int right_choice = 3;//car on a définit que le bon choix est le numero 3
- int user_choice;
- Serial.println("Quel est le nom de votre chien ?");
- Serial.println("1 - Teddy");
- Serial.println("2 - Rox");
- Serial.println("3 - Albert");
- Serial.println("Entrer votre réponse (1, 2 ou 3) :"); //serial.print : permet d'écrire et ln = retour à la ligne
- //wait for serial message
- while (!Serial.available()); //attend message
- //write serial message to user_code
- user_choice = Serial.parseInt(); //serial.parseint = lecture de la réponse de l'utilisateur
- question_bon = (right_choice == user_choice);
- Serial.end();
- Serial.begin(9600);
- return question_bon;
- }
- bool code(int seed) {
- bool code_bon;
- int code = 42 * seed; //seed étant la référence au numero du case (1,10,11,100,101, etc...)
- int user_code; // par exemple, ici, pour le niv 2, l'utilisateur va taper 42*10 soit 420
- Serial.println("Entrer votre code :");
- //wait for serial message
- while (!Serial.available());
- //write serial message to user_code
- user_code = Serial.parseInt();
- code_bon = (code == user_code);
- return code_bon;
- }
- bool biometric_scan() {
- bool code_bon;
- String right_string = "boitecostaud"; //string est l'équivalent de scanf
- String user_string = ""; //l'utilisateur doit donc écrire "boitecostaud" qui est le bon code
- Serial.println("Entrer votre mot de passe :");
- //wait for serial message
- while (!Serial.available());
- //write serial message to user_code
- user_string = Serial.readString();
- user_string.trim(); //permet d'enlever les espaces
- code_bon = (right_string.equalsIgnoreCase(user_string)); // compare "right_string" et "user_string" en ignorant les majuscules
- return code_bon;
- }
- void toneYes(){
- tone(BUZZER, 370); //BUZZER branché sur le PIN 9 et 370 est la fréquence du buzzer
- delay(100); //delai de 100ms
- tone(BUZZER, 1000);
- delay(200);
- noTone(BUZZER); //arrete le buzzer
- delay(1000);
- }
- void toneNo(){
- tone(BUZZER,300);
- delay(200);
- noTone(BUZZER);
- delay(100);
- tone(BUZZER,300);
- delay(200);
- noTone(BUZZER);
- }
- void setup() {
- //settins I/O
- //setting dipswiths pin as input
- for (int i = 0; i < 4; i++) {
- pinMode(PINS_DIPSWITCH[i], INPUT_PULLUP); //INTPUT PULLUP permet de pas avoir bcp de branchements mais ON équivaut à 0 et OFF renvoie 1
- }
- //setting leds pins as output
- pinMode(GREEN_LED, OUTPUT); //OUTPUT et pas INPUT car on sort de l'électricité par la LED
- pinMode(RED_LED, OUTPUT);
- pinMode(BUZZER, OUTPUT);
- //initialising Serial
- Serial.begin(9600);
- Serial.setTimeout(12000);
- }
- void loop() {
- int level = read_level();
- bool result = false;
- result = select_lvl(level);
- if (result == true){
- //sequence checking : true
- Serial.println("True sequence");
- digitalWrite (GREEN_LED, HIGH);
- digitalWrite(RED_LED, LOW);
- toneYes();
- //door should open
- //stop the program execution with infinite loop
- while (result);
- }
- else{
- //sequence checking : false
- Serial.println("False sequence");
- digitalWrite(RED_LED, HIGH);
- digitalWrite (GREEN_LED, LOW);
- toneNo();
- delay(500);
- }
- //slow down the loop
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement