This post is more of a sticky for how I did it, including the code, so that if others want to duplicate, contribute to it, you can feel free to.
First off, here is the sketch for the arduino. I used an Arduino Uno R3.
Code: Select all//--------------------------------------//
// //
// Randomly turn on and off //
// four relays to control fans //
// to simulate wind in grow tent //
// //
// 05/22/21 //
// rev 001 //
// supercazzola //
// //
//--------------------------------------//
// global scope variables
const long DELAY_MAX = 300000; // manimum delay in ms (5 mins)
const long DELAY_MIN = 10000; // minimum delay in ms (10 secs)
const int NUMBER_OF_FANS = 4; // number of fans (relays)
const int FAN_PINS[NUMBER_OF_FANS] = {8, 9, 10, 11}; // arduino pin numbers used for relays
unsigned long timer[NUMBER_OF_FANS] = {20000, 20000, 20000, 20000}; // array to hold timers for each fan
unsigned long lastFanStateChange[NUMBER_OF_FANS] = {0, 0, 0, 0}; // array to hold when in millis the fan last changed state
int fan_state[NUMBER_OF_FANS] = {LOW, LOW, LOW, LOW}; // LOW or HIGH state of the relays
//-----------------------------------//
void setup()
{
randomSeed(analogRead(0)); // seed the random number generator
Serial.begin(9600); // serial port 9600 baud
for (int i = 0; i < NUMBER_OF_FANS; i++)
{
pinMode(FAN_PINS[i], OUTPUT); // set each pin to OUTPUT type
digitalWrite(FAN_PINS[i], fan_state[i]); // read desired state for each fan servo from global array
}
}
//-----------------------------------//
void loop()
{
unsigned long now = millis(); // time now at start of loop since start of program in ms
for (int i = 0; i < NUMBER_OF_FANS; i++)
{
if ((now - lastFanStateChange[i]) >= timer[i])
{
unsigned long randomValue = random(DELAY_MIN, DELAY_MAX + 1); // variable to store the random number
fan_state[i] = !fan_state[i]; // toggle the state of the fan relay
digitalWrite(FAN_PINS[i], fan_state[i]); // set pin to correct state
timer[i] = randomValue; // set fan relay's timer to new random number
lastFanStateChange[i] = now; // update when the fan relay's state was last changed to now
}
}
}
The components you will need include:
In my case, I hooked up to pins 8,9,10,and 11. Let me know if it doesn't make sense or if you need help with the hookup.
As an aside, I decided to 3d print some cases. I didn't design these. They were on thingiverse. They are:
IMG_1499.jpg (8.82 MiB) Viewed 6775 times