Background: The code below is being used to control 2 motors, 2 LED's, and in conjunction with Gobetwino, 2 programs on the computer. Alarms are setup 8 times a day to randomly run 1 of 3 functions at a given alarm time.
Problem: I want to ensure each randomly called function runs exactly 4 times per day. At the moment there is no guarantee that a function will A.) run at least once everyday and B.) not be randomly chosen to run all 12 times a day.
I searched the forum and came across one proposed method to get this done using an array, which would contain 12 elements, in my case:
A for loop to process each element in the array.
A while loop inside it to process the current element repeatedly until a unique random number had been found
A for loop inside that to compare the currently proposed random number against all the preceding elements in the array
Could somebody please explain to me how to "process" and "compare" the elements or suggest a different way to get this done? Any help would be greatly appreciated. Thank you!
#include <Time.h>
#include <TimeAlarms.h>
int randNumber;
int dirpin = 11; //small motor
int steppin = 12;
int dirpinb = 8; //big motor
int steppinb = 9;
int redled = 4; //red LED
int greenled = 5; //green LED
void setup()
{
Serial.begin(9600); //initialize communication with chip
setTime(14,44,30,2,24,14); //set time
Alarm.alarmRepeat(14,45,0, AlarmF); //set first alarm time
Alarm.alarmRepeat(14,47,0, AlarmF); //set second alarm time
Alarm.alarmRepeat(14,49,0, AlarmF); //set third alarm time
Alarm.alarmRepeat(14,51,0, AlarmF); //set fourth alarm time
Alarm.alarmRepeat(14,53,0, AlarmF); //set fifth alarm time
Alarm.alarmRepeat(14,55,0, AlarmF); //set sixth alarm time
Alarm.alarmRepeat(14,57,0, AlarmF); //set seventh alarm time
Alarm.alarmRepeat(14,59,0, AlarmF); //set eighth alarm time
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(dirpinb, OUTPUT);
pinMode(steppinb, OUTPUT);
randomSeed(analogRead(0)); //generate different random number
}
void loop()
{
Alarm.delay(0);
}
void AlarmF()
{
int randNumbers;
randNumbers = random(1, 13); //generate a random number between 1 and 12
if (randNumbers == 1 || randNumbers == 2 || randNumbers == 3 || randNumbers == 4)
{
void UpwardsFM();
}
else if (randNumbers == 5 || randNumbers == 6 || randNumbers == 7 || randNumbers == 8)
{
void DownwardsFM();
}
else if (randNumbers == 9 || randNumbers == 10 || randNumbers == 11 || randNumbers == 12)
{
void Sham();
}
}
void UpwardsFM()
{
int i;
Serial.print("#S|OPENVID|["); //gobetwino opens video capture software
Serial.println("]#");
delay(15000); //wait 15 seconds
Serial.println("#S|SENDK|[0&{F5}]#"); //gobetwino hits F5 to start recording
delay(10000); //wait 10 seconds
digitalWrite(redled, HIGH); //turn on red LED
digitalWrite(greenled, HIGH); //turn on green LED
Serial.print("#S|AUDIO1|["); //gobetwino opens audio1
Serial.println("]#");
delay(5000); //wait 5 seconds
digitalWrite(dirpinb, HIGH); //set the big motor to rotate clockwise
digitalWrite(dirpin, LOW); //set the small motor to rotate clockwise
for (i = 0; i<50; i++) //rotate the big motor 50 steps
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(1000); //wait 1 second
for (i = 0; i<200; i++) //feed the fish AKA rotate the small motor 200 steps
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(8000);
}
delay(1000); //wait 2 seconds
digitalWrite(dirpinb, LOW); //change the direction of the big motor to counterclockwise
for (i=0; i<50; i++) //return the big motor to its starting position
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
Serial.println("#S|SENDK|[1&^q]#"); //gobetwino quits VLC
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[0&%{F4}~]#"); //gobetwino hits ALT+F4 to quit video capture software
}
void DownwardsFM()
{
int i;
Serial.print("#S|OPENVID|["); //gobetwino opens video capture software
Serial.println("]#");
delay(15000); //wait 15 seconds
Serial.println("#S|SENDK|[0&{F5}]#"); //gobetwino hits F5 to start recording
delay(10000); //wait 10 seconds
digitalWrite(redled, HIGH); //turn on red LED
digitalWrite(greenled, HIGH); //turn on green LED
Serial.print("#S|AUDIO2|["); //gobetwino opens audio2
Serial.println("]#");
delay(5000); //wait 5 seconds
digitalWrite(dirpinb, LOW); //set the big motor to rotate counterclockwise
digitalWrite(dirpin, LOW); //set the small motor to rotate clockwise
for (i = 0; i<50; i++) //rotate the big motor 50 steps
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(1000); //wait 1 second
for (i = 0; i<200; i++) //feed the fish AKA rotate the small motor 200 steps
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(8000);
}
delay(1000); //wait 1 seconds
digitalWrite(dirpinb, HIGH); //change the direction of the big motor to counterclockwise
for (i=0; i<50; i++) //return the big motor to its starting position
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
Serial.println("#S|SENDK|[1&^q]#"); //gobetwino quits VLC
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[0&%{F4}~]#"); //gobetwino hits ALT+F4 to quit video capture software
}
void Sham()
{
int i;
randNumber = random(1, 3); //generate a random number: 1 or 2
if (randNumber == 1)
{
digitalWrite(dirpinb, HIGH); //set the big motor to rotate clockwise
digitalWrite(dirpin, LOW); //set the small motor to rotate clockwise
Serial.print("#S|OPENVID|["); //gobetwino opens video capture software
Serial.println("]#");
delay(15000); //wait 15 seconds
Serial.println("#S|SENDK|[0&{F5}]#"); //gobetwino hits F5 to start recording
delay(10000); //wait 10 seconds
digitalWrite(redled, HIGH); //turn on red LED
digitalWrite(greenled, HIGH); //turn on green LED
Serial.print("#S|AUDIO3|["); //gobetwino opens audio3
Serial.println("]#");
delay(5000); //wait 5 seconds
for (i = 0; i<50; i++) //rotate the big motor 50 steps
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[1&^q]#"); //gobetwino quits VLC
digitalWrite(redled, LOW); //turn off red LED
digitalWrite(greenled, LOW); //turn off green LED
digitalWrite(dirpinb, LOW); //set the big motor to rotate counterclockwise
for (i = 0; i<50; i++) //rotate the big motor 50 steps to return to rest position
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[0&%{F4}~]#"); //gobetwino hits ALT+F4 to quit video capture software
}
else if (randNumber == 2)
{
digitalWrite(dirpinb, LOW); //set the big motor to rotate clockwise
digitalWrite(dirpin, LOW); //set the small motor to rotate clockwise
Serial.print("#S|OPENVID|["); //gobetwino opens video capture software
Serial.println("]#");
delay(15000); //wait 15 seconds
Serial.println("#S|SENDK|[0&{F5}]#"); //gobetwino hits F5 to start recording
delay(15000); //wait 15 seconds
digitalWrite(redled, HIGH); //turn on red LED
digitalWrite(greenled, HIGH); //turn on green LED
Serial.print("#S|AUDIO3|["); //gobetwino opens audio3
Serial.println("]#");
delay(5000); //wait 5 seconds
for (i = 0; i<50; i++) //rotate the big motor 50 steps
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[1&^q]#"); //gobetwino quits VLC
digitalWrite(redled, LOW); //turn off red LED
digitalWrite(greenled, LOW); //turn off green LED
digitalWrite(dirpinb, HIGH); //set the big motor to rotate counterclockwise
for (i = 0; i<50; i++) //rotate the big motor 50 steps to return to rest position
{
digitalWrite(steppinb, LOW);
digitalWrite(steppinb, HIGH);
delayMicroseconds(8000);
}
delay(10000); //wait 10 seconds
Serial.println("#S|SENDK|[0&%{F4}~]#"); //gobetwino hits ALT+F4 to quit video capture software
}
}