Project problem: multithreading

Hello everyone,

I’m Tom and this is my first post on this forum. I’m currently working on a schoolproject with the Arduino Mega. I am making a device to help (almost) blind people, the device is controlled by an Arduino. Additionally I have 5 ultrasonic sensor connected to the arduino. At this moment the signal from the ultrasonic sensor is send to a LED to give the signal that an object is near. I know blind people won’t be able to see that, the LEDs are easier to use while programming, I wil change it later.

My code at this moment:
I had to put the code into a .TXT-file, because my post was to long. It is attached to the post.

The problem which I’m faced with has to do with programming. The code is almost ‘finished’, I only need to transfer the signal of the ultrasonic sensor to the LED. I want to do that through this code:

if (distance1 == 0) {
digitalWrite(ledPin1, HIGH);
delay(0);
digitalWrite(ledPin1, LOW);
delay(200);
}
if (distance1 > 0 && distance1 < 10) {
digitalWrite(ledPin1, HIGH);
delay(200);
digitalWrite(ledPin1, LOW);
delay(0);
}
if (distance1 >= 10 && distance1 < 20) {
digitalWrite(ledPin1, HIGH);
delay(175);
digitalWrite(ledPin1, LOW);
delay(25);
}
if (distance1 >= 20 && distance1 < 30) {
digitalWrite(ledPin1, HIGH);
delay(150);
digitalWrite(ledPin1, LOW);
delay(50);
}
if (distance1 >= 40 && distance1 < 50) {
digitalWrite(ledPin1, HIGH);
delay(125);
digitalWrite(ledPin1, LOW);
delay(75);
}
if (distance1 >= 50 && distance1 < 70) {
digitalWrite(ledPin1, HIGH);
delay(100);
digitalWrite(ledPin1, LOW);
delay(100);
}
if (distance1 >= 70 && distance1 < 100) {
digitalWrite(ledPin1, HIGH);
delay(75);
digitalWrite(ledPin1, LOW);
delay(125);
}
if (distance1 >= 100 && distance1 < 150) {
digitalWrite(ledPin1, HIGH);
delay(50);
digitalWrite(ledPin1, LOW);
delay(150);
}
if (distance1 >= 150 && distance1 < 200) {
digitalWrite(ledPin1, HIGH);
delay(25);
digitalWrite(ledPin1, LOW);
delay(175);
}
if (distance1 >= 200) {
digitalWrite(ledPin1, HIGH);
delay(0);
digitalWrite(ledPin1, LOW);
delay(200);
}

The only problem is the delays, I want all the 5 LEDs to blink their distance at the same time, not after eachother. As you are all aware, an Arduino can only process 1 thread at the time. I have done my research and tried to fix this with the library ‘TimedAction’. Ít didn’t work out for me, I can’t get it to work for me. All of the tutorials I watched aren’t sufficient enough for this more complex piece of coding.

If there is anyone who can give me a hint on what to do, or on what other libary I can use I would really appreciate it.

Thanks in advance,

Tom

PS_1: If you have questions, please ask!
PS_2: I’m a beginner in coding, please forgive me for my messy coding
PS_3: Where ‘//led pin code//’ stands, there will the ultrasonic to LED sequence come
PS_4 I’m very sorry for my English :smiley:

Arduino code for blinds.txt (8.94 KB)

check out the sticky post at the top of this forum:
Using millis() for timing. A beginners guide

It will show you how to do delays without using delay() so your program can go on to do other things!

The code is almost 'finished'

Nope.

I had to put the code into a .TXT-file, because my post was to long.

Your code is too long.
That should have been a clue.
You need to do something about that.

//echoPin1 loop
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance_output1 = (duration1*vgeluidcmus)/2;

//echoPin2 loop
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance_output2 = (duration2*vgeluidcmus)/2;

//echoPin3 loop
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distance_output3 = (duration3*vgeluidcmus)/2;

//echoPin4 loop
digitalWrite(trigPin4, LOW);
delayMicroseconds(2);
digitalWrite(trigPin4, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin4, LOW);
duration4 = pulseIn(echoPin4, HIGH);
distance_output4 = (duration4*vgeluidcmus)/2;

//echoPin5 loop
digitalWrite(trigPin5, LOW);
delayMicroseconds(2);
digitalWrite(trigPin5, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin5, LOW);
duration5 = pulseIn(echoPin5, HIGH);
distance_output5 = (duration5*vgeluidcmus)/2;

Single function.
Do it.

What is the function of code lines like this?

delay(0);

Also see Demonstration code for several things at the same time to get some ideas.

jremington:
What is the function of code lines like this?

delay(0);

A delay of a few microseconds? :slight_smile:

I recommend that you use the NewPing library. It includes an example of using 15 ultrasonic sensors so 5 sensors should not be a problem.

Arrays and loops could reduce the size and complexity of your sketch quite a bit.

For example:

//pinconstants
const byte tempPin = A0;

const byte SensorCount = 5;
const byte EchoPins[SensorCount] = {2, 4, 6, 8, 10};
const byte TriggerPins[SensorCount] = {3, 5, 7, 9, 11};
const byte LEDPins[SensorCount] = {12, 13, 14, 15, 16};  // Requires Arduino MEGA

unsigned long Duration[SensorCount];
const unsigned long MaxDuration =  29000; // About 5 meters distance
unsigned DistanceCM[SensorCount];

const unsigned long BlinkMicrosPerCM = 100;

//only one
float TEMP, vgeluidms, vgeluidcmus;
int readingTEMP;

void setup()
{
  //pinmodes
  analogReference(INTERNAL1V1);  // Requires Arduino MEGA

  for (int i = 0; i < SensorCount; i++)
  {
    pinMode(EchoPins[i], INPUT);
    pinMode(TriggerPins[i], OUTPUT);
    pinMode(LEDPins[i], OUTPUT);
  }

  //developer
  Serial.begin(9600);
  //developer
}

void loop()
{
  // Read temperature
  readingTEMP = analogRead(tempPin);
  TEMP = readingTEMP / 9.31;

  // Calculate speed of sound
  vgeluidms = 331 + (0.6 * TEMP);
  vgeluidcmus = ((vgeluidms * 100) / 1000000);

  // Read all sensors
  for (int i = 0; i < SensorCount; i++)
  {
    digitalWrite(TriggerPins[i], LOW);
    delayMicroseconds(2);
    digitalWrite(TriggerPins[i], HIGH);
    delayMicroseconds(10);
    digitalWrite(TriggerPins[i], LOW);
    uint32_t d =  pulseIn(EchoPins[i], HIGH, MaxDuration); // pulse w/ timeout
    if (d == 0)
      d = MaxDuration;  // No echo? Figure 5 meters.
    Duration[i] = (d + (9 * Duration[i])) / 10; // Filter
    DistanceCM[i] = (Duration[i] * vgeluidcmus) / 2;
    if (DistanceCM[i] > 500)
      DistanceCM[i] = 500;  // Limit to 5 meters
    // Allow echoes to die down before reading the next sensor
    if (d < MaxDuration)
      delayMicroseconds(MaxDuration - d);
  }

  //developer
  for (int i = 0; i < SensorCount; i++)
  {
    Serial.print(DistanceCM[i]);
    Serial.print("-");
  }
  Serial.println();
  //developer

  //  Once per second, pulse the LEDs based on distances
  unsigned long currentMillis = millis();
  static unsigned long PreviousStartTime = 0;
  if (currentMillis - PreviousStartTime >= 1000)
  {
    PreviousStartTime += 1000;
    // Turn all LEDs on
    for (int i = 0; i < SensorCount; i++)
    {
      digitalWrite(LEDPins[i], HIGH);
    }
    bool lightsStillOn = true;
    unsigned long blinkStartTime = millis();
    while (lightsStillOn)
    {
      unsigned long blinkDuration = millis() - blinkStartTime;
      lightsStillOn = false;
      for (int i = 0; i < SensorCount; i++)
      {
        // LED lights up longer the closer the object is
        // At 100 CM it lights 400 milliseconds
        // At 200 CM it lights 300 milliseconds
        if (blinkDuration < (500 - DistanceCM[i]))
          lightsStillOn = true;
        else
          digitalWrite(LEDPins[i], LOW);
      }
    }
  }
}

jremington:
What is the function of code lines like this?

delay(0);

surprisingly i am seeing it two times today, earlier i was going through the sketch file of ESP8266HttpClient.cpp and met couple of delay(0)'s across the page

delay(0); could be re-written as yield(); on an ESP8266. It gives the WiFi and other processes a chance to run, which would normally take less than a millisecond.