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.
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.
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.
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.
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.
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;
}
}
}
}