More repeatability PLEASE

I have a sketch that uses Robin2's blink w/o delay example.. When I run the sketch, I notice a variation in the time delays between the loop cycles. Most noticeable are the two "strobe" routines (output 1 and 4).. They will start off with output 4 flashing. then output 1 then as the Arduino runs, the flash will rotate to output 1 then 4 and back and forth.. Is there a way to keep the outputs in "sync" with their starting sequence?

I plan to use this sketch to replace a clock motor system that uses 20 drums on a shaft and pins inserted into the drum to hit a micro switch to make lights light for a certain amount of time. I cant have "slip" in the loop timing.

_5_slot_timer.ino (4.9 KB)

Try this:

  1. Make the pin assignments byte instead of int. None are greater that 255 in value.
  2. Make all the time assignments unsigned long. Some you just have as "unsigned". millis() returns an unsigned long number.
  3. Put UL after the time constants, example 60000UL.
  4. Use direct port manipulation:

digitalWrite(output_1_Pin, output_1_State);

PIND = PIND | 0b00000100; // flip output bit 2 by writing to input bit register

I added the top 3 suggestions and it's MUCH better but it is still slipping..

The 4th suggestion confuses me..can you point me to the explaination of: PIND = PIND | 0b00000100;

And thanks a MILLION for the help

It's PIND=0b00000100;

The whole point of using the PINx is that you don't have to read the register first - it'll just toggle the pin.

PIND=PIND|0b00000100 would toggle every pin on port D that was currently high, and PD2 no matter what.

OK.. I have added all 4 suggestions and I still don't have a repeatable sequence.. It starts off fine then about the 12th loop it will reverse the order then about 12 more loops and it reverses again.. The time for each loop is changing as well (its pretty noticeable as the flash increase in interval and then decrease)..

Any suggestions?. The clock motor scheme is so old school and I really would love to replace it with the Arduino.

Post the latest version of your program.

...R

They will start off with output 4 flashing. then output 1 then as the Arduino runs, the flash will rotate to output 1 then 4 and back and forth.. Is there a way to keep the outputs in "sync" with their starting sequence?

Hmm, I think it's doing what you told it to do.

Output 1 has a period of 1720ms, and output 4 has a period every 1820ms. They will not stay the same relative to each other, because output 1 is a higher frequency: 1/1.720s = 0.5814Hz. Output 2's frequency is 1/1.820s = 0.5495Hz. They will drift in and out of "sync" as you are seeing. But they're supposed to, because they're running at different frequencies. The beat frequency, or the period of the two going in and out of sync is

   0.5814Hz - 0.5495Hz = 0.0319Hz

That means they go in and out of sync once every 31.35 seconds.

Perhaps they're supposed to be different offsets from some base clock frequency? Do you have a make/model or picture we could see? Or a timetable of events?

Cheers,
/dev

What was wrong with continuing this discussion in your previous thread? Newbie in trouble with Multiple Blink WO Delay - #4 by dcr_inc - Project Guidance - Arduino Forum

I think this is the right time to sit back and plan out your program properly. You've tested blink-without-delay and hopefully you understand what it does. Now you need to map that into your actual problem.

You start with 20 controllable outputs.
You want them to switch on or off at defined times. (How many on/off per output do you want to allow?)
The times are based on the 24-hour cycle. (Do you want it to accurately track the day or is a few percent slippage acceptable?)
You want to be able to redefine the time(s) with the LCD and keypad.

The user-interface code is going to be the most complex part of this. Start with writing the manual - how are the users going to use this? Write out what the display will show and what key presses are required to step between different outputs and times. Then you can write a program to that specification.

Thanks for the guidance.. I set the time interval to the same on both 1 and 4 and low and behold, The outputs do as directed..

Now to write the input part, I hope I have enough smarts to figure it out..

Thanks again