I'm a fairly new with Arduino coding and have been trying to build my skills with direct port manipulation. I have 4 LEDs in which I am trying to strobe LEDs 1 & 4 while blinking LEDs 2 & 3. When I push a button, the strobing effect stops on LEDs 1 & 4 but LEDs 2 & 3 continue to blink. Everything for the most part is working, except when the strobe effect is activated I lose the brightness of the middle LEDs. I'm guessing it must have to do with my calls to the PORTD causing a 50% duty cycle on the middle LEDs. I've tried some examples of bitwise OR operators in which I can get the middle LEDs to come on full brightness but then they are trying to strobe. Is there a way to flash the LEDs 2 & 3 at full brightness while strobing LEDs 1 & 4 using Port Manipulation? Any help would be much appreciated. Here is my sketch:
The button is only there to toggle between the two patterns. Press the button and the outer LEDs stop strobing. Press it again and the outer LEDs strobe again. I have tried this with digitalWrite commands. The following code seems to work. I am just wondering if there is a way to do the same with using the port commands. I know it is harder...I'm just trying to learn some new skills.
Generally speaking, yes of course. That is all and what digitalWrite and Read can use to do i/o, after all. There's nothing you can't do yourself in your own code. You can def manipulate one bit without effecting any others on the port.
Now that we all are sure the code works logically speaking with digitalWrite, the translation to port manipulation is nearly trivial.
It will depend on some bit wise masking and such like; I will try it when I am at the big rig or someone will before that time.
I assume you've poked around a bit on the internets for a tutorial or two on this, it's fairly straight ahead.
When you write to the PORTD port like that, you are setting all 8 bits. In general you want to change only a subset of the bits. To turn on a set of bits, use: PORTD |= B00110000;
To turn off a set of bits, use: PORTD &= ~B00110000;
These use the bitwise OR and AND operations to force some bits ON or OFF while not changing the other bits.
When speed is important like really important the thing @johnwasser reiterates is useful.
The code can be simpler easier to follow if you just deal with one bit at a time, unless of course you are talking parallel bits to some other hardware that would better see them all change at once.
S'all up to you, naturally. The dual edge sword which is expressing yourself in code. At least dual. Edgy sword, that.