I am using millis to flash leds at different speeds and this works fine. However i would like to randomly change the offtime, and i cant get this to work. To see if the "if" condition is triggering a change i changed it to simply change offtime to a new number and still nothing happens. The following code is in void loop.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 >= 20000)
{
previousMillis1 = currentMillis;
OffTime1 = 900;
}
However offtime remains unchanged at the original value
That code does not compile and is not in a function.
Help us help you.
Please follow the advice given in this link How to get the best out of this forum when posting code, in particular the section entitled 'Posting code and common code problems'.
Things that suck:
a). Images of code suck.
b). Images of error messages suck.
c). Code snippets suck.
Use code tags (the </> icon above the compose window) to make it easier to read and copy code for examination
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.
Post a schematic.
Post an image of your project.
Which Micro Controller are you using?
Is this simulator code?
Please describe the problem better then you just did.
b707
March 16, 2023, 10:17am
3
This code has nothing to do with your problem. Could you please to show a relevant code?
gcjr
March 16, 2023, 10:33am
5
consider
const byte PinLed = LED_BUILTIN;
unsigned long msecPeriod;
unsigned long msecLst;
unsigned long msecLed [] = { 500, 2000};
// -----------------------------------------------------------------------------
void
loop (void)
{
unsigned long msec = millis ();
if (msec - msecLst >= msecPeriod) {
msecLst = msec;
digitalWrite (PinLed, ! digitalRead (PinLed));
if (LOW == digitalRead (PinLed)) {
msecPeriod = random(1,10) * 100;
Serial.println (msecPeriod);
}
else
msecPeriod = msecLed [digitalRead (PinLed)];
}
}
void
setup (void)
{
Serial.begin (9600);
pinMode (PinLed, OUTPUT);
}
1 Like
Excluding the code which initialises the variables, here is the full void loop.
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 >= 20000)
{
previousMillis1 = currentMillis;
OffTime1 = 900;
}
if(currentMillis - previousMillis2 >= 20000)
{
previousMillis2 = currentMillis;
OffTime2 = random(600, 1500);
}
if((switchpos1 == HIGH) && (currentMillis - previousMillis >= 1000)) //1st octal tranceiver
{
switchpos1 = LOW;
switchpos2 = HIGH;
previousMillis = currentMillis; // Remember the time
digitalWrite(switch1, switchpos1); // Update the actual LED
digitalWrite(switch2, switchpos2);
}
else if((switchpos1 == LOW) && (currentMillis - previousMillis >= 500))
{
switchpos1 = HIGH;
switchpos2 = LOW;
previousMillis = currentMillis; // Remember the time
digitalWrite(switch1, switchpos1);
digitalWrite(switch2, switchpos2);
}
if((chipState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
chipState1 = LOW; // Turn it off
previousMillis1 = currentMillis; // Remember the time
digitalWrite(chipPin1, chipState1); // Update the actual LED
}
else if ((chipState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
chipState1 = HIGH; // turn it on
previousMillis1 = currentMillis; // Remember the time
digitalWrite(chipPin1, chipState1); // Update the actual LED
}
if((chipState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
{
chipState2 = LOW; // Turn it off
previousMillis2 = currentMillis; // Remember the time
digitalWrite(chipPin2, chipState2); // Update the actual LED
}
else if ((chipState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
{
chipState2 = HIGH; // turn it on
previousMillis2 = currentMillis; // Remember the time
digitalWrite(chipPin2, chipState2); // Update the actual LED
}
}
whether using the random function or just setting a new number, OffTime1 and OffTime2 retain their original values.
b707
March 16, 2023, 11:00am
7
Your code contains an eight millis conditions and use only three previousMillis
variables. In general, each such condition must use a separate variable.
Hello cookie1310
Below you see another solution for your school assignment.
#define usl unsigned long // I´m lazy to type
// make names
enum Switch{Off,On};
// make variables
usl offOn[] {2000,500};
constexpr int LedPin {9};
usl timeTag;
void setup()
{
pinMode(LedPin,OUTPUT);
}
void loop()
{
if (millis()-timeTag>= offOn[digitalRead(LedPin)])
{
timeTag=millis();
digitalWrite(LedPin,digitalRead(LedPin)?LOW:HIGH);
if (digitalRead(LedPin)==LOW) offOn[Off]=(usl)random(100,2000);
}
}
Have a nice day and enjoy coding in C++.
gcjr
March 16, 2023, 11:16am
9
it would help if you posted how the various timer variables are initialized
looks like previousMillis2 is reset (to currentMillis) in the chipState2 conditions, preventing the case where offTime2 being set to a random variable is ever set
again, guessing because you didn't post the entire code
Ah yes, well spotted. Problem solved. Thanks.
system
Closed
September 12, 2023, 12:05pm
11
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.