My project is a six barrel gatling gun (mini-gun) toy. My current goal is to have the light inside the barrel light up when the barrel reaches the top of the circle. I
I'm using a DC motor with an encoder. The encoder gets 408.168 counts per rotation.
Barrel 1 will be in the correct position at startup, when the encoder is at 0.
Barrel 2 will be in the correct position at 68 counts
Barrel 3 will be in the correct position at 136 counts so on and so on.
I also want to set a duration variable for how long the light stays on i.e. Barrel 1 will stay on from 0 counts to 10 counts. int duration =10;
This is all easy to write a program for for one rotation, but what if I just want it to perpetually spin. I don't want to reset the encoder counts at 408 because that decimal of .168 will compound with each rotation and eventually make it off.
I started writing an obscenely verbose if statement array
Yes that is correct. I think the number of counts doesn't equal out to a complete rotation: Here is the page for the motor: https://www.pololu.com/product/4844
The encoder is capable of 1632.67 counts per rotation if you count the rise and fall of each sensor. I'm counting the rise of just one (1632.67/4 = 408.1675)
Do you need that resolution? It might not be too hard to make, say, a 6 count per rev encoder with a Hall Effect switch or optical sensor. Then use millis() for the dwell.
I probably don't need that high of resolution, but I do want to make the first barrel return to the top position when it slows to a stop. This way it will always be reset properly. To do that I think I would probably need more than the six steps, but probably not 408.
I have thought about using an absolute position encoder rather than relative, but this is really just a toy and I don't want to start getting into more expensive components if I don't need to. I just figured there is so much about the coding side things I"m not aware of that I might just be missing a function or library that can handle this kind of thing.
i believe you need to accumulate the frac, 0.168 each complete rotation, cnt > 408, and whenever the accumulated fraction > 1, subtract 409 instead of 408 from your count as well as subtracting 1 from the accumulation
Yeah I think you're right. I have an excel sheet set up that does just that for me. I'm just not sure if the best way to implement it into code without writing 50,000 if statements.
I guess I don't mind doing it if that's the way. I just didn't know if there was something more efficient
Without having to write all the "flash light if" statements is there an easy way to do that just as a multiple? I.e every multiple of 68028 flash light
If you could mount magnets in or on the barrels you could make a six PPR encoder. "Fire" each barrel when its magnet passes a Hall switch. Mount the magnet on barrel '1' a few inches further along (out of range) than the others and add another Hall switch for a home position sensor.
there's a line whenever (! (cnt % 68))
1st column is the absolute count
2nd column is a running count is the cnt that's reset each complete rotation
3rd is the adjust offset reduced by 1.0 when >= 1.0
4th column indicates when the extra adjustment is made
Yeah man, that matches my excel Sheet exactly. My column 1 matches your column 2 which I think is the most important part as far as the correct integer value with the remainder added when appropriate.