Post by Admin on Jun 10, 2016 13:31:43 GMT
Lets face it, Arduino uno is just simple microcontroller that was released less than a decade ago for wanna be programmers and hardware enthuisastics to make their own version of a robot. Let me get in to detail. Trust me this is worth trying to you mentees because you havenot gone through programming before or have most of you done with any micro controllers except for a few.
So what multi threading? you have two threads like these below .( not literally )
See whats happening? In the above image, One can see that every now a process TOTALLY INDEPENDENT of each other works. Thats multithreading. Okhay let me break it down to a DAY-TO-DAY life scenario. You are watching a movie in VLC, What if your computer couldnt run anything other that at that moment? nothing in the sense , The ENTIRE MOVIE had to be OVER !! WHAT? Yea! Or may be you should stop it to run a file copying that runs in the background. Imagine your processor could only do one thing only at a time as it was a dumb computer. Well, Now you neednt worry because every other computer was made with atleast false multithreading or pseudo.
The CPU was not literally divided into Cores. The arduino is not either. But not your laptops with an intel i7 or i5 processors. HYPERTHREADING is something you can read about.
Anyway not going off topic here. Let me continue..
SO IT GOES LIKE THIS , Your processor can do two things. Compute and allocate memory . BUT NO ONE TOLD IT TO DO ONE AT A TIME NOW? Check the graph below to understand.. ( If you still dont understand, ping me, ask me in the group )
We will start with Timerone.h available here to download as a library as it is not there natively.
And heres an example .. Read the code and I will be posting more on timers..
So what multi threading? you have two threads like these below .( not literally )
See whats happening? In the above image, One can see that every now a process TOTALLY INDEPENDENT of each other works. Thats multithreading. Okhay let me break it down to a DAY-TO-DAY life scenario. You are watching a movie in VLC, What if your computer couldnt run anything other that at that moment? nothing in the sense , The ENTIRE MOVIE had to be OVER !! WHAT? Yea! Or may be you should stop it to run a file copying that runs in the background. Imagine your processor could only do one thing only at a time as it was a dumb computer. Well, Now you neednt worry because every other computer was made with atleast false multithreading or pseudo.
The CPU was not literally divided into Cores. The arduino is not either. But not your laptops with an intel i7 or i5 processors. HYPERTHREADING is something you can read about.
Anyway not going off topic here. Let me continue..
SO IT GOES LIKE THIS , Your processor can do two things. Compute and allocate memory . BUT NO ONE TOLD IT TO DO ONE AT A TIME NOW? Check the graph below to understand.. ( If you still dont understand, ping me, ask me in the group )
We will start with Timerone.h available here to download as a library as it is not there natively.
And heres an example .. Read the code and I will be posting more on timers..
#include <TimerOne.h>
// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.
const int led = LED_BUILTIN; // the pin with a LED
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(150000);
Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
Serial.begin(9600);
}
// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables
void blinkLED(void)
{
if (ledState == LOW) {
ledState = HIGH;
blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
}
// The main program will print the blink count
// to the Arduino Serial Monitor
void loop(void)
{
unsigned long blinkCopy; // holds a copy of the blinkCount
// to read a variable which the interrupt code writes, we
// must temporarily disable interrupts, to be sure it will
// not change while we are reading. To minimize the time
// with interrupts off, just quickly make a copy, and then
// use the copy while allowing the interrupt to keep working.
noInterrupts();
blinkCopy = blinkCount;
interrupts();
Serial.print("blinkCount = ");
Serial.println(blinkCopy);
delay(100);
}
QUESTION)) using timers implement polling as a function poll() for a GPIO pin as input and determine stopping and starting of an LED green while LED RED is blinking at 1HZ uninterrupted.