Arduino Multitasking

Ok I know arduino is not great at multitasking however Im not asking for a lot in my opinion

What I am trying to do is:

Two inputs: A and B

A is a safety input so micro controller is enabled when A is HIGH
B is just a push switch

If A is on then carry on to scan for B
If B is on do a task that will take around 5 seconds in human time

However B will be just a simple push switch, so press and release after lets say half a second but still carry out all the task that will take around 5 seconds

Also here is the multitasking bit while the task B is being carried out arduino needs to scan for input A and if input A changes to LOW at any time arduino needs to shut down task be and lets say disable all outputs

I havent submitted a code because I am not asking you guys to write it for me I am looking for you to help me with a logic.

So far I have created a state machine using millis() but it doesnt hang on to the output its look for the input B to be pressed all the time.

#include <Bounce2.h>

const byte safetySwitchPin = 3;
const byte startButtonPin = 4;

Bounce startButton;

bool executeFlag = false;

void setup(){
  pinMode(safetySwitchPin, INPUT_PULLUP);
  startButton.attach(startButtonPin, INPUT_PULLUP);
}

void loop(){
  if(!digitalRead(safetySwitchPin)){
    startButton.update();
    if(startButton.fell()){
      executeFlag = true;
    }
  }
  else{
    executeFlag = false;
  }
  
  if(executeFlag){
    taskToRun();
  }
}

I use Bounce2 for the button debouncng because why make code that’s already there?

taskToRun() MUST be non blocking so it keeps on polling the switch (so no delay() ). And when the task is done you can set executeFlag to false again and everything can start over.

So far I have created a state machine using millis()

But, I'm not going to share it.

but it doesnt hang on to the output its look for the input B to be pressed all the time.

So fix it so it does. Whatever it and it are.

Have a look at the demo Several Things at a Time

...R

thanks for replays guys! I have finally manged to implement the code that I needed!

Originally my problem was with the hardware…

I used this website to do emulation online instead of wiring and uploading code again and again

this is the code:

int drl = 11;
int drlstate;
int ignition =10;
int ignitionstate;
int super = 9;
int superstate;
int washer = 8;
int washerstate;
int intensive = 7;
int intensivestate;
unsigned long currentMillis;
unsigned long superMillis;

void setup() {

pinMode(drl, OUTPUT);
pinMode(ignition, INPUT);
pinMode(super, INPUT);
pinMode(washer, OUTPUT);
pinMode(intensive, OUTPUT);
}

void loop() {

ignitionstate = digitalRead(ignition);
if(ignitionstate == HIGH)
{
drlstate = HIGH;
digitalWrite(drl, drlstate);
superstate = digitalRead(super);
if(ignitionstate == HIGH && superstate == LOW)
{
active();
}
}
else
{
drlstate = LOW;
digitalWrite(drl, drlstate);
washerstate = LOW;
digitalWrite(washer, washerstate);
intensivestate = LOW;
digitalWrite(intensive, intensivestate);
}
}

void active()
{
currentMillis = millis();
superMillis = millis();
while(currentMillis <= (3000+superMillis)&&(ignitionstate == HIGH))
{

ignitionstate = digitalRead(ignition);
currentMillis = millis();
washerstate = HIGH;
digitalWrite(washer, washerstate);
}
while(currentMillis <= (5000+superMillis)&&(ignitionstate == HIGH))
{
ignitionstate = digitalRead(ignition);
currentMillis = millis();
washerstate = LOW;
digitalWrite(washer, washerstate);
intensivestate = HIGH;
digitalWrite(intensive, intensivestate);

}
while(currentMillis >= (5000+superMillis)&&(ignitionstate == HIGH))
{
ignitionstate = digitalRead(ignition);
intensivestate = LOW;
digitalWrite(intensive, intensivestate);
break;
}
}

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.