Hello guys...i have a little problem. I have done some tings with arduino before so i am not totaly new (servos, lights, sensors...) but now i am using multiple solenoids for the first time... The thing is i have two solenoids that runindependently from each other (one has to be open for 1000ms and other for 500ms) i have tried millis() with LEDs and it is working (now i am waiting postal delivery-solenoids)...I have saperated power-24V for solenoids. Signals will go trough two relays....Now i am wondering if they will be waiting for this specific time (as LEDs) or they will be blinking?? thanks for answers
Code please
OK times are a little diferent but the idea is the same i think...
Stroj-p.ino (2.29 KB)
//Global Variables
const byte Senzor = A0; // pin za senzor
const byte Bat1 = 11; // pin od bata1
const byte Bat2 = 12;//pin od bata2
unsigned long SprozenSenzor_Millis;
unsigned long Bat2_millis;
unsigned long buttonInterval= 300;
unsigned long Bat1_OnAt;
unsigned long Bat2_OnAt;
unsigned long turnOn_Bat1_Delay = 500;
unsigned long turnOff_Bat1_Delay = 800 ;
unsigned long turnON_Bat2_Delay = 850;
unsigned long turnOff_Bat2_Delay = 700;
bool Bat1_Ready = false;
bool Bat2_Ready = false;
bool Bat1_State = false;
bool Bat2_State = false;
bool Senzor_State = false;
void setup() {
pinMode(Bat1, OUTPUT);
pinMode(Bat2, OUTPUT);
digitalWrite(Bat1, LOW);
digitalWrite(Bat2, LOW);
}
void loop() {
unsigned long currentMillis = millis();
// --------BAT 1----------------------------------------
if (analogRead(Senzor)== LOW) {
SprozenSenzor_Millis = currentMillis;
Bat1_Ready = true;
}
{
if (Bat1_Ready) {
if ((unsigned long)(currentMillis - SprozenSenzor_Millis) >= turnOn_Bat1_Delay) {
digitalWrite(Bat1, HIGH);
Bat1_State = true;
Bat1_OnAt = currentMillis;
Bat1_Ready = false;
}
}
if (Bat1_State) {
if ((unsigned long)(currentMillis - Bat1_OnAt) >= turnOff_Bat1_Delay) {
Bat1_State = false;
digitalWrite(Bat1, LOW);
}
}
}
//-------------------BAT 2------------------------
//Bat 2
{
if (analogRead(Senzor)==LOW) {
SprozenSenzor_Millis = currentMillis;
Bat1_Ready = true;
}
{
if (Bat1_Ready) {
if ((unsigned long)(currentMillis - SprozenSenzor_Millis) >= turnON_Bat2_Delay) {
digitalWrite(Bat2, HIGH);
Bat2_State = true;
Bat2_OnAt = currentMillis;
Bat2_Ready = false;
}
}
if (Bat2_State) {
if ((unsigned long)(currentMillis - Bat2_OnAt) >= turnOff_Bat2_Delay) {
Bat2_State = false;//
digitalWrite(Bat2, LOW);
}
}
}
}
//debounce
{
unsigned long previousButtonMillis= millis();
if (millis() - previousButtonMillis >= buttonInterval) {
if (analogRead(Senzor) == LOW) {
Senzor_State = ! Senzor_State;
previousButtonMillis += buttonInterval;
}
}
}
}
Now i am wondering if they will be waiting for this specific time (as LEDs) or they will be blinking??
If what you will be doing is to substitute solenoids for LEDs then they will behave in the same way. How are you powering the solenoids ?
if (analogRead(Senzor) == LOW)
This is an unusual way to test whether a pin is LOW. Why not use digitalRead() ?
How is the input pin wired ?
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.
...R
Thanks for help, i appreciate.For power supply i will use 230/24VDC supplier from Schrack (somehow i have it at home) ..yes i taught it will be better if i use digitalRead instead of analogRead (i picked this on a internet and i was not sure) I will use your advice. For sensor i am using IR tracker from arduino...and i really should go trough this tutorial again.
For power supply i will use 230/24VDC
How will the Arduino be turning the solenoids on and off ?
oh sorry i did not understand your question right. I will use two 5V relay module from Arduino.