Advertisement
pleasedontcode

Hysteresis Threshold Code

Aug 19th, 2024
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.74 KB | Source Code | 0 0
  1. const int sensorPin = A0;
  2. const int outputPin = 9;
  3.  
  4. const float upperThreshold = 75.0;
  5. const float lowerThreshold = 70.0;
  6.  
  7. bool fanState = false;
  8.  
  9. void setup() {
  10.   pinMode(outputPin, OUTPUT);
  11.   digitalWrite(outputPin, LOW);
  12.   Serial.begin(9600);
  13. }
  14.  
  15. void loop() {
  16.   int sensorValue = analogRead(sensorPin);  
  17.   float temperature = (sensorValue * 5.0 / 1023.0) * 100.0;
  18.  
  19.   Serial.print("Temperature: ");
  20.   Serial.println(temperature);
  21.  
  22.   if (temperature >= upperThreshold && !fanState) {
  23.     digitalWrite(outputPin, HIGH);  // Turn on fan/LED
  24.     fanState = true;
  25.   }
  26.   else if (temperature <= lowerThreshold && fanState) {
  27.     digitalWrite(outputPin, LOW);  // Turn off fan/LED
  28.     fanState = false;
  29.   }
  30.  
  31.   delay(500);  
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement