Reading potentiometer value with other functions

I am trying to get a potentiometer to read the value in the serial monitor, while doing different things for different values. If the value is less then 500, then the buzzer is off, and the led is on. If the value is greater then 800, the led will turn off and the buzzer will be on constantly. Those two functions work well, but the third one is the issue. It is supposed to make the buzzer beep every 0.5 seconds if the value is between 500 and 800, but it stops the output to every 0.5 seconds like the buzzer, instead of the set 0.01 seconds.

Basically, I want a constant output no matter what the buzzer/other functions are is doing.

It is also worth noting this this function will be used in a bigger project, and I will hopefully be able to use the same method for similar functions.

Thanks in advance, and all help is greatly appreciated.

Here is my current code:

#define led 8
#define pot1 A1
#define buz 7


float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}


void setup() {
  
  pinMode(led, OUTPUT);
  pinMode(buz, OUTPUT);
  Serial.begin(9600);
}


void loop() {
 
  int analogValue = analogRead(pot1);
 
  Serial.println(analogValue);
  delay(10);
  if(analogValue < 500){
    digitalWrite(led, HIGH);
    digitalWrite(buz, LOW);  
}
  else if(analogValue < 800){
    digitalWrite(led, LOW);
    
    digitalWrite(buz, HIGH);
    delay(500);
    digitalWrite(buz, LOW);
    delay(500);
    
  }
  
  else if(analogValue > 800){
    digitalWrite(buz, HIGH);
    digitalWrite(led, LOW);
  }
  
  
}

Since you are using Serial, have you used it to track down where the problem is? Print the values associated with the function.

It’s not just serial. It is where one delay for the buzzer delays everything, when I only want it to delay the buzzer.

If potValue is <500, then both conditions are true.

try this

if (analogValue > 800){...
else if (analogValue < 500){...
else {...

Leo..

Hi,
Can I suggest you forget ifs and else ifs and look at switch..case?

Tom... :grinning: :+1: :coffee: :australia:

'delay()' delays everything. If you want something to happen periodically without making everything else wait, look at:
File->Examples->02.Digital->BlinkWithoutDelay

Thanks everyone. I will end up using ‘switch case’ for the potentiometers in the project, and the ‘blink without delay’ part for a different part. I don’t know how I didn’t know about the ‘switch case’, so all the help is very appreciated.