Hello forum,
I am a newB at this forum and in C/C++ programming as well. (I did some turbo Pascal programming before), so I hope my question is not the one that has already been answered many times before...
Lately I bought my Arduino for a project but in my first serious trials things work another way then I expected.
I try to make a (relatively simple) timer with which I can switch a light at random. Random times should vary between about 3 minutes and 33 minutes. The light should stay on for about 12 seconds.
Another light should switch on 2 seconds after the first light switches on, but switch off at the same moment.
Not necessary but practical is the third channel I included for a control led that shows activity, shortly blinking each second.
The program seems to work but random times vary only between 3 minutes and say 11 minutes.
No matter my changes in the numbers. So my first question here is: What did I do wrong there?
Second: When I want to use more randomized values do I need to put in extra pinreadings to read another pin or can I just keep on referring to a read of pin0?
Another thing is that in order to learn the programming I tried to avoid hard delays (so I based my sketch on the "blinking without delays" example), but in the case of the light that switches on 2 seconds after the first, I didn't manage to get that working.
This is not a serious problem for this specific project, but in order to improve my skills and a better understanding of the syntax I would like to get that fixed as well.
underneath my code so far (so here with the delay I try to get rid off).
int Pin13 = 13; //control led
int Pin12 = 12; //maintimer/switch
int Pin11 = 11; //second switch following mainswitch with startdelay.
int value = LOW;
long previousMillis13a = 0;
long previousMillis13u = 0;
long aan13 = (20);
long uit13 = (980);
long previousMillis12a = 0;
long previousMillis12u = 0;
long aan12;
long uit12;
void setup()
{
Serial.begin(9600);
pinMode(Pin13, OUTPUT);
pinMode(Pin12, OUTPUT);
pinMode(Pin11, OUTPUT);
}
void loop()
{
if (millis() - previousMillis13a > aan13) //"blinking without delay" for the control led
{
previousMillis13a = millis();
if (value == HIGH)
value = LOW;
digitalWrite(Pin13, value);
if (millis() - previousMillis13u > uit13)
{
previousMillis13u = millis();
if (value == LOW)
value = HIGH;
digitalWrite(Pin13, value);
}
}
aan12 = 10000;
if (millis() - previousMillis12a > aan12)
{
previousMillis12a = millis();
digitalWrite(Pin12, LOW);
digitalWrite(Pin11, LOW);
randomSeed(analogRead(0));
uit12 = (random(200,2200)*1000); //200000=3minutes 2200000=33minutes
if (millis() - previousMillis12u > uit12)
{
previousMillis12u = millis();
digitalWrite(Pin12, HIGH);
delay(2000); //the delay I would like to get rid off
digitalWrite(Pin11, HIGH);
}
}
}
thank you all in advance for your help!
Matthijs