// https://wokwi.com/projects/371823955470390273
// https://forum.arduino.cc/t/weight-sensor-buzzer-problem/1153593

# define anLED  7

void setup() {
  Serial.begin(115200);
  Serial.println(" scale thing");

  pinMode(anLED, OUTPUT);
  digitalWrite(anLED, LOW);
}

bool doIt;    // flag for mass went over 100. is only ever true or false.

void loop() {
  delay(250);   // just to slow down printing and stuff

// read the mass. slide fader is proxy for all the scale stuff 
  int mass = analogRead(A0) / 8;  // will range from 0 to 127 or so

  Serial.print("mass = "); Serial.println(mass);

  if (mass < 40) {
    if (digitalRead(anLED) == HIGH) {
      Serial.println("          turn the LED off.");
      digitalWrite(anLED, LOW);
    }
  }

  static bool oldOverweight;
  bool newOverweight = mass > 100;

//  if (newOverweight and not oldOverweight) {
  if (newOverweight && !oldOverweight) {
    doIt = true;
  }
  oldOverweight = newOverweight;    // for next time

// now doIt will say if the mass _became_ greater than 100
  if (doIt) {
    Serial.println("          turn the LED on.");
    digitalWrite(anLED, HIGH);
    doIt = false;   // don't do it again! must _become_ 100 again first.
  }

  delay(25);    // poor man's debouncing. crude but effective
}