Arduino Tricks #1 | Parallel Programming gives Better Perfomance



Hi Guys, here's another trick when it comes to making your Arduino Projects perform faster and get consistent results.

If maalala ninyo from the time na nagstart kayo mag learn ng Microcontroller programming like Arduino Boards(ATMEL CHIPS), Most of us ay dumaan sa Blink Example kung saan ito yung ating naging first step and foundation para maunawaan natin how does Arduino UNO works.

And that famous line on the loop:

digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);


While this tends to be exciting at first, but when you start building your projects incorporating a lot of sensors on it, this tends out to be na nagiging slow response na ang Arduino board mo especially if you are building projects having remote or bluetooth or wifi functionality.

Here's why?:

Watch the video below and observe the comparison of two Arduino boards with same functions which is to blink the led every 90millisecs and at the same time sweep the servo back and fourth from 0 to 180 degrees on a constant speed of 15msPWM.

One board uses the traditional programing using delay() function to blink the led then afterwards sweep the servo, the other Arduino Board will also blink the led every 1sec and at the same time sweeps the servo pero this time using the Parallel theory.

With Series Programming, what happens is, everytime naglalagay ka ng delay sa program mo, everything stops on that section of the loop because arduino will complete the delay first. So yung Led everytime it blinks while waiting to complete the delay for blink high and blink low, the servo stops. after that, yung servo naman ang gagalaw pero yung led maghihintay na macomplete ng servo yung task and so on. What if may other sensor ka pa after ng servo at led? That is the reason most new coders in arduino experiences delays on data or responses from their sensors and devices, worst nagoof or hang yung ibang modules. very common mistake ito lalo na samga gumagamit ng gps at gsm modules.

Programmer 2 used Parallel approach, wherein inalis nya ang delays sa blink and only used this function for the servo but not to delay the loop., he used it for sending PWM on the servo. while the led this time uses time intervals from the clock pin of arduino. The result is: walang pakialam yun led kung gaano kabilis or kabagal mo gusto pagalawin yung serv, the led will just blink according to the time interval, while servo also doesn't mind the blink function na ginagawa ng led either mabagal o mabilis ang intervals ng blink the servo will just continue to do his thing.

So now that you understand the effects in improper use of delay() function,you will expect na slow ang performance ng project mo compare sa other projects na ginamit ng tama ang delay() function at ang Interval functions();


Check below the actual codes used by the two programmers so you can compare and better understand the functions used.

Hope this tip will help you in your next projects! See you again on the next tutorial guys!


Source code by Programmer 1:


#include <Servo.h>
Servo myservo;
int pos = 0;
const int ledPin =  LED_BUILTIN;

void setup() {
  myservo.attach(9);
  pinMode(ledPin, OUTPUT);
}

void loop() {

  digitalWrite(ledPin,HIGH);
  delay(90);
  digitalWrite(ledPin,LOW);
  delay(90);
 
 
  for (pos = 0; pos <= 180; pos += 1) {
    myservo.write(pos);
    delay(15); // 2.7 secs
  }

digitalWrite(ledPin,HIGH);
  delay(90);
  digitalWrite(ledPin,LOW);
  delay(90);

  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    delay(15);  // 2.7 secs
  }

}


Source code by Programmer 2:

#include <Servo.h>
Servo myservo;
int pos = 0;
const int ledPin =  LED_BUILTIN;
int ledState = LOW;


unsigned long prev_Mi = 0;
const long inter = 90;

void setup() {
  myservo.attach(9);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) {
    myservo.write(pos);
    Blink();
    delay(15); // 2.7 secs
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);
    Blink();
    delay(15);  // 2.7 secs
  }

}

void Blink() {
  unsigned long cur_Mi = millis();

  if (cur_Mi - prev_Mi >= inter) {
    prev_Mi = cur_Mi;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

Comments

  1. Sir ano pong pinag kaiba ng.....
    "const int" sa "int"?

    anong ibig sabihin ng codes na to Sir?
    "unsigned long prev_Mi = 0;"
    para san po yung "unsigned long".?

    anong ibig sabihin ng codes na to Sir?
    const long inter = 90;

    anong ibig sabihin ng codes na to Sir?
    void Blink() {
    unsigned long cur_Mi = millis();

    if (cur_Mi - prev_Mi >= inter) {
    prev_Mi = cur_Mi;
    if (ledState == LOW) {
    ledState = HIGH;
    } else {
    ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
    }


    ReplyDelete
    Replies
    1. Yung const int means hindi na magbabago yung value na inasign sa variable. while yung int alone the value of the variable can be modified by the program. Yung ibang functions na tinatanong mo, you can search it on Arduino Library, ang importante my reference ka na on how it is being applied in programming :)

      Delete

Post a Comment

Popular Posts