Skip to content

Instantly share code, notes, and snippets.

@Eladkay
Created March 9, 2020 09:31
Show Gist options
  • Save Eladkay/d3ec675812815cd25a702b267e7402f0 to your computer and use it in GitHub Desktop.
Save Eladkay/d3ec675812815cd25a702b267e7402f0 to your computer and use it in GitHub Desktop.
Vaults Project Arduino Code
int lastReading = 0;
int threshold = 18;
int hittingThres = 0;
int SECONDS_THRESHOLD = 60;
void calibrate() { // sets threshold
int readings = 0;
int NUM_CALIBRATE = 5;
for(int i = 0; i < NUM_CALIBRATE; i++) {
if(i % 2 == 1) digitalWrite(12, HIGH);
else digitalWrite(12, LOW);
readings += analogRead(0);
delay(1000);
}
digitalWrite(12, LOW);
threshold = 2 + readings / NUM_CALIBRATE;
}
void setup() {
Serial.begin(9600);
pinMode(13, INPUT); // on-off switch
pinMode(0, INPUT); // sensor
pinMode(12, OUTPUT); // LED
Serial.println("All set");
digitalWrite(12, HIGH);
calibrate();
}
void loop() {
// put your main code here, to run repeatedly:
if(!digitalRead(13)) {
//Serial.println("yes");
} else {
//Serial.println("no");
//return;
}
int reading = analogRead(0);
if(lastReading == 0) lastReading = reading;
int normalizedReading = (lastReading + reading)/2;
Serial.println(normalizedReading);
lastReading = normalizedReading;
Serial.print(" ");
if(normalizedReading >= threshold) hittingThres++;
else hittingThres--;
if(hittingThres >= SECONDS_THRESHOLD) digitalWrite(12, HIGH);
else digitalWrite(12, LOW);
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment