I need to program a little chaser. That means, I need to send some binary code like this:
0000
0001
0010
0100
1000
then loop
I have done this in Basic where I can work in nibbles or bytes. But my understanding is that in Arduino, I can only work with one output bit at a time.
My output will be 4 consecutive pins and I need to provide a variable that is a delay between outputs.
Please help me learn now this is done when I can only write to a bit at a time.
Or, maybe I CAN make a nibble or byte and output a byte at a time?
I don't need much to get me started. My last question on the programming part of this forum turned out great! I was able to take the hints and sample code given to me here and that project is well underway.
I can't get you a link right now, but google "Arduino port manipulation" and take the first hit to this site. You can write a whole byte at a time as long as the pins are on the same port.
This is part of a stepper motor program but should work as a chaser, connect 4 LEDs up to pins 8,9,10,11 and give it a try. You can change the bit patterns in line 10 for different effects.
/*
Type a number in top of serial monitor for steps to go,
-32000 (CCW) to 32000 (CW), & press [ENTER].
*/
const byte red = 8,
yellow = 9,
green = 10,
blue = 11,
stepNr [4] = {0b0001, 0b0010, 0b0100, 0b1000};
byte cntr = 0;
int dlay = 100, // delay between steps, higher num, lower speed
stg = 16; // steps to go
void setup()
{
Serial.begin(9600); // set serial monitor baud rate to match
DDRB = 0x0F; // set pins 8,9,10,11 to OUTPUT
}
void loop()
{
if (stg != 0)
{
PORTB = stepNr[cntr & 0x3] & 0x0F;
delay(dlay);
stg < 0 ? cntr-- : cntr++; // sets direction
stg < 0 ? stg++ : stg--; // adds to or subtracts from stg
// depending on direction
}
if (Serial.available() > 0)
{
stg = Serial.parseInt();
Serial.println(stg);
}
}