Hello,
Im am very new to the whole Arduino's this whole thing. So i was wondering if it was possible for an Arduino to be able to run 3 things at a time ( i had neo pixels, voice activated lights, and a fingerprint lock in mind ) if you don't mind could you also recommend what arduino i should use i was currently thinking about a Arduino Uno- R3 and the kit (http://www.adafruit.com/products/68 )
Thanks,
Lasercow1 8)
It can indeed do many things at once, but it's not really multi-tasking.
The Arduino sketch (Arduino-speak for program) has two parts: setup() which runs once and loop() which loops forever. Loop() is looped very quickly, and so code in there happens very frequently: that allows you to service many things at once.
The trap most of us fall into is to time things with delay(): eg use it to say how long an led is on and then again to say how long it's off, as described here. That effectively prevents your "multitasking" from happening since everything stops while the delay() is delaying.
So right from the word go, you should ditch delay() in favour of using millis() to time things as explained here and here.
Edit: JimboZA beat me to it
The Arduino has a single thread processor, so no multithreading. However, written properly it can give the appearance of multithreading by using the Blink Without Delay method. Since you are new to Arduino, I highly suggest you look at the examples provided with the arduino software. One of them is Blink Without Delay.
3 things at a time
At the same time, NO.
But you can do things like this:
const unsigned long TaskAtime = 25*60*1000UL; //Runs TaskA every 25 minutes
const unsigned long TaskBtime = 1000UL; //Runs TaskB every 1 second
const unsigned long TaskCtime = 2000UL; //Runs TaskC every 2 seconds
const unsigned long TaskDtime = 4000UL; //Runs TaskD every 4 seconds
unsigned long TimeA; //Times up, Task A time
unsigned long TimeB; //Times up, Task B time
unsigned long TimeC; //Times up, Task C time
unsigned long TimeD; //Times up, Task D time
// etc.
unsigned long microsNow;
//==========================================================
void setup()
{
microsNow = micros(); //Initailize
TimeA = millis(); //Initailize
TimeB = TimeA; //Initialize
TimeC = TimeA; //Initialize
TimeD = TimeA; //Initialize
pinMode(13,OUTPUT); //
pinMode(12,OUTPUT); //
pinMode(11,OUTPUT); //
pinMode(10,OUTPUT); //
} // >>>>>>>>>>>>>> END OF setup() <<<<<<<<<<<<<<<<<
void loop()
{
//==================
//Your normal loop stuff goes here
//==================
//Is it time to check for time sensitive Tasks
//Do this every 1000us (i.e. 1ms)
if (micros() - microsNow >= 1000UL)
{
microsNow = micros(); //Re-initialize for next iteration
TickTock(); //It is time to check
}
//==================
} // >>>>>>>>>>>>>> END OF loop() <<<<<<<<<<<<<<<<<
//==========================================================
// FUNCTIONS
//==========================================================
//Check to see if time sensitve Tasks should run
void TickTock()
{
//1ms has gone by, check if there is a Task to run
unsigned long millisNow = millis();
//==================
if (millisNow - TimeA >= TaskAtime) //Is it time to run Task A?
{
TimeA = millis(); //Re-initialize
TaskA();
}
//==================
if (millisNow - TimeB >= TaskBtime) //Is it time to run Task B?
{
TimeB = millis(); //Re-initialize
TaskB();
}
//==================
if (millisNow - TimeC >= TaskCtime) //Is it time to run Task C?
{
TimeC = millis(); //Re-initialize
TaskC();
}
//==================
if (millisNow - TimeD >= TaskDtime) //Is it time to run Task D?
{
TimeD = millis(); //Re-initialize
TaskD();
}
//Other time task checking goes here as needed
}
//==================
void TaskA()
{
digitalWrite(13,!digitalRead(13)); //Toggle pin 13
//Other stuff
}
//==================
void TaskB()
{
digitalWrite(12,!digitalRead(12)); //Toggle pin 12
//Other stuff
}
//==================
void TaskC()
{
digitalWrite(11,!digitalRead(11)); //Toggle pin 11
//Other stuff
}
//==================
void TaskD()
{
digitalWrite(10,!digitalRead(10)); //Toggle pin 10
//Other stuff
}
//======================================================================
// END OF CODE
//======================================================================
Look at the demonstration code for several things at the same time to understand how to do this.
1 Like
Hi,
I was wondering if it were possible to program the arduino to switch threads when a finger is placed on the fingerprint sensor or when the word lights is said to respond upon that or when plugged in to the neopixels to now change the thread to neopixels. ( i want to make a fingerprint sensor lock, use neopixels, and voice activated lights.)
Thanks,
Lasercow1
There are libraries for multitasking, but those libraries are not easy to use, and it has many consequences for the sketch.
You better make a sketch without delay, and decide what to do upon the incoming data.
Have a look at the blink without delay example: http://arduino.cc/en/Tutorial/BlinkWithoutDelay
In you sketch you collect all sensor data, and act upon that data.
Why have you started a second Thread on what is virtually the same topic as here. And the answers in the other Thread are relevant to your question here.
Double posting just wastes time and causes confusion.
I will ask the moderator to merge them.
...R