Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- #include <OneButton.h>
- Servo s1;
- OneButton button(A0, true);
- int pot = A4; //potentiometer
- int val; //code variable
- int closed =97; //Servo position when closed
- int opened = 125; //Servo position when opened
- void setup()
- {
- pinMode(pot, INPUT);
- Serial.begin(9600); //optional
- button.attachDoubleClick(doubleclick); //Variable to close the box with a double click
- s1.attach(3); //Servo Pin
- s1.write(closed); //Locks the box when it gets power
- delay(1000); //delay to make sure the servo had enough time to move
- }
- void loop()
- {
- button.tick(); //variable to check for the double click button
- int pStatus = analogRead(pot); //variable to check the potentiometer value
- pStatus = map(pStatus, 1, 1024, 1, 4); //mapping the potentiometer from 1 to 4. This means I have a total of 3 different digits which I can enter. This number can be increased as much as you want
- if(pStatus == 1 && val == 0) //when the potentiometer is on position 1 and the value (for the code) is zero 1 is added to the value
- {
- val ++;
- }
- if(pStatus == 3 && val == 1)
- {
- val ++;
- }
- if(pStatus == 1 && val == 2)
- {
- val = 0;
- }
- if(pStatus == 2 && val == 2)
- {
- val++;
- }
- if(pStatus == 1 && val == 3) //safety point. Without this the potentiometer could just be turned from one to the other side a few times and the save would open anyways
- {
- val = 0;
- }
- if(pStatus == 3 && val == 3)
- {
- val++;
- }
- if(pStatus == 1 && val == 4) //when this point is reached the safe opens
- {
- s1.write(opened);
- }
- else //to ensure the safe stays closed and closes again when double click is performed
- {
- s1.write(closed);
- }
- Serial.println(val); //optional
- }
- void doubleclick() //void for the double click
- {
- val = 0; //when the double click is performed the code value is set to 0 again, meaning the safe closes.
- }
- //PROBLEMS / ISSUES
- //With this few code digits the save is still easy to acces trough trying.
- //Because of this and the fact that there is only one "safety point" the save ironicly isn't that safe.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement