Help with causing random events on Uno (Noob)

So I’m new to Arduino and have very limited programming experience. I have a sketch setup that is basically a timer to control functions on a light for a fish tank via an IR emitter. I have it working right now for the basic functions, but I also want it to randomly send the IR command for the light to go into thunderstorm mode. I haven’t decoded all of the function for the IR remote yet, so this sketch only has the PowerButton in it. I have the Hex codes for the functions as well, so I’m hoping to changed from the delay format here to hex on the PWM channel in the future to save code space.

I have no idea how to proceed to randomly cause the t-storm function to occur.

Here’s what I have right now (be gentle, it’s my first time haha). It’s basically just an alarm that fires the emitter at certain times.

////////////SETUP//////////////////////////////////////////////////
#include <Time.h>
#include <TimeAlarms.h>
#include <DateTime.h>
#include <DateTimeStrings.h>

int IRledPin =  13;               // Pin location for IR output

void setup()               
{ pinMode(IRledPin, OUTPUT);       // Designate IRledPin as Output
  Serial.begin(9600);              // Connect @ (Baud)
  setTime(8,29,0,1,1,11);          // set time (HR,MIN,SEC,MO,DAY,YR)
  
  
////////////ALARM FUNCTIONS/////////////////////////////////////////
  Alarm.alarmRepeat(8,30,0, PowerButton);  // (HR,MIN,SEC,FUNCTION)


////////////CLOCK///////////////////////////////////////////////////
void  loop(){                       
  digitalClockDisplay();
  Alarm.delay(1000); }              // Clock display update frequency (msec)

void digitalClockDisplay()          // Digital clock
{ Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); }

void printDigits(int digits)        // Add :
{Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);}

  
////////////SIGNAL///////////////////////////////////////////////////
// Create Frequency (38khz/26msec)
void pulseIR(long microsecs) 
{  cli();                           // kill interupts
  while (microsecs > 0)
{  digitalWrite(IRledPin, HIGH);   // ~3 msec
   delayMicroseconds(7);            // ~delay
   digitalWrite(IRledPin, LOW);     // ~3 msec
   delayMicroseconds(7);            // ~delay
    microsecs -= 26;  }
sei();  }                           // zombie interupts


////////////FIXTURE FUNCTIONS////////////////////////////////////////
void PowerButton()                  //Fixture power on/off toggle
 
{ pulseIR(8840);
  delayMicroseconds(4320);
  
  pulseIR(620);
  delayMicroseconds(480);
  
  pulseIR(600);
  delayMicroseconds(500);
  
  pulseIR(540);
  delayMicroseconds(1660);
  
  pulseIR(580);
  delayMicroseconds(520);
  
  pulseIR(520);
  delayMicroseconds(560);
  
  pulseIR(600);
  delayMicroseconds(480);
  
  pulseIR(620);
  delayMicroseconds(480);
  
  pulseIR(540);
  delayMicroseconds(540);
  
  pulseIR(620);
  delayMicroseconds(1600);
  
  pulseIR(520);
  delayMicroseconds(1660);
  
  pulseIR(580);
  delayMicroseconds(520);
  
  pulseIR(520);
  delayMicroseconds(1660);
  
  pulseIR(600);
  delayMicroseconds(1580);
  
  pulseIR(620);
  delayMicroseconds(1580);
  
  pulseIR(540);
  delayMicroseconds(1660);
  
  pulseIR(600);
  delayMicroseconds(1580);
  
  pulseIR(600);
  delayMicroseconds(500);
  
  pulseIR(600);
  delayMicroseconds(500);
  
  pulseIR(520);
  delayMicroseconds(560);
  
  pulseIR(600);
  delayMicroseconds(500);
  
  pulseIR(600);
  delayMicroseconds(500);
  
  pulseIR(520);
  delayMicroseconds(560);
  
  pulseIR(580);
  delayMicroseconds(1620);
  
  pulseIR(540);
  delayMicroseconds(540);
  
  pulseIR(600);
  delayMicroseconds(1600);
  
  pulseIR(520);
  delayMicroseconds(1660);
  
  pulseIR(580);
  delayMicroseconds(1620);
  
  pulseIR(580);
  delayMicroseconds(1620);
  
  pulseIR(520);
  delayMicroseconds(1660);
  
  pulseIR(580);
  delayMicroseconds(1600);
  
  pulseIR(580);
  delayMicroseconds(520);
  
  pulseIR(580);
  delayMicroseconds(1620);
  
  pulseIR(540);
  delayMicroseconds(38980);
  
  pulseIR(8860);
  delayMicroseconds(2120);
  
  pulseIR(620);  }

sigh
put [ code ] in front of your code and [ / code ] after it
leave out the spaces, they're just there so you can see what to do
your code will then look like this and we can read it properly :slight_smile:

int i=0;

Sorry about that... fixed.

excellent :slight_smile:
now take a look at the random() function
it might return a number that you use to delay then scare the carp out of your fish!

Cool! Is there any limit to how high I can set the random function parameters? I want the t-storm function to go off maybe once or twice a week at most. Can I just figure out how many mseconds are in a week then use that in the random function?

since you don't need to be accurate to the nearest millisecond
take the return from random() and multiply it by something - minutes maybe?

Perfect! That should work nicely.

With random function being only psuedo-random, how large a range does one typically need to specify in the parameters to avoid a predictable cycle?

set the seed with randomSeed()
how smart are your fish anyway!

Haha, I guess they’re intelligent as far as fish go (which isnt really saying much)…

I’m a mechanical engineer and always wanted to experiment with electronics as I never get to do much with electronics in my work. I figured a light controller would be an easy first project.

Thanks for the help. I’m sure I’ll have more issues as I try to implement this :slight_smile: