On an Arduino, I’m lighting up 8 LEDs with patterns. I have the patterns stored as binary values in an array. I’m stuck with how to read each bit in a pattern and assign to a pin.
I think problem is at the “stuck here” below.
This must be answered already somewhere but I can’t find the right search terms to find it. My hits are similar but much more complex scenarios. Thanks.
byte patterns[3] = {0b10101010, 0b11111111, 0b11011000};
byte pins[8] = {2,3,4,5,6,7,8,9};
void setup() {
// set all led pins to outputs
for (byte currentPin = 0; currentPin <8; currentPin++){
pinMode(pins[currentPin], OUTPUT);
} // for
} // setup
void loop() {
// go through each pattern
for(byte currentPattern=0; currentPattern<3;currentPattern++)
delay(500);
// use bits of current pattern to set pins
/* stuck here:
need to read each bit from the current pattern
and assign to each pin
*/
} // loop
The bitRead() and bitWrite() functions may be useful.
How about this? I used sizeof instead of magic numbers so you can change the number of pins, patterns or bits easier.
byte patterns[3] = {0b10101010, 0b11111111, 0b11011000};
byte pins[8] = {2, 3, 4, 5, 6, 7, 8, 9};
void setup()
{
// set all led pins to outputs
for (byte currentPin = 0; currentPin < sizeof(pins); currentPin++)
{
pinMode(pins[currentPin], OUTPUT);
} // for
} // setup
void loop()
{
// go through each pattern
for (byte currentPattern = 0; currentPattern < sizeof(patterns); currentPattern++)
{
for(byte n = 0; n < sizeof(pins); n++)
{
digitalWrite(pins[n], bitRead(patterns[currentPattern], n));
}
delay(500);
}
} // loop
Hi @JohnKauffman
try this code:
byte patterns[3] = {0b10101010, 0b11111111, 0b11011000};
byte pins[8] = {2,3,4,5,6,7,8,9};
void setup() {
// set all led pins to outputs
for (byte currentPin = 0; currentPin <8; currentPin++){
pinMode(pins[currentPin], OUTPUT);
} // for
} // setup
void loop() {
// go through each pattern
for(byte currentPattern=0; currentPattern<3;currentPattern++)
{
PORTD = patterns[currentPattern] >> 2; // sets digital 2-7 pins
PORTB = patterns[currentPattern] << 6; // sets digital 8 & 9 pins
}
delay(500);
// use bits of current pattern to set pins
/* stuck here:
need to read each bit from the current pattern
and assign to each pin
*/
} // loop
// use bits of current pattern to set pins
byte pattern = patterns[currentPattern];
for (byte currentPin = 0; currentPin < 8; currentPin++)
{
byte bitMask = 0x80 >> currentPin; // Bits left to right
digitalWrite(pins[currentPin], pattern & bitMask);
}
G'Fungus:
Wo-Hoo.
Took your advice at looking into bitRead(). Half hour later, got it all working. And is same as your later post.
Much thanks for starting with just tip so I could work on it.
ruilviana:
I got it working with bitRead(). Now will work on understanding your solution. I have always wanted to learn about bit shifting. Thanks.
JohnWasser:
Got it working with bitRead(). Now it looks like it is time for me to get down to understanding masking.
Thanks.
For future searchers, here is my completed code.
byte patterns[] = {0b11000000, 0b10100000, 0b10010000};
byte pins[8] = {2,3,4,5,6,7,8,9};
byte lenPatterns = 3;
byte currentPattern = 0;
byte index =0; // when read bits: pin index *and* bit index
void setup() {
for (byte currentPin=0; currentPin<8; currentPin++){
pinMode(pins[currentPin], OUTPUT);
} // for
for(currentPattern = 0;currentPattern<lenPatterns;currentPattern++){
delay(1000);
for(index=0;index<8;index++){
Serial.print( bitRead( patterns[currentPattern], index), BIN);
digitalWrite( pins[index], bitRead( patterns[currentPattern], index));
}
Serial.println();
}
}
void loop() {} // loop