I show 3 slightly different versions of the same program below. Each program should theoretically take about 10 seconds to finish.
I basically just need to loop some functions over and over a specific number of times and for a specific duration for each part of the loop. Oh and just so you all understand why I did the nested loops when you view the code, I found that the arduino doesn't like it if I try to tell it to do more than 30,000 loops in one statement so I made two nested loops to enable me to go beyond that threshold. If I don't do that and tell it to do 100,000 loops in one statement, for instance, it will get stuck and just stay on forever. I'm not sure why that is or if it is or if it is at all related to the problem I'm presenting below...
Ok well now to the interesting part:
In the first program I have 1,000 loops to perform (10*100) and each loop contains two 5 ms delays. This should take 10 seconds to finish and it does exactly as expected.
//--- Digital Pins - use as many as u need
int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;
void setup()
{
//--- Set up the outputs
pinMode(relayPin7, OUTPUT);
pinMode(relayPin8, OUTPUT);
pinMode(relayPin9, OUTPUT);
pinMode(relayPin10, OUTPUT);
pinMode(relayPin11, OUTPUT);
pinMode(relayPin12, OUTPUT);
pinMode(relayPin13, OUTPUT);
//--- open the serial port - unneccessary, but lets you see output on computer screen
Serial.begin(9600);
}
void loop()
{
{
for (int a=1; a <= 10 ; a++)//loops # times
{
for (int i=1; i <= 100; i++)//loops # times
{
digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
digitalWrite(relayPin8, HIGH);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
delay(5);// keep current flowing for a time measured in ms --- 1000 = 1s
digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, HIGH);
digitalWrite(relayPin11, HIGH);
digitalWrite(relayPin12, HIGH);
digitalWrite(relayPin13, LOW);
delay(5); // keep current flowing for a time measured in ms
}
}
}
while (1) // turn everything off and stay that way forever
{
digitalWrite(relayPin7, LOW);
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
}
}
In the second one I have 10,000 loops to perform (10*1,000) but I reduce the two delay times to 0.5 ms (5/10). This should also take 10 seconds but it only takes 1 second to complete.
//--- Digital Pins - use as many as u need
int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;
void setup()
{
//--- Set up the outputs
pinMode(relayPin7, OUTPUT);
pinMode(relayPin8, OUTPUT);
pinMode(relayPin9, OUTPUT);
pinMode(relayPin10, OUTPUT);
pinMode(relayPin11, OUTPUT);
pinMode(relayPin12, OUTPUT);
pinMode(relayPin13, OUTPUT);
//--- open the serial port - unneccessary, but lets you see output on computer screen
Serial.begin(9600);
}
void loop()
{
{
for (int a=1; a <= 10 ; a++)//loops # times
{
for (int i=1; i <= 1000; i++)//loops # times
{
digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
digitalWrite(relayPin8, HIGH);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
delay(5/10);// keep current flowing for a time measured in ms --- 1000 = 1s
digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, HIGH);
digitalWrite(relayPin11, HIGH);
digitalWrite(relayPin12, HIGH);
digitalWrite(relayPin13, LOW);
delay(5/10); // keep current flowing for a time measured in ms
}
}
}
while (1) // turn everything off and stay that way forever
{
digitalWrite(relayPin7, LOW);
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
}
}
In the third one I have 100,000 loops to perform (10*10,000) but I reduce the two delay times to 0.05ms (5/100). This should also take 10 seconds and now it does just as expected again.
//--- Digital Pins - use as many as u need
int relayPin7 = 7;//these are the digital out pins on the arduino
int relayPin8 = 8;
int relayPin9 = 9;
int relayPin10 = 10;
int relayPin11 = 11;
int relayPin12 = 12;
int relayPin13 = 13;
void setup()
{
//--- Set up the outputs
pinMode(relayPin7, OUTPUT);
pinMode(relayPin8, OUTPUT);
pinMode(relayPin9, OUTPUT);
pinMode(relayPin10, OUTPUT);
pinMode(relayPin11, OUTPUT);
pinMode(relayPin12, OUTPUT);
pinMode(relayPin13, OUTPUT);
//--- open the serial port - unneccessary, but lets you see output on computer screen
Serial.begin(9600);
}
void loop()
{
{
for (int a=1; a <= 10 ; a++)//loops # times
{
for (int i=1; i <= 10000; i++)//loops # times
{
digitalWrite(relayPin7, HIGH); // Make all pins either High or Low (SET operation)
digitalWrite(relayPin8, HIGH);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
delay(5/100);// keep current flowing for a time measured in ms --- 1000 = 1s
digitalWrite(relayPin7, LOW); // define new High or Low values for all pins (RESET operation)
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, HIGH);
digitalWrite(relayPin11, HIGH);
digitalWrite(relayPin12, HIGH);
digitalWrite(relayPin13, LOW);
delay(5/100); // keep current flowing for a time measured in ms
}
}
}
while (1) // turn everything off and stay that way forever
{
digitalWrite(relayPin7, LOW);
digitalWrite(relayPin8, LOW);
digitalWrite(relayPin9, LOW);
digitalWrite(relayPin10, LOW);
digitalWrite(relayPin11, LOW);
digitalWrite(relayPin12, LOW);
digitalWrite(relayPin13, LOW);
}
}
So the big question is... why is the 10,000 loop program seemingly either only performing 1,000 loops or performing 10,000 loops 10 times faster than I'm telling it to?