Multitasking the arduino

Hi this is KAMAL

Recently i had done some work in home automation and for that i took (pir sensor,fire sensor, mercury switch, button) and i created the code also. Individually they works perfect but when i try to merge ( i resolve the pin issues ) that 4 operation cannot done simultaneously after one completed then the other starts. And am in the neck moment for my project so requesting a immediate solution.

Have a read here:

How to do multiple things at the same time

Yeah now i have the idea, i'll check it out and let u know and thank you verymuch for your help. As this is my first post and you are my first guide.

Let us know if you run into any hangups.

i attached my code and i cant make all sensors work at same time but individualy they works good
can anyone help me to resolve

// fire sensor

int sensorPin = A0; // select the input pin for the fire
int sensorValue = 10; // variable to store the value coming from the sensor
int led1 = 9; // Output pin for LED

// tilt sensor

const byte Button1 = A1;
const byte led2= 10;
byte ButtonState;
byte lastState = LOW;
byte count = 0;

// door sensor

const int Button2 = 2;
const int led3 = 13; // the number of the LED pin
int buttonState = 0;

// car sensor

const byte Button3 = A0;
const byte led4 = 11;
const byte led5 = 12;
byte ButtonState1;
byte lastState1 = LOW;
byte count1 = 0;

void setup() {

Serial.begin(9600);

// fire sensor
pinMode(led1, OUTPUT);

// tilt sensor
pinMode(led2, OUTPUT);
pinMode(Button1, INPUT);

// door sensor
pinMode(led3, OUTPUT);
pinMode(Button2, INPUT);

// car sensor
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(Button3, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

// fire sensor
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if (sensorValue < 100){
Serial.println("Fire Detected");
Serial.println("LED1 on");
digitalWrite(led1,HIGH);
digitalWrite(led1, HIGH);
delay (1000);
digitalWrite(led1, LOW);
delay (1000);
digitalWrite(led1, HIGH);
delay (1000);
digitalWrite(led1, LOW);
delay (1000);
digitalWrite(led1, HIGH);
delay (1000);
digitalWrite(led1, LOW);
delay (1000);
digitalWrite(led1, HIGH);
delay (1000);
digitalWrite(led1, LOW); }}

// tilt sensor
{
ButtonState = digitalRead(Button1);

if(ButtonState && ButtonState != lastState){ // button latch, no debounce needed.

if(count < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
count += 255; // same as count = count + 5;
digitalWrite(led2, HIGH);
delay (1000);
digitalWrite(led2, LOW);
delay (1000);
digitalWrite(led2, HIGH);
delay (1000);
digitalWrite(led2, LOW);
delay (1000);
digitalWrite(led2, HIGH);
delay (1000);
digitalWrite(led2, LOW);
delay (1000);
digitalWrite(led2, HIGH);
delay (1000);
digitalWrite(led2, LOW);}
lastState = ButtonState;}

// door sensor
{
buttonState = digitalRead(Button2);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(led3, HIGH);
delay (1000);
digitalWrite(led3, LOW);
delay (1000);
digitalWrite(led3, HIGH);
delay (1000);
digitalWrite(led3, LOW);
delay (1000);
digitalWrite(led3, HIGH);
delay (1000);
digitalWrite(led3, LOW);
delay (1000);
digitalWrite(led3, HIGH);
delay (1000);
digitalWrite(led3, LOW);
delay (1000); } }

// car sensor

{
ButtonState1 = digitalRead(Button3);

if(ButtonState1 && ButtonState1 != lastState1) // button latch, no debounce needed.
{
if(count1 < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
count1 += 255; // same as count = count + 5;
else
count1 = 0;
Serial.print(".");
analogWrite(led4, count1);
delay(500);
analogWrite(led5, count1);
delay(500);
analogWrite(led5, LOW);
Serial.println(count1);}
lastState1 = ButtonState1;}

}

total.ino (3.51 KB)

Aren't all those delays going to make the whole thing unresponsive?

Please use code tags when posting code

I see many delay()s in that code so I looked no further for the problem. Did you read the advice in the thread linked to in post #1 ?

Any time you have numbers in your variable names you are doing something wrong. Instead of Button3 why not call it CarSensor? Then you don't need to put a comment next to it every time you use it.

The Arduino is like a man, it can only do one thing at a time. But it can do multiple thing quickly after each other and get them all done. But if one of those things is "do absolutely nothing for an hour", the delay(), then yeah, things are not getting done quick...

So lesson learned, this is the last time you used delay() :wink:

Sorry for the worst format i had sent thisnia ma first post thatswhy and caould i need to change anything in the code? But i saw in youtube that arduino do multiple operations simultaneously.

But i saw in youtube that arduino do multiple operations simultaneously.

What you probably saw was the illusion of the Arduino doing many things simultaneously, because it even though it is lowly microcontroller, it operates at timescales humans find hard to comprehend.

Is there any other way to resolve?

Another way, no. A way, yes. Remake the code without delay(). Before you try and do that have a look at Blink Without Delay.

septillion:
The Arduino is like a man, it can only do one thing at a time.

I heard recently about a man multi-tasking. He was drinking a cup of coffee while waiting for the postman.

...R

BASITH_KAMAL:
Is there any other way to resolve?

Did you read the link that I posted for you? It very clearly states not to use delay. It makes a really big deal of it. Then it explains how to write the same code without delay. Maybe you go actually read that link and find the answer to your problem.

Maybe you go actually read that link and find the answer to your problem.

But that requires multi-tasking - reading, analyzing, and thinking all at the same time. 8)

You could give Arduino-MOS a try. See: GitHub - joe7575/Arduino-MOS: A ultra lightweight cooperative multitasking schema for Arduino devices
It should be easy to spend one task for each sensor according to the LedTask() in the example "small.ino"

/Joe