CCCPaste

Hysteresis Threshold Code

const int sensorPin = A0;
const int outputPin = 9;

const float upperThreshold = 75.0;
const float lowerThreshold = 70.0;

bool fanState = false;

void setup() {
  pinMode(outputPin, OUTPUT);
  digitalWrite(outputPin, LOW);
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);  
  float temperature = (sensorValue * 5.0 / 1023.0) * 100.0;
  
  Serial.print("Temperature: ");
  Serial.println(temperature);

  if (temperature >= upperThreshold && !fanState) {
    digitalWrite(outputPin, HIGH);  // Turn on fan/LED
    fanState = true;
  }
  else if (temperature <= lowerThreshold && fanState) {
    digitalWrite(outputPin, LOW);  // Turn off fan/LED
    fanState = false;
  }

  delay(500);  
}