JinhaKangLAB / Arudino-Codes

0 stars 0 forks source link

Mirage_Timer #2

Open JinhaKangLAB opened 1 year ago

JinhaKangLAB commented 1 year ago

define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

const int numReadings = 10; int readings[numReadings]; int readIndex = 10;

int total = 0; int average = 0;

const int RELAY_PIN1 = 2; const int RELAY_PIN2 = 3;

const int threshold = 40;

boolean isOn = false; boolean isOff = false;

// Timer variables long previousMillis = 0; long interval = 30000; // 1000 is 1 second

void setup() { Serial.begin(9600); // start the serial port

pinMode(RELAY_PIN1, OUTPUT); pinMode(RELAY_PIN2, OUTPUT);

for (int i = 0; i < numReadings; i ++) { readings[i] = 0; } inflate(); }

void loop() {

// 5v float volts = analogRead(sensor) 0.0048828125; // value from sensor (5/1024) int distance = 13 * pow(volts, -1); // worked out from datasheet graph Serial.println(distance); delay(100); // slow down serial port

total = total - readings[readIndex];

readings[readIndex] = distance; total = total + readings[readIndex]; readIndex++; if (readIndex >= numReadings) { readIndex = 0; }

average = total / numReadings; //Serial.println(average);

if (average <= threshold && isOn) { // Someone is here, but we're already on! (this prevents it from constantly clicking when someone has triggered it) deflate();

} else if (average <= threshold && isOff) { // Someone is here and blower is currently off // turn on vacuum deflate();

// Timer logic
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
  previousMillis = currentMillis;

  // Turn off both motors
  stopAllMotors();
  Serial.println("timer is up!");
}

} else if (average > threshold && isOff) {

inflate();

}

// DO WE NEED THIS? IS IT GOOD? BAD?? // IF BAD, KILL ME!!!!! delay(1);

}

void inflate() { // start the motor! Serial.println("blower is starting"); Serial.println("Set blower Relay HIGH"); digitalWrite(RELAY_PIN1, HIGH);

// stop the other motor! Serial.println("vaccuum is stopping"); Serial.println("Set vacuum Relay LOW"); digitalWrite(RELAY_PIN2, LOW);

isOn = true; isOff = false; }

void deflate() { // start the motor! Serial.println("vacuum is starting"); Serial.println("Set vacuum Relay HIGH"); digitalWrite(RELAY_PIN2, HIGH);

// stop the other motor! Serial.println("blower is stopping"); Serial.println("Set blower Relay LOW"); digitalWrite(RELAY_PIN1, LOW); // delay(1000); isOn = false; isOff = true; }

void stopAllMotors() { Serial.println("STOPPING ALL MOTORS");

// To get rid of clicks at interval during wait timer // Do we need to reset pinMode to INPUT temporarily here? // // int relay1State = digitalRead(RELAY_PIN1); // int relay2State = digitalRead(REALY_PIN2); // // if(relay1State == LOW){ // //do nothing // } else { // digitalWrite(RELAY_PIN1, LOW); // } // // if(relay1State == LOW){ // //do nothing // } else { // digitalWrite(RELAY_PIN1, LOW); // }

digitalWrite(RELAY_PIN1, LOW); digitalWrite(RELAY_PIN2, LOW);

}