Multiple tasks from one if-statment

Hello, i'm a newbie here, and i wonder if there is any solutions to my problem. I try to make a sketch that, when an input is from 0-2v, a green LED is on, from 2-3.5V a yellow LED is on an from 3.5-4.99 a red one is on. At 5V, i want the red LED to blink and a buzzer to sound. The problem is that i want the red LED to blink at the same time as the buzzer sound, and not in a loop. is that possible?
This is the sketch i am using:

int redPin=3;
int yellowPin=4;
int greenPin=5;
int readPin=A1;
int buzzer = 12;
int readVal;
float V2;
float tr1=2;
float tr2=3.5;
float trAlarm=5;
int delayAlarm=10;
int delayAlarm2=200;

void setup() {
  Serial.begin(9600);

pinMode(redPin,OUTPUT);
pinMode(yellowPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(readPin,INPUT);
pinMode(buzzer,OUTPUT);
}

void loop() {
  
   readVal=analogRead(readPin);
  V2=(5./1023.)*readVal;

if (V2<tr1){
digitalWrite(greenPin,HIGH);
}
else
{
digitalWrite(greenPin,LOW);
 }
  
if(V2>tr2){
 digitalWrite(greenPin,LOW);
}
if(V2>tr1 && V2<tr2){
  digitalWrite(yellowPin,HIGH);
}
else{
  digitalWrite(yellowPin,LOW);
}
if(V2>tr2){
  digitalWrite(redPin,HIGH);
}
else{
  digitalWrite(redPin,LOW);
}
  if(V2==trAlarm){
 digitalWrite(redPin,HIGH);    
  delay(delayAlarm2);
  digitalWrite(redPin,LOW);
  delay(delayAlarm);
    unsigned char i;
 for(i=0;i<80;i++)
     {
      digitalWrite(buzzer,HIGH);
      
      delay(1);
      digitalWrite(buzzer,LOW);
      delay(1);
   }
   for(i=0;i<100;i++)
    {
      digitalWrite(buzzer,HIGH);
      delay(2);
      digitalWrite(buzzer,LOW);
      delay(2);
    }}
    Serial.print("potensiometer Volt er ");
  Serial.println(V2);

}

Use millis

For extra information and examples look at

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

You can use the tone() function.
That will produce a tone on your piezzo while running other code.
Please in IDE press ctrl-t. It will line out your code. Then post again...

If your buzzer is an active buzzer, wire the buzzer (+) leg to redPin (3) and remove the buzzer loop.

Thanks, greatful for your reply's. Will check out these solutions.
By the way, here the sketch is "lined out".

int redPin = 3;
int yellowPin = 4;
int greenPin = 5;
int readPin = A1;
int buzzer = 12;
int readVal;
float V2;
float tr1 = 2;
float tr2 = 3.5;
float trAlarm = 5;
int delayAlarm = 10;
int delayAlarm2 = 200;

void setup() {
  Serial.begin(9600);

  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(readPin, INPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {

  readVal = analogRead(readPin);
  V2 = (5. / 1023.) * readVal;

  if (V2 < tr1) {
    digitalWrite(greenPin, HIGH);
  } else {
    digitalWrite(greenPin, LOW);
  }

  if (V2 > tr2) {
    digitalWrite(greenPin, LOW);
  }
  if (V2 > tr1 && V2 < tr2) {
    digitalWrite(yellowPin, HIGH);
  } else {
    digitalWrite(yellowPin, LOW);
  }
  if (V2 > tr2) {
    digitalWrite(redPin, HIGH);
  } else {
    digitalWrite(redPin, LOW);
  }
  if (V2 == trAlarm) {
    digitalWrite(redPin, HIGH);
    delay(delayAlarm2);
    digitalWrite(redPin, LOW);
    delay(delayAlarm);
    unsigned char i;
    for (i = 0; i < 80; i++) {
      digitalWrite(buzzer, HIGH);

      delay(1);
      digitalWrite(buzzer, LOW);
      delay(1);
    }
    for (i = 0; i < 100; i++) {
      digitalWrite(buzzer, HIGH);
      delay(2);
      digitalWrite(buzzer, LOW);
      delay(2);
    }
  }
  Serial.print("potensiometer Volt er ");
  Serial.println(V2);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.