Can you use Analog write & Digital write on the same script?

I have a Model Railroad which requires all projects to work at the same time. I am a beginner so please keep your answer simple.

I have completed 4 projects on my breadboard and have learned I needed to use Millis to have them all run at the same time. I now wanted 3 LED's in a farmhouse where the lights in each room will go off and on at different intervals and would be able to be dimmed. I created this to start with using Analog Write. It worked fine. When I moved it to the millis script that contained all other projects it would not function properly.

Can you not use Analog Write & Digital Write on the same script?

If you can't is there a way to do what I want?

Stay Safe

Harold

Of course you can. Post your code that “doesn’t work”.

int bottomfbPin=11;
int topfbPin=5;
int masterbPin=3;
int bright=255;
int medium=75;
int dim=15;
int off=0;

void setup() {
// put your setup code here, to run once:
pinMode(bottomfbPin,OUTPUT);
pinMode(topfbPin,OUTPUT);
pinMode(masterbPin,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
analogWrite(bottomfbPin,bright);
delay(5000);

analogWrite(bottomfbPin,off);
delay(2500);

analogWrite(topfbPin,bright);
delay(5000);

analogWrite(topfbPin,off);
delay(10000);

analogWrite(masterbPin,bright);
delay(5000);

analogWrite(masterbPin,off);
delay(7500);
}

This worked correctly as you see it. When I copied & pasted it to the code below the other projects stopped blinking and the Bedroom lights all came on and stayed on.

#define INTERVAL_1 500 // ms Traffic Light
#define INTERVAL_2 50 // ms White LED TV
#define INTERVAL_3 70 // ms Blue LED TV

byte led_1; // Yellow Traffic Light
byte led_2; // White TV
byte led_3; // Blue TV
unsigned long time_for_action_1;
unsigned long time_for_action_2;
unsigned long time_for_action_3;

void setup() {
// put your setup code here, to run once:

pinMode(13,OUTPUT);// Traffic Light
pinMode(6,OUTPUT); // TV
pinMode(2,OUTPUT); // TV}
}
void loop() {
if (millis() > time_for_action_1) {
time_for_action_1 = millis() + (unsigned long)INTERVAL_1;
led_1 = !led_1;} // Yellow Blicking Traffic Light

if (millis() > time_for_action_2) {
time_for_action_2 = millis() + (unsigned long)INTERVAL_2;
led_2 = !led_2;} //TV

if (millis() > time_for_action_3){
time_for_action_3 = millis() + (unsigned long)INTERVAL_3;
led_3 = !led_3;} // TV

digitalWrite(13,led_1);
digitalWrite(6,led_2);
digitalWrite(2,led_3);}

millis() is a free running counter that counts the milliseconds since the Arduino booted. So obviously, a statement like

if (millis() > time_for_action_1) {

can not control any repeating action. Technically, it will after about a week when the value rolls over to zero again. But then again, just once and not again for another week.

For the correct way to use millis(), see the documentation and tutorials on this site.

aarg:
For the correct way to use millis(), see the documentation and tutorials on this site.

And the (famous!) example at https://www.arduino.cc/en/tutorial/BlinkWithoutDelay .

Yes, but many people do not realize that there is a tutorial for it:

and the documentation for millis() references the same tutorial, so RTFM gets you all of that.

Thanks
I will check out the tutorial. I am not sure what you are saying about the repeat. The code is working with all the other projects perfectly. I will let you know as I learn more. Thanks again.

Stay Safe

Harold

haroldwolf:
Thanks
I will check out the tutorial. I am not sure what you are saying about the repeat. The code is working with all the other projects perfectly. I will let you know as I learn more. Thanks again.

Stay Safe

Harold

No problem. But if you dig up the other code, you will undoubtedly discover that it is not written the same way.

Your analogue code uses delay().
Your digital code is written (semi) correctly and uses millis() timing.

You cannot combine these without first rewriting the analogue code so as to not use delay().

When the analogue code is off delay()ing, the rest of the program, including the digital code, can’t be doing anything else. Therefore the millis() code can’t detect if a digital transition is required and effect that change until after the delay has finished. By that time (possibly 35 seconds later) it’s too late.

Rewrite the analogue code so it doesn’t use delay(). Also, as others have said, fix your digital timing so it uses millis() correctly while you are at it.

Edit: Steps to achieve this...

  1. Add a millis() timer to the analogue sketch that fires every 5000ms (5s)
  2. Every time the timer “if” statement is executed, add 1 to a counter variable
  3. The counter should count 0, 1, 2, 3, 4, 5, 6 and when it reaches 6 reset back to zero
  4. Depending on the value of the counter (0..5) do the appropriate analogWrite (you have 6 of them)
  5. Adjust the above 6 cases to also set the correct interval for the next millis() timeout (5000, 2500, 5000... etc)
  6. Once you have the analogue sketch working using millis(), only then integrate it into the digital sketch.

Also, as others have said, fix your digital timing so it uses millis() correctly while you are at it.

There is nothing wrong with the digital code unless he OP runs it for more than 52 days or so continuously.

Grumpy_Mike:
There is nothing wrong with the digital code unless he OP runs it for more than 52 days or so continuously.

49.7, but granted.

Still I think it would be better to use the accepted “standard” implementation as it totally avoids the millis() wrap around problem. Especially as this is being embedded in a model and could well be left on for extended periods.

I would also suggest that >= is more correct than >. Using greater-than is an out by one error (not that it will usually make much difference in this case).

Now I am really confused. I started studying the tutorial so I would do it properly. In the meantime I changed the three LED's in the Farmhouse from the Analog to the same digital format I had for the other projects. It all is working perfectly. The reason that the ms is so long at the farmhouse is that it represents people going from room to room at different times.

Am I still missing something in my Coding?

Stay Safe

Harold

It would help enormously if you were to post all the code you have written in one proper posting. Read the how to use this forum sticky post.

Basically reply #8 told you want you need to do. That is eliminate all delays and replace with the millis state machine method.