JinhaKangLAB / Arudino-Codes

0 stars 0 forks source link

Mirage_Smooth #1

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 = 60;

boolean isOn = false; boolean isOff = false;

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; } startMotor(); }

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 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) stopMotor();

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

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

startMotor();

}

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

void startMotor() { // 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 stopMotor() { // 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);

isOn = false; isOff = true; } // if (distance <= 60){ // Serial.println(distance); // print the distance // } // else { // Serial.println("distance: "); // Serial.println(distance); // } //}

JinhaKangLAB commented 1 year ago

https://www.makerguides.com/sharp-gp2y0a21yk0f-ir-distance-sensor-arduino-tutorial/