multithreading program in Arduino

Hi guys I have here a picture of our prototype. I am planning to use arduino as my microcontroller for our automation for our prototype. I’ll explain how the automation will work. I put pumps inside the storage tank (dirty water) and electrocoagulation tank . The pump(fr. storage tank) will start as soon as you pressed the start button then the pump starts to work. It has water level sensor so if it reaches high level it will stop the pump and the electrocoagulation will start to process. When the turbidity probe reaches the desired turbidity, electrocoagulation process will stop and the pump(in the EC) will start to process and gives off the water to activated carbon filter (it has also water level sensor which will stop pumping water when the high level reaches). Is it possible to make that programs in arduino? can you give us the code. or the algorithm.


In response to your Title have a look at the demo Several Things at a Time. I think that concept will be more than adequate for your project and it is simple to implement and debug.

In any case an Arduino cannot do true multi-threading.


Robin2 is correct. An Arduino cannot do true multithreading (for some definition of true multithreading). However, an Arduino can do several things at about the same time (for some definition of "about").

If you want true multithreading, you will need multiple Arduinos. However, there was nothing that I saw in the description that would require this level of hardware or true multithreading. I just saw a typical Arduino project.

If you want someone else to do it for you, you should be posting in the "Gigs and Collaborations" section and have your wallet ready.

Can you check if my code is correct. thanks.

int floatSwitchPin = 2; //Pin 2 int pump1Pin = 3;//Pin 3 int ecPin = 4; // Pin4 int pump2Pin = 5; //Pin 5 int floatSwitch2Pin = 6; //Pin 6 int analogPin = A0; //pin that the Turbidity probe is attached to

void setup() { pinMode(floatSwitchPin, INPUT_PULLUP); pinMode(pump1Pin, OUTPUT); pinMode(ecPin, OUTPUT); pinMode(pump2Pin, OUTPUT); pinMode(floatSwitch2Pin, INPUT_PULLUP); } Void loop() { If(digitalRead(floatSwitchPin)==HIGH)//switch reach high level { digitalWrite(pump1Pin,LOW);//turnoff the pump EcProcess(); } else { digitalWrite(pump1Pin,HIGH);//turn on pump } Void EcProcess() { int NTU_Value = analogRead(analogPin);//read the value of turbidity probe if(NTU_Value == 1) { digitalWrite(EcPin, LOW); //Stop electrocoagulation process( using power supply circuit) pump2Process(); } else { digitalWrite(EcPin, HIGH); //turn on electrocoagulation process( using power supply circuit) } Void pump2Process() { if (digitalRead(floatSwitch2Pin) == HIGH) { digitalWrite(pump2Pin, LOW); } else { digitalWrite(pump2Pin, HIGH); } }




AnalogRead in EcProcess looks wrong. It might be right, but analog read usually compares to ranges, not specific values. As 1 is in the code, maybe it is used as digital input?

It looks cool with the braces going down the left side, but that also makes it difficult to read your code. Please read the ‘how to use this forum’ post at the top of the list. It’s got lots of useful information that will help folks who want to help you.

EDITThis appears to be a continuation of your previous thread. Perhaps you should request that a moderator merge the two posts, so folks know more about your project when trying to read it.

is it possible to implement electrocoagulation process using arduino uno ?

What kind of signal does your turbidity sensor produce? Continuous analog (0 - 5V) or ON/OFF?

Its a microcontroller, so yes possible, but note:

1.5 Arduino LLC products are not authorized for use in safety-critical applications where a failure of the Arduino LLC product would reasonably be expected to cause severe personal injury or death. Safety-critical applications include, without limitation, life support devices and systems, equipment or systems for the operation of nuclear facilities and weapons systems. Arduino LLC products are neither designed nor intended for use in military or aerospace applications or environments, nor for automotive applications or the automotive environment. The Customer acknowledges and agrees that any such use of Arduino LLC products is solely at the Customer’s risk, and that the Customer is solely responsible for compliance with all legal and regulatory requirements in connection with such use.

So I'd say not advisable...

@bahayamaricar, stop cross-post. Threads merged.

You seem to think in processes, which run until completion before returning. This is not the way how Arduino sketches behave, where instead loop() is called over and over again. Several solutions are possible:

The EcProcess may look like this pseudo code:

Idle: wait for tank full;
stop Filling;
start Electrocoagulation;
Busy: wait for turbidity<limit;
stop Electrocoagulation;
start Emptying;
Done: wait for tank empty;
stop Emptying;
goto Idle;

Blocking solution: replace the if-statements in your code (wait for…) by while-loops, which loop until some condition becomes true. Then the controller cannot do anything else, i.e. no logging, no reaction on buttons or other asynchronous tasks, unless programmed explicitly into the various loops.

Non-blocking: Create one or more state machines, that do the various tasks. The pseudo code labels correspond to states of the EcProcess machine. On the last transition of a machine, back into Idle state, start the next machine.

Cooperative multiprocessing: Not really different from using state machines, but looking more like independent sequential tasks. Using task macros there exists e.g. taskWaitFor(someCondition), that waits for someCondition before advancing to the next state.

The latter solutions allow to run several state machines or tasks concurrently, i.e while one task is waiting for some event, other tasks can do something else.

A note for the other readers: Please forgive me my enthousiasm for the task macros. Some of my very first programs were written in Simula67, the grandfather of OOP. That’s why I was immediately excited by the idea of writing corresponding code also for Arduinos. The machines at that time consisted of a room full of hardware, comparable to what’s available nowadays in a single microcontroller chip. Unlike other multi-tasking approaches, Simula and the task macros do not rely on special hardware features, like concurrent processes, task switching, threads and semaphores, instead all that is implemented in ordinary C code. The task macros only encapsulate typical single-line code snippets, as used in every Arduino sketch.