Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- int soundSens = A0;
- int opButt = 3;
- int setButt = 4;
- int servo = 5;
- String millisOp[10] = {"0", "0", "0", "0", "0", "0", "0", "0", "0", "0"};
- String tempo[8] = {"400", "400", "0", "0", "0", "0", "0", "0"};
- int h = 0;
- int opState01 = 0;
- int opLastState01 = 0;
- bool lockState01 = true;
- int opState02 = 0;
- int opLastState02 = 0;
- bool lockState02 = true;
- Servo Servo01;
- void iCheck();
- void tempoCheck();
- void setup() {
- for (int i = 3; i <= 4; i++) {
- pinMode(i, INPUT);
- }
- pinMode(soundSens, INPUT);
- Servo01.attach(servo);
- Serial.begin(9600);
- }
- void loop() {
- int sens = analogRead(soundSens);
- opState01 = digitalRead(opButt);
- opState02 = digitalRead(setButt);
- Servo01.write(0);
- if (h >= 10) {
- h = 0;
- }
- // Toggle lock state on button press
- if (opState01 != opLastState01) {
- if (opState01 == LOW) { // Button pressed
- lockState01 = !lockState01; // Toggle the lock state
- }
- opLastState01 = opState01;
- }
- if (!lockState01) { // Check if lock is engaged
- if (sens == HIGH) {
- millisOp[h] = millis();
- delay(100);
- h++;
- }
- // Test serial output (don't mind this)
- for (int i = 0; i < 10; i++) {
- Serial.print(millisOp[i]);
- Serial.print(", ");
- if (i == 9) {
- Serial.println();
- }
- }
- }
- if (lockState01) {
- iCheck();
- h = 0;
- }
- // Toggle lock state on button press
- if (opState02 != opLastState02) {
- if (opState02 == LOW) { // Button pressed
- lockState02 = !lockState02; // Toggle the lock state
- }
- opLastState02 = opState02;
- }
- if (lockState02) { // Check if lock is engaged
- if (sens == HIGH) {
- millisOp[h] = millis();
- delay(100);
- h++;
- }
- }
- if (!lockState02) {
- tempoCheck();
- // Also Test, don't mind this
- for (int i = 0; i < 8; i++) {
- Serial.print(tempo[i]);
- Serial.print(", ");
- if (i == 9) {
- Serial.println();
- }
- }
- }
- }
- void iCheck() {
- if (millisOp[1].toInt() >= millisOp[0].toInt() + tempo[0].toInt() && millisOp[1].toInt() <= millisOp[2].toInt() - tempo[0].toInt() + 400) {
- if (millisOp[2].toInt() >= millisOp[1].toInt() + tempo[1].toInt() && millisOp[2].toInt() <= millisOp[3].toInt() - tempo[1].toInt() + 400) {
- if (millisOp[3].toInt() >= millisOp[2].toInt() + tempo[2].toInt() && millisOp[3].toInt() <= millisOp[4].toInt() - tempo[2].toInt() + 400) {
- if (millisOp[4].toInt() >= millisOp[3].toInt() + tempo[3].toInt() && millisOp[4].toInt() <= millisOp[5].toInt() - tempo[3].toInt() + 400) {
- if (millisOp[5].toInt() >= millisOp[4].toInt() + tempo[4].toInt() && millisOp[5].toInt() <= millisOp[6].toInt() - tempo[4].toInt() + 400) {
- if (millisOp[6].toInt() >= millisOp[5].toInt() + tempo[5].toInt() && millisOp[6].toInt() <= millisOp[7].toInt() - tempo[5].toInt() + 400) {
- if (millisOp[7].toInt() >= millisOp[6].toInt() + tempo[6].toInt() && millisOp[7].toInt() <= millisOp[8].toInt() - tempo[6].toInt() + 400) {
- if (millisOp[8].toInt() >= millisOp[7].toInt() + tempo[7].toInt() && millisOp[8].toInt() <= millisOp[9].toInt() - tempo[7].toInt() + 400) {
- Servo01.write(180); // Change if not accurate an paglibot an servo
- Serial.println("UNLOCK YEHEY!!"); // ig kakadi an lock motor chuchu
- }
- }
- }
- }
- }
- }
- }
- }
- else {
- Serial.println("LOCKED NOO!!");
- }
- }
- void tempoCheck() {
- for (int i = 1; i <= 8; i++) {
- tempo[i - 1] = (millisOp[i].toInt() - millisOp[i - 1].toInt()) - 200;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement