Search code examples
arduinomultitaskingtimedelay

How run servo and ultrasonic sensor concurently with millis on arduino?


I have a problem with the servo and ultrasonic that can not run concurently. My code works at the moment as follow : servo will move every 10 seconds and ultrasonic always detect the range and display the result on LED. I was looking for references on google and I found out that the delay caused servo and ultrasonic can't run together. multitasking required in order to use the millis (). but I still can not use the function millis (). how to change the delay in my code with millis()?

Thanks for helping me!

#include <Servo.h>
#define trigPin 13
#define echoPin 12
#define ledM 10
#define ledK 9
#define ledH 8

Servo ikiservo;  //servo 
int pos = 0;    //set positon servo
int pinSpeaker = 4;  //buzzer

void setup() {
  Serial.begin (9600); //SERIAL
  ikiservo.attach(3); //servo
  pinMode(pinSpeaker, OUTPUT);  
  pinMode(trigPin, OUTPUT); //ultrasonic
  pinMode(echoPin, INPUT); //ultrasonic
  pinMode(ledM, OUTPUT);
  pinMode(ledK, OUTPUT);
  pinMode(ledH, OUTPUT);
}

void loop() {
  //-------------SERVO----------

  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  1 degree 
    ikiservo.write(pos);            
    delay(10000);       // waits 10s for the servo to reach the position 
  }

  //---------ULTRASONIC-----------
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  delay (400);

  if (distance > 4 && distance <7) {  // turn on ledM when distance 4<distance<7
    digitalWrite(ledM,HIGH); 
    digitalWrite(ledK,LOW);
    digitalWrite(ledH,LOW);
    playTone(300, 160);          //buzzer ON
    delay(1000);
    //buzzer
  } else if(distance > 2 && distance <4) {// turn on ledK when 7<distance<4
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,HIGH);
    digitalWrite(ledH,LOW);
    playTone(0, 0);
    delay(1000);
  } else if(distance <2) {//turn on ledH when distance<2
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,LOW); 
    digitalWrite(ledH,HIGH);
    playTone(0, 0);
    delay(1000);
  } else{                      //turn off all
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,LOW);  
    digitalWrite(ledH,LOW);
    playTone(0, 0);
    delay(1000);
  }
}

void playTone(long duration, int freq) { //funtion for add tone on buzzer
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

Solution

  • You are not waiting for 10 seconds, but for 15 minutes. Probably that one is the only issue in your code.

    Just write this

    for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
    {                                 // in steps of  1 degree 
        ikiservo.write(pos);            
        delay(100); // waits 0.1s for the servo to reach the position
                    // so total time will be 9 seconds
    }
    

    If, on the other way, you want to span the 90° area in 10 seconds, then go back, the code is

    void loop() {
        //-------------SERVO----------
    
        for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
        {                                  // in steps of  1 degree 
            ikiservo.write(pos);
    
            //---------ULTRASONIC-----------
            long duration, distance;
            digitalWrite(trigPin, LOW); 
            delayMicroseconds(2);
            digitalWrite(trigPin, HIGH);
            delayMicroseconds(10);
            digitalWrite(trigPin, LOW);
            duration = pulseIn(echoPin, HIGH);
            distance = (duration/2) / 29.1;
            Serial.print(distance);
            Serial.println(" cm");
    
            if (distance > 4 && distance <7) {  // turn on ledM when distance 4<distance<7
                digitalWrite(ledM,HIGH); 
                digitalWrite(ledK,LOW);
                digitalWrite(ledH,LOW);
            } else if(distance > 2 && distance <4) {// turn on ledK when 7<distance<4
                digitalWrite(ledM,LOW);
                digitalWrite(ledK,HIGH);
                digitalWrite(ledH,LOW);
            } else if(distance <2) {//turn on ledH when distance<2
                digitalWrite(ledM,LOW);
                digitalWrite(ledK,LOW); 
                digitalWrite(ledH,HIGH);
            } else{                      //turn off all
                digitalWrite(ledM,LOW);
                digitalWrite(ledK,LOW);  
                digitalWrite(ledH,LOW);
            }
    
            delay(100); // waits 0.1s for the servo to reach the position 
        }
    }
    

    I had to remove the tone, because the way you use it does not allow other "processes" to work concurrently. Just see if it is the approach you want;if it is report back and we can see how to implement it also with the buzzer