-1

So I am "playing" with an Arduino Uno R3. With an ultrasonic sensor and LED's I programmed the Arduino to measure a distance, and if it gets too close to something, the LEDs start to shine one by one. Now I want to implement a buzzer that beeps in a different interval for each LED shining. Basically like a distance sensor in cars for parking.

My problem is that I tried it with a simple activate, delay, deactivate but figured out fast that it delays the whole program. Does anyone know how I can solve my problem?

#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
#define BUZZER 2
unsigned char led[8]={3,4,5,6,7,8,9,10};
#define MAX 50

void setup() {
   Serial.begin(9600);
   delay(1000);
   for(int i=0;i<8;i++){
      pinMode(led[i], OUTPUT);
      digitalWrite(led[i], LOW);
   }
   pinMode(BUZZER, OUTPUT);
}

void loop() {
  long a=0;
  SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
  Serial.println(a);
  distance_measure(a, sr04);
  
}

void distance_measure(long a, SR04 sr04){
  a=sr04.Distance();
   for(int i=7;i>=0;--i){
     if(a<=(i+1)*(MAX/8)){
       digitalWrite(led[i], HIGH);
       beep(i);
     }
     else{
       digitalWrite(led[i],LOW);
     }
   }
   
  delay(50);
}

void beep(int i){
  digitalWrite(BUZZER, HIGH);
  delay(50);
  digitalWrite(BUZZER, LOW);
  delay(i*300);
  
}

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 2
    You need to use a timer for that. Timers are special parts on the microcontroller that can periodically call a function. Take a look at the Timer1 lib in Arduino – Alexander Daum Jul 31 '20 at 13:41
  • You will want to use timer interrupts, which let you set up a later action to fire when a hardware timer fires. A whole guide is probably too long to share here, but you should be able to find articles with that keyword. An alternative would be to write a program that doesn't actually delay, but keeps track of the current time and uses variables to track when your various actions should fire. – nanofarad Jul 31 '20 at 13:42
  • Do small things very fast, at the end of the day it looks like you are doing several stuff at the same time. – Jose Jul 31 '20 at 13:45
  • see the BlinkWithoutDelay basic example. and then this https://arduino.stackexchange.com/questions/77027/running-arduino-with-2-outputs-and-2-inputs/77028#77028 – Juraj Jul 31 '20 at 14:26
  • 2
    @AlexanderDaum, yes you can blink a LED in timer interrupt, but you can't do anything more complex there so it is not the right way to do several things at once. Arduino uses a timer or RTC to for a miilis() function and this can be used to time things with simple `if (millis() - previousMillis > INTERVAL)`. else timers are used for pulse trains generation, fast pulse changes capture etc – Juraj Jul 31 '20 at 15:57

1 Answers1

-1

So i made it work like i want it to. Can i improve it a bit more because i dont think i made it the best way possible.

void beep(int i){
  unsigned static long previousMillis = 0;
  unsigned long currentMillis = millis();
  unsigned int j=i;
  if(currentMillis - previousMillis > (INTERVAL*j)){
    previousMillis = currentMillis;
    if(digitalRead(BUZZER)==0){
      digitalWrite(BUZZER, HIGH);
    }
    else if(currentMillis - previousMillis > (INTERVAL/j)){
      previousMillis = currentMillis;
      digitalWrite(BUZZER, LOW);
    }
  }
}