Multitasking Using Multiple Sensor Outputs

type or paste code here

#define M_1 2 // Motor 1
#define M_2 3 // Motor 2
#define M_3 4 // Motor 3

#define Sensor_1 8 // Sensor 1
#define Sensor_2 9 // Sensor 2
#define Sensor_3 10 // Sensor 3

int Port_1 = A0; // Port 1
int Port_2 = A1; // Port 2
int Port_3 = A2; // Port 3

int portValue_1 = 0;
int outputValue_1 = 0;

int portValue_2 = 0;
int outputValue_2 = 0;

int portValue_3 = 0;
int outputValue_3 = 0;

void setup() {
Serial.begin(9600);

pinMode(M_1, OUTPUT);
pinMode(M_2, OUTPUT);
pinMode(M_3, OUTPUT);

pinMode(Sensor_1, INPUT);
pinMode(Sensor_2, INPUT);
pinMode(Sensor_3, INPUT);

}

void loop() {

int SSensor_1 = digitalRead(Sensor_1);
int SSensor_2 = digitalRead(Sensor_2);
int SSensor_3 = digitalRead(Sensor_3);

portValue_1 = analogRead(Port_1);
portValue_2 = analogRead(Port_2);
portValue_3 = analogRead(Port_3);

outputValue_1 = map(portValue_1, 0, 1023, 0, 3000);
outputValue_2 = map(portValue_2, 0, 1023, 0, 3000);
outputValue_3 = map(portValue_1, 0, 1023, 0, 3000);

Serial.print("sensor1 = " );
Serial.print(SSensor_1);
Serial.print("\t output_1 = ");
Serial.println(outputValue_1);

Serial.print("sensor2 = " );
Serial.print(SSensor_2);
Serial.print("\t output_2 = ");
Serial.println(outputValue_2);

Serial.print("sensor3 = " );
Serial.print(SSensor_3);
Serial.print("\t output_ = ");
Serial.println(outputValue_3);

if ( SSensor_1 == 0)
{
digitalWrite(M_1, HIGH);
delay( outputValue_1);
digitalWrite(M_1, LOW);
delay(500);
}

else if ( SSensor_2 == 0)
{
digitalWrite(M_2, HIGH);
delay( outputValue_2);
digitalWrite(M_2, LOW);
delay(500);
}

else if ( SSensor_3 == 0)
{
digitalWrite(M_3, HIGH);
delay( outputValue_3);
digitalWrite(M_3, LOW);
delay(500);
}

else{
digitalWrite(M_1, LOW);
digitalWrite(M_2, LOW);
digitalWrite(M_3, LOW);

  }

}

Can you Do changes in code that At time multiple sensors can be work I mean if 2 sensor are active at a time both condition have to work at time

No, I can't. But you can by organizing your logic to quickly detect all sensors and then process each one very quickly. It will seem like multitasking. IF you use a microcontroller that actually allows multitasking, then perhaps you can do multitasking.

If this is a question what is the meaning in detail.
The sketch itself has potential to be tuned using C++.
Have a nice day and enjoy coding in C++.

Make per-sensor functions that handle the sensors and motors and remember what to do as needed.

void process2(void){
  // check and handle the #2 subsystem.  Call frequently by loop();
  static unsigned long lastTrigger;
  static int state = 0;
  SSensor_2 = digitalRead(Sensor_2);
  if ( SSensor_2 == 0 && state ==0) 
  {
    state = 1; // triggered--start the motor and remember the time
    lastTrigger = millis();
    portValue_2 = analogRead(Port_2);
    outputValue_2 = map(portValue_2, 0, 1023, 0, 3000);
    digitalWrite(M_2, HIGH);
  }
  if (state == 1 && (millis() - lastTrigger) > outputValue_2) 
  { //time's up -- reset
     state = 0;
     digitalWrite(M_2, LOW);
   }
}

//  and

void loop(){
  //...
  process2();  
  //...
} 

Thank You So Much Code is working

Hello
Bt I want that if sensor is active although motor will be of after port delay
can you please do for that

??

Do you want the motor to keep running outputValue2 milliseconds after Sensor2 stops being 0? Then it might just need to reset the timer while it's pressed:

if ( SSensor_2 == 0) {
  lastTrigger = millis();
}

Of if you want the motor to stay off for a certain time after it runs, you might transition to a pause state:

:

(

Actually My Application is like that when sensor value is 0 means when it detect only one time motor will be on till Defined port delay

Inshort Even if the sensor is detected, the motor should be turned on only once and stop after defined delay which is defined by portValue_2

Waiting for your reply

I want that if any of sensor is activate than motor will have to on and if all sensor are active than all motor will have to on at same condition defined in code w

Hello prajapatiayu148gmailcom
Post a state transition diagram to explain your requirements.
Have a nice day and enjoy coding in C++.

The intention is still unclear:

If the motors interact so that only one must be on at any one time, you might need to track how many motors are running and make that part of the conditions required to change states. Like if ( SSensor_2 == 0 && state ==0 && num_running_motors == 0 ){state=1;num_running_motors++;...}

My application is that Like I have coffee machine In this machine there are multiple output of coffee and coffe will have to come when sensor detect bt I need coffee in limited quantity so that have to off motor off at delay
Understood ?

Hello prajapatiayu148gmailcom
The motor has to be run if the sensor==button has been pressed until either the button has been released or the timer has fired. The time duration is adjustable via the analog input.
Is this assumption of functionality correct ?
Have a nice day and enjoy coding in C++.

Even if the sensor == button is pressed, the motor will continue for the time specified by the potentiometer. and After This time motor will have to off even if sensor is active or pressed.

Hello prajapatiayu148gmailcom
I´m talking about one [1] set of button, motor and timer to understand the desired function.
This functionality can be multiple used by C++.
Have a nice day and enjoy coding in C++.

Even if the sensor == button is pressed, the motor will continue for the time specified by the potentiometer. and After This time motor will have to off even if sensor is active or pressed.

So each motor has three states: off, waiting for a button press; on, waiting for the potentiometer controlled timer to run out; and off, but ignoring any future button presses?

  void process2(void){
  // check and handle the #2 subsystem.  Call frequently by loop();
  static unsigned long lastTrigger;
  static int state = 0;
  SSensor_2 = digitalRead(Sensor_2);
  if ( SSensor_2 == 0 && state ==0) 
  {
    state = 1; // triggered--start the motor and remember the time
    lastTrigger = millis();
    portValue_2 = analogRead(Port_2);
    outputValue_2 = map(portValue_2, 0, 1023, 0, 3000);
    digitalWrite(M_2, HIGH);
  }
  if (state == 1 && (millis() - lastTrigger) > outputValue_2) 
  { //time's up 
     state = 2; 
     digitalWrite(M_2, LOW);
   }
   if( state == 2 ) { // absorbing state    
      ; // do nothing. 
  }
}

Hello prajapatiayu148gmailcom
Check this example C++sketch for on set of motors, sensors and ports. This sketch had been tested in my test environment and you have to change the pin addresses to your hardware setup. To add more sets as a.m. you have to add the pin addresses to the array and extend the structured array per set. That´s all.

/* BLOCK COMMENT
  ATTENTION: This Sketch contains elements of C++.
  https://www.learncpp.com/cpp-tutorial/
  https://forum.arduino.cc/t/multitasking-using-multiple-sensor-outputs/952169
*/
#define ProjectName "Multitasking Using Multiple Sensor Outputs"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte Motors[] {9};      // // portPin o---|220|---|LED|---GND
constexpr byte Sensors[] {A0};      // portPin o---|button|---GND
constexpr byte Ports[] {A8};      // Vcc o---|portPin|---GND
#define OutPutTest
constexpr  unsigned long OutPutTestTime {1000};
// CONSTANT DEFINITION
enum {One};
enum  {Off, On};
enum {hasBeenReleased, hasBeenPressed};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct TIMER {              // has the following members
  unsigned long duration;   // memory for interval time
  unsigned long stamp;      // memory for actual time
  bool onOff;            // control for start/stop
};
struct BUTTON {
  byte pin;
  bool statusQuo;
  TIMER scan;
};
struct ANALOG {
  byte pin;
  unsigned long value;
  long  minMap;
  long maxMap;
};
struct MOTORCNTRL {
  BUTTON sensor;
  byte  motor;
  TIMER wait;
  ANALOG port;
};
MOTORCNTRL motorCntrls[] {
  { Sensors[One], false, 20, 0, true,
    Motors[One],
    3000, 0, false,
    Ports[One], 0, 0, 3000
  },
};
// -------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File   : ")), Serial.println(__FILE__);
  Serial.print(F("Date   : ")), Serial.println(__DATE__);
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);  // used as heartbeat indicator
  //  https://www.learncpp.com/cpp-tutorial/for-each-loops/
  for (auto Sensor : Sensors) pinMode(Sensor, INPUT_PULLUP);
  for (auto Motor : Motors) pinMode(Motor, OUTPUT);
#ifdef OutPutTest
  // check outputs
  for (auto Motor : Motors) digitalWrite(Motor, HIGH), delay(OutPutTestTime);
  for (auto Motor : Motors) digitalWrite(Motor, LOW), delay(OutPutTestTime);
#endif
}
void loop () {
  currentTime = millis();
  digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
  for (auto &motorCntrl : motorCntrls) {
    if (currentTime - motorCntrl.sensor.scan.stamp >= motorCntrl.sensor.scan.duration) {
      motorCntrl.sensor.scan.stamp = currentTime;
      int stateNew = !digitalRead(motorCntrl.sensor.pin);
      if (motorCntrl.sensor.statusQuo != stateNew) {
        motorCntrl.sensor.statusQuo = stateNew;
        Serial.print(F("Sensor ")), Serial.println(stateNew ? "pressed" : "released");
        switch (stateNew) {
          case hasBeenPressed:
            digitalWrite( motorCntrl.motor, HIGH);
            motorCntrl.wait.stamp = currentTime;
            motorCntrl.wait.onOff = On;
            break;
          case hasBeenReleased:
            digitalWrite( motorCntrl.motor, LOW);
            motorCntrl.wait.onOff = Off;
            break;
        }
      }
    }
    if (currentTime - motorCntrl.wait.stamp >= (unsigned long) map(analogRead(motorCntrl.port.pin),0,1023,motorCntrl.port.minMap,motorCntrl.port.maxMap) && motorCntrl.wait.onOff) {
      digitalWrite( motorCntrl.motor, LOW);
      motorCntrl.wait.onOff = Off;
    }
  }
}

Have a nice day and enjoy coding in C++.