2

I am new to Arduino and I am not sure about how to get two loops run parallel to each other. I have tested the individual loops and both of them work perfectly before I combined them together.

One loop will take input from the LCD touchscreen. One loop will take input from vibration sensor which will determine the state (on/off) of LED. Once the LED is turned on, it has to stay at the ON state for 3 seconds before switching to OFF state.

The problem I am facing is that when the LED is triggered to ON state by vibration sensor, I have to wait 3 seconds for the input from LCD to get processed. Is there any way I can combine these two loops in such a fashion that I run them simultaneously, i.e. run two inputs together?

  • Arduino Due, Zero support multi-threading. You can try them. – goddland_16 May 24 '17 at 4:20
  • Please search for delay on this site and you will find several questions similar to yours, and for some, answers that probably are relevant to you. What you want is replace delay(0) with something else: a timer or calls to millis() with comparison to some time reference. – jfpoilpret May 24 '17 at 5:29
  • Just answered something really similar here: arduino.stackexchange.com/questions/38713/… please read it for reference – gilhad May 24 '17 at 6:45
4

You can combine the 2 programs into 1 program. But when you start wanting to do multiple things at once then you can't have any code that "blocks" and waits to finish before letting the next thing happen. The Arduino has a single core, it can't do multithreading. But if you write non-blocking code you can make things appear to happen at the same time. I imagine the problem with your code now is that you have probably used the delay function. That function blocks, so nothing else can happen while the delay is running. Instead you have to use millis to record the time and periodically check against that time to see if it is time to take the next step instead of calling a delay to wait for the next step. See the "Blink Without Delay" example that comes with the IDE and any of the multitude of tutorials about it on the web to see how it works.

| improve this answer | |
  • 4
    "The Arduino has a single core, it can't do multi threading." This is simply 100% not true, and is a falsehood (if not a downright lie) that started to appear around the advent of dual core PC's. CPU's of any architecture or core count can do multi-threading. Multi threading != multiple cores .stackoverflow.com/a/16117084https://stackoverflow.com/a/… If you want multi-threading on an embedded system such as Arduino or any other microcontroller, you should probably start looking at RTOS's or OS's. – user9993 May 24 '17 at 7:59
  • So you're saying that the Arduino UNO or Mega is capable of executing multiple machine instructions at once? Sure, you can have task schedulers and all sorts of switching back and forth. But it is just not possible for the processor used in those boards to execute more than one instruction at any time. If you think there is a way then please explain it. I'm not talking about a pre-emptive multi-tasking where two processes take turns using the processor. But true multi-threading where the processor actually does two things at once. – Delta_G May 25 '17 at 3:02
  • @Delta_G in fact... I also used to agree with you, and went searching for the definition of multi-threading to prove that the temporal multi-threading is a sort of "emulation" but... I discovered that user9993 is right: Multithreading on wikipedia says that the ability to switch from one thread to another, even on the same CPU, is also considered multithreading. – frarugi87 May 25 '17 at 7:12
  • @frarugi87 Yep, I have no reason to lie. Quite frankly I think it shows an appalling lack of knowledge in the "arduino world" when I have to create an account to correct something that is so incorrect it would leave OP in a worse place. Multithreading has not once meant multicore. Threads executing in parallel on multiple cores is an implementation detail. On Windows open Task Manager (or equivalent on your OS eg top), note how you likely have several thousand threads. Does your CPU also have several thousand cores? – user9993 May 25 '17 at 7:41
  • 1
    Either way, it's a moot point. Please take a look at the poster's question. Do you think he needs any sort of complicated task scheduler or multi-threading system to solve it? Or do you think he could just write a little quick non-blocking millis based code and be done? – Delta_G May 26 '17 at 2:26
1

I totally second Delta's answer. Make the two loops "non blocking", then run them one after the other.

Just wanted to add another solution, to be applied only when strictly needed. And only if the program really needs it. And only if other solutions are not enough. And... Well, you understood.

You can use FreeRTOS to have a sort of "multi-threading" environment. You have, IIRC, four "threads" which can jump from one loop to the other. You will have to carefully design it (particularly the shared memory) but in the end you will have even blocking "threads" running in parallel. Here are some more infos.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.