Switch task with potentiometer, mulltitasking

hy

can someone help?
i have a problem ,stuck in project. Arduino uno .

i need a blinking led, maybye multitasking with potentiometer on analog input

example:
read A0 if 0 turn off
if
read A1 potentiometer, if read 1023 turn on
output pin 10,2,4,6
and flash led on pin 11 12

but when

A2 potentiometer read 1023 turn on
pin 10 ,2,3,7

and

A3 read 1023 turn on
pin 10,3,5,6

and
A4 read 1023 turn on pin 11,12

I coded each task separately, how to combine them,
put them in a row

CODING: (flash and output 10,2,4,6

void setup() {
pinMode(10,HIGH);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(A1, INPUT);
pinMode(02,HIGH);
pinMode(04,HIGH);
pinMode(06,HIGH);
}

void loop() {
if(analogRead(A1) > 1020) {
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,LOW);
delay(1000);
digitalWrite(11,LOW);
delay(1000);
digitalWrite(10,HIGH);
digitalWrite(02,HIGH);
digitalWrite(04,HIGH);
digitalWrite(06,HIGH);
delay(1000);
digitalWrite(10,LOW);
digitalWrite(02,LOW);
digitalWrite(04,LOW);
digitalWrite(06,LOW);
delay(1000);
}
else {
if(analogRead(A1) < 1020)
digitalWrite(12, LOW); //if it isn't higher than 512, make pin 4 low
}
}

CODING ( 10,3,5,6

void setup() {
pinMode(10,HIGH);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(A3, INPUT);
pinMode(05,HIGH);
pinMode(03,HIGH);
pinMode(06,HIGH);
}

void loop() {
if(analogRead(A3) > 1020) {
digitalWrite(12,HIGH);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,HIGH);
delay(100);
digitalWrite(11,HIGH);
delay(100);
digitalWrite(12,LOW);
delay(50);
digitalWrite(11,LOW);
delay(50);
digitalWrite(12,LOW);
delay(1000);
digitalWrite(11,LOW);
delay(1000);
digitalWrite(10,HIGH);
digitalWrite(05,HIGH);
digitalWrite(03,HIGH);
digitalWrite(06,HIGH);
delay(1000);

digitalWrite(10,LOW);
digitalWrite(05,LOW);
digitalWrite(03,LOW);
digitalWrite(06,LOW);
delay(1000);
}
else {
if(analogRead(A3) < 1020)
digitalWrite(12, LOW); //if it isn't higher than 512, make pin 4 low
}
}

thanks

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Is this a class exercise or assignment?

Perhaps something like this:

if (analogRead(A0) < 200) task1();
else if (...) task2();
...

why multitasking?

consider

const byte LedPin0 = 10;
const byte LedPin1 = 11;
const byte LedPin2 = 12;
const byte LedPin3 = 13;

const byte LedPins [] = {  LedPin0, LedPin1, LedPin2, LedPin3 };
enum { Off = HIGH, On = LOW };

unsigned long msecLst;
#define Period 250

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis();

    // ---------------------------
    if (500 > analogRead (A0))  {
        for (unsigned n = 0; n < sizeof(LedPins); n++)
            digitalWrite (LedPins [n], Off);
        return;
    }

    // ---------------------------
    if (1020 < analogRead (A1))  {
        if ( (msec - msecLst) > Period)  {
            msecLst = msec;

            digitalWrite (LedPins [0], ! digitalRead (LedPins [0]));
        }
        digitalWrite (LedPins [1], On);
    }
    else  {
        digitalWrite (LedPins [0], Off);
        digitalWrite (LedPins [1], Off);
    }

    // ---------------------------
    if (1020 < analogRead (A2))
        digitalWrite (LedPins [2], On);
    else
        digitalWrite (LedPins [2], Off);

    // ---------------------------
    if (1020 < analogRead (A3))
        digitalWrite (LedPins [3], On);
    else
        digitalWrite (LedPins [3], Off);
}

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

    for (unsigned n = 0; n < sizeof(LedPins); n++)  {
        digitalWrite (LedPins [n], Off);
        pinMode      (LedPins [n], OUTPUT);
    }
}

Hello and hy deno991
Take some time and describe the desired functionality of the sketch in simple words and this very simple and short form, including all dependencies between inputs and outputs.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

You can't use 'delay()' in your code if you want smooth multitasking. When you call 'delay()' no other task can run.

The easiest way to insert a delay without delay() is to break the process into steps or 'states'. A state is any part of your sketch that is waiting for an event to happen (button press, operation completed, delay finished) and when the event happens, you go on to the next state. Since there are a limited number of states each process can be in, the process is known as a Finite State Machine.

For example: Let's say you wanted two LEDs to blink at the same time but at different speeds. You can't even do that easily with a simple delay():

void loop()
{
  digitalWrite(LED1, HIGH);
  delay(3000);
  digitalWrite(LED1, LOW);
  delay(2000);

  digitalWrite(LED2, HIGH);
  delay(1000);
  digitalWrite(LED2, LOW);
  delay(2500);
}

This will obviously not do what you want because the 'delay()' calls allow only one LED to be active at a time.

The way to make it work is to make each independent process a separate Finite State Machine. I like to use an 'enum' to give names to each state. In this case the states are the same for both so we can get by with one 'enum' for both:

  enum
  {
    Idle, TurnOn, OnDelay, TurnOff, OffDelay
  } StateLED1 = TurnOn, StateLED2 = TurnOn;

Now you can see that each 'delay()' is spread across two states. The first state starts the delay timer and changes to the second state. The second state checks the timer and, if the timer has expired, goes on to the next state. The process repeats when the "OffDelay" state ends and goes to the "TurnOn" state.

The "Idle" state is there in case you want to turn off either or both blinkers.

const byte LED1 = 4;
const byte LED2 = 5;

enum
  {
    Idle, TurnOn, OnDelay, TurnOff, OffDelay
  } StateLED1 = TurnOn, StateLED2 = TurnOn;

void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  static unsigned long Delay1Start;
  static unsigned long Delay2Start;

  switch (StateLED1)
  {
    case Idle:
      digitalWrite(LED1, LOW);
      break;

    case TurnOn:
      digitalWrite(LED1, HIGH);
      Delay1Start = currentMillis;
      StateLED1 = OnDelay;
      break;

    case OnDelay:
      if (currentMillis - Delay1Start >= 3000)
        StateLED1 = TurnOff;
      break;

    case TurnOff:
      digitalWrite(LED1, LOW);
      Delay1Start = currentMillis;
      StateLED1 = OffDelay;
      break;

    case OffDelay:
      if (currentMillis - Delay1Start >= 2000)
        StateLED1 = TurnOn;
      break;
  }
  
  
  switch (StateLED2)
  {
    case Idle:
      digitalWrite(LED2, LOW);
      break;

    case TurnOn:
      digitalWrite(LED2, HIGH);
      Delay2Start = currentMillis;
      StateLED2 = OnDelay;
      break;

    case OnDelay:
      if (currentMillis - Delay2Start >= 1000)
        StateLED2 = TurnOff;
      break;

    case TurnOff:
      digitalWrite(LED2, LOW);
      Delay2Start = currentMillis;
      StateLED2 = OffDelay;
      break;

    case OffDelay:
      if (currentMillis - Delay2Start >= 2500)
        StateLED2 = TurnOn;
      break;
  }
}

And there we go. No delay(). No wait loops.

1 Like

Hello johnwasser
Brillant example to code blink without delay for several LEDS in standard C.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

Very good demo-code to demonstrate how state-machines work

here is a code-version without state-machine.
This code has the same timing with different On/Off-times for the two LEDs

const byte LED1 = 4;
const byte LED2 = 5;

unsigned long LED1_Timer;
unsigned long LED1_OnTime  = 3000;
unsigned long LED1_OFFTime = 2000;
unsigned long LED1_WaitTime;

unsigned long LED2_Timer;
unsigned long LED2_OnTime  = 1000;
unsigned long LED2_OFFTime = 2500;
unsigned long LED2_WaitTime;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

void setup(){
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  LED1_WaitTime = LED1_OnTime;
  LED2_WaitTime = LED2_OnTime;
}

void loop() {

  if ( TimePeriodIsOver(LED1_Timer,LED1_WaitTime) ) { // check if WaitTime is over
    // if the amount of milliseconds stored in variable LED1_WaitTime
    // have REALLY passed by
    if ( digitalRead(LED1) == HIGH) { // if LED1 is switched on
      digitalWrite(LED1,LOW); 
      LED1_WaitTime = LED1_OFFTime;
    }
    else { // LED1 is switched off
      digitalWrite(LED1,HIGH);
      LED1_WaitTime = LED1_OnTime;      
    }    
  }

  if ( TimePeriodIsOver(LED2_Timer,LED2_WaitTime) ) { // check if WaitTime is over
    // if the amount of milliseconds stored in variable LED2_WaitTime
    // have REALLY passed by
    if ( digitalRead(LED2) == HIGH) {
      digitalWrite(LED2,LOW);
      LED1_WaitTime = LED2_OFFTime;
    }
    else { // LED2 is switched off
      digitalWrite(LED2,HIGH);
      LED2_WaitTime = LED2_OnTime;      
    }    
  }

}

best regards Stefan

hy
i work on one project for me.
i need a five Analog input

A0 = everiting off
A1 ( if analogread> 1020 ) = turn on 10,2,4,6 pins and 11,12 pins independet flashing leds
A2 ( if analog analogread> 1020)= turn on 10,2,3,7 pins and 11,12 pins led s independet flash
A3 ( if > 1020 analogread)= turn on 10,3,5,6 pins , and independet flash 11,12 pin
A4 ( if analogread > 1020)= turn on 11, 12 pin flashing leds

You should find out which unique task to activate. Setting the same output pins to different values for each task in sequence causes flicker only, nothing meaningful.

yes, only i need a blinking LED s on different pin, and diferent time ( for pin 11, 12)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.