tkuryga
November 23, 2022, 7:37pm
1
Hi all. I'm new to Arduino and coding. Looking for some assistance. Trying to write a code to make a box lid open and close quickly with pneumatics. Randomly activate solenoid (to control cylinder) for 30 duration of seconds, then pause for 8 minutes. Repeat sequence. Will this work. Thanks.
int sol = 6;
int LONG = 480000;
int dewell = 30000;
void setup()
{
Serial.begin(9600);
pinMode(sol, OUTPUT);
}
void loop() {
{
digitalWrite(sol, HIGH);
delay(random(dewell));
digitalWrite(sol, LOW);
delay(LONG);
}
}
alto777
November 23, 2022, 7:51pm
2
You can name a variable LONG, but for it to be long it has to be declared as long.
When using variables for time, unsigned long is the preferred type
unsigned long LONG = 480000UL;
unsinged long dwell = 30000;
The UL is for large numbers, dunno if it is needed here but it will not hurt.
This
delay(random(dewell));
will delay between 0, not at all, and 29999, about thirty seconds.
You could use another form of calling random ().
If you supply two arguments, the random number will be greater than or equal to the first, and less than the second.
So
delay( random(2000, 10000) );
would delay between two and ten seconds. Use variables like you did when you called it with but one argument if you want.
And just for good sense pick a different name, like longTime or TooLong instead of LONG for the other thing.
a7
Kai-R
November 23, 2022, 7:51pm
3
tkuryga:
Will this work.
No. LONG should at least be a "long int". Otherwise only 21248 ms would be waited for.
system
Closed
May 22, 2023, 7:52pm
4
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.