Adafruit BDSM Alarm

1. Adafruit Metro Mini
2. MG90S Servo
3. 7 Segment Digit Display
4. Toggle Switch
5. Momentary Button (2)
6. AAA Battery (3)





// Alarm Choker
// Vije Miller

#include "SevSeg.h"
#include <Servo.h>

SevSeg sevseg;
int digit = 0;

Servo choke;
const int servo_pin = 10;
const int start = 180;
const int finish = 0;
int pos = 0;

const int top = 11;
const int bottom = 12;
int top_state = 0;
int bot_state = 0;

const unsigned long sec = 1000;
const unsigned long hr = 3600 * sec;
int cd = 0;

int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
// digit
byte numDigits = 1;
byte digitPins[] = {};
// A, B, C, D, E, F, G, DP
byte segmentPins[] = {3, 2, 8, 7, 6, 4, 5, 9};
bool resistorsOnSegments = true;
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(50);

// servo
choke.attach(servo_pin);
choke.write(start);

// buttons
pinMode(top, INPUT);
pinMode(bottom, INPUT);
}

void loop() {
bot_state = digitalRead(bottom);
sevseg.setNumber(digit);
sevseg.refreshDisplay();
if (bot_state != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (bot_state != buttonState) {
buttonState = bot_state;
if (buttonState == HIGH) {
if (digit == 9) {
digit = 0;
sevseg.setNumber(digit);
sevseg.refreshDisplay();
} else {
digit++;
sevseg.setNumber(digit);
sevseg.refreshDisplay();
}
}
}
}

// start button
top_state = digitalRead(top);
if (top_state == HIGH) {
if (digit == 0) {
countdown();
} else {
alarm();
}
}
lastButtonState = bot_state;
}

void alarm() {
for (int w = 0; w < 3; w++) {
sevseg.setNumber(digit);
sevseg.refreshDisplay();
delay(500);
sevseg.blank();
sevseg.refreshDisplay();
delay(500);
}

for (int k = digit; k > 0; k--) {
sevseg.setNumber(k,0);
sevseg.refreshDisplay();
delay(hr);
}
sevseg.setNumber(0);
sevseg.refreshDisplay();
delay(1000);
countdown();
}

void countdown() {
for (int i = 9; i > 0; i--) {
sevseg.setNumber(i);
sevseg.refreshDisplay();
delay(1000);
}
sevseg.setNumber(0);
sevseg.refreshDisplay();
wakeup();
}

void wakeup() {
for (int x = start; x > finish; x--) {
choke.write(x);
delay(20);
}
}

@VijeMiller
KE=½mv²