Multitask analog inputs millis function

Hi, thanks for recieving in community. I am beginner in arduino world and sory if i have stupid questions. Working on project with attached code and confused hot to set paralel monitoring analog inputs with milis code. I search and search for days but, still without solutions.

const int buzzer = 5;

void setup() {

pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A1));
delay(1);
if(analogRead(A1) < 400)
{
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
delay(2000);
 tone(buzzer, 2000); 
}
else
{
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
delay(1);
noTone(buzzer);     
}
if(analogRead(A2) < 400)
{
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
delay(2000);
 tone(buzzer, 2000); 
}
else
{
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
delay(1);
noTone(buzzer);   
}
if(analogRead(A3) < 400)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(2000);
 tone(buzzer, 2000); 
}
else
{
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
delay(1);
noTone(buzzer);    
}
if(analogRead(A4) < 400)
{
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
delay(2000);
 tone(buzzer, 2000);
}
else
{
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
delay(1);
noTone(buzzer);   
}
}

hot to set paralel monitoring analog inputs with milis code

I do not know what you are wanting. Please explain what that means.

Please put code in code tags ([code] [/code]) not quote tags.

some millis() timing tutorials.
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Sorry friend, i am completly noob. Idea is to simultaneously check analog inputs and activate outputs. With this code, other three inputs is blocked during delay time. Ok, i will try again with tutorials. Thanks.

You cannot sample the analog inputs simultaneously. AnalogRead() takes about 110us. So you can read the 4 inputs one after another, but that will take 440us at a minimum.

Not literally, sorry.

Project is monitornig seeding machine.

In 4 lane i have 4 ir transmiter-reciever pair. If seeds go trough barier, activate green led... if 2 seconds no seeds passing, activate red led and buzzer. Thats all. Maybe to use digital inputs, i dont now, like i am said, completly noob. Chose analog to set specific value, look more precise.

milos88:
Hi, thanks for recieving in community. I am beginner in arduino world and sory if i have stupid questions. Working on project with attached code and confused hot to set paralel monitoring analog inputs with milis code. I search and search for days but, still without solutions.

Do not use pinMode() on pins you are using for analogRead().

It looks like what you wrote is:
When any value is low (<400), immediately set the outputs one way, wait two seconds, and turn on the buzzer.
When the value is high (>= 400), immediately set the outputs the other way, wait 1/1000th of a second, and turn off the buzzer.

What I think you want is:
If any inputs are low (<400), immediately set the outputs one way.
If any input is high, immediately set the outputs the other way.
If any of the inputs have been low for two seconds or more, turn on the buzzer.
If all inputs are high, turn off the buzzer.

That would look something like this:

const int buzzer = 5;


unsigned long LastHighTime1 = 0;
unsigned long LastHighTime2 = 0;
unsigned long LastHighTime3 = 0;
unsigned long LastHighTime4 = 0;


void setup()
{
  //pinMode(A1, INPUT);
  //pinMode(A2, INPUT);
  //pinMode(A3, INPUT);
  //pinMode(A4, INPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}


void loop()
{
  int highCount = 0;


  if (analogRead(A1) < 400)
  {
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    if (millis() - LastHighTime1 >= 2000)
      tone(buzzer, 2000);
  }
  else
  {
    LastHighTime1 = millis();
    highCount++;
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
  }


  if (analogRead(A2) < 400)
  {
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    if (millis() - LastHighTime2 >= 2000)
      tone(buzzer, 2000);
  }
  else
  {
    LastHighTime2 = millis();
    highCount++;
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
  }


  if (analogRead(A3) < 400)
  {
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    if (millis() - LastHighTime3 >= 2000)
      tone(buzzer, 2000);
  }
  else
  {
    LastHighTime3 = millis();
    highCount++;
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
  }


  if (analogRead(A4) < 400)
  {
    digitalWrite(12, HIGH);
    digitalWrite(13, LOW);
    if (millis() - LastHighTime4 >= 2000)
      tone(buzzer, 2000);
  }
  else
  {
    LastHighTime4 = millis();
    highCount++;
    digitalWrite(12, LOW);
    digitalWrite(13, HIGH);
  }


  if (highCount == 4)
  {
    // All inputs are high
    noTone(buzzer);
  }
}

Sorry guys, i am probably anoying, thanks for help.

This is close to solution.

i want:

if a1 < 400 set immediately input 6low and 7high and stay 2 seconds with that state
if a2 < 400 set immediately input 8low and 9high and stay 2 seconds with that state
if a3 < 400 set immediately input 10low and 11high and stay 2 seconds with that state
if a4 < 400 set immediately input 12low and 13high and stay 2 seconds with that state
if a1 > 400 longer than 2 second - set input 6high and 7low
if a2 > 400 longer than 2 second - set input 8high and 9low
if a3 > 400 longer than 2 secondset - set input 10high and 11low
if a4 > 400 longer than 2 secondset - set input 12high and 13low

activate buzzer in any of first four cases.

thanks again for yours time, sorry on my bad english.

only problem that i have with first code is... during 2 seconds when one input is bellow 400, program not checking other inputs.

milos88:
only problem that i have with first code is... during 2 seconds when one input is bellow 400, program not checking other inputs.

Because of this: delay(2000);. While waiting for a delay() to end the processor does nothing else. Read up on how to do several things at the same time and be prepared to make changes to your sketch.

Edit your first post to use code tags.

milos88:
i want:
if a1 < 400 set immediately input 6low and 7high and stay 2 seconds with that state
if a2 < 400 set immediately input 8low and 9high and stay 2 seconds with that state
if a3 < 400 set immediately input 10low and 11high and stay 2 seconds with that state
if a4 < 400 set immediately input 12low and 13high and stay 2 seconds with that state
if a1 > 400 longer than 2 second - set input 6high and 7low
if a2 > 400 longer than 2 second - set input 8high and 9low
if a3 > 400 longer than 2 secondset - set input 10high and 11low
if a4 > 400 longer than 2 secondset - set input 12high and 13low

activate buzzer in any of first four cases.

thanks again for yours time, sorry on my bad english.

I think this will do what you want:

const byte BuzzerPin = 5;
unsigned long BuzzerTimer = 0;


const byte ChannelCount = 4;
const byte InputPins[ChannelCount] = {A1, A2, A3, A4};
const byte GreenPins[ChannelCount] = {6, 8, 10, 12};
const byte RedPins[ChannelCount] = {7, 9, 11, 13};


unsigned long ChannelTimers[ChannelCount];  // Default to 0


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


  for (int channel = 0; channel < ChannelCount; channel++)
  {
    pinMode(GreenPins[channel], OUTPUT);
    digitalWrite(GreenPins[channel], HIGH);
    pinMode(RedPins[channel], OUTPUT);
    digitalWrite(RedPins[channel], LOW);
  }


  pinMode(BuzzerPin, OUTPUT);
}


void loop()
{
  unsigned long currentMillis = millis();


  if (BuzzerTimer != 0 && currentMillis - BuzzerTimer >= 2000)
  {
    noTone(BuzzerPin);
  }


  for (int channel = 0; channel < ChannelCount; channel++)
  {
    boolean inputLow = analogRead(InputPins[channel]) < 400;


    if (inputLow)
    {
      // Immediately turn off the Green light and turn on the Red light
      digitalWrite(GreenPins[channel], LOW);
      digitalWrite(RedPins[channel], HIGH);
      ChannelTimers[channel] = currentMillis;  // Time the pin was last low
      BuzzerTimer = currentMillis; // Start the buzzer for 2 seconds
      tone(BuzzerPin, 2000);
    }
    else
    {
      // Input has gone high.  If it was last low more than two seconds ago, switch the lights
      if (ChannelTimers[channel] != 0 && currentMillis - ChannelTimers[channel] >= 2000)
      {
        digitalWrite(GreenPins[channel], HIGH);
        digitalWrite(RedPins[channel], LOW);
        ChannelTimers[channel] = 0;
      }
    }
  }
}

Yes, yes, bravo! You save my life! This is great!

ffff, look soo complicated... i hope i will learn arduino to this level one day.

Tanks one more time!

My tutorial on Multi-tasking in Arduino and How to write Timers and Delays in Arduino are applicable to your case
The times tutorial has small millisDelay class that simplifies running multiple timers/delays

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