I wired up two 74HC595 chips with 16 LEDs, and wrote some simple code. They are counting in binary... How do I change this?
int dataPin = 13;
int latchPin = 12;
int clockPin = 11;
void setup()
{
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 4);
shiftOut(dataPin, clockPin, MSBFIRST, 3);
delay(10);
digitalWrite(latchPin, HIGH);
}
system
July 11, 2010, 1:08pm
2
Use this instead:
shiftOut(dataPin, clockPin, MSBFIRST, 1<<3);
shiftOut(dataPin, clockPin, MSBFIRST, 1<<2);
1<<0 = 00000001
1<<1 = 00000010
1<<2 = 00000100
etc.
Ok, it works. I realised that I have to add that 1 to my other number. As in 1<<5 is light 6, ect. Thanks!
system
July 11, 2010, 3:16pm
4
Well... in the code you posted you sent the numbers 4 and 3 to the shift registers. As you said they 'counted in binary' I assume you wanted to light up the 4th and 3rd LED on the chips, which should work for 4, but not for 3.
Just replace 4 with 1<<3 and so on. '<<' is a bit shift operator which shifts the content of a byte n-bits to the left. In this example 1 is shifted 3 times to the left, resulting in the 4th LED lighting up.
Edit: seems you've found that one out already.
So how would I do no lights on?
system
July 11, 2010, 3:55pm
6
You'll have to zero in on that one yourself
system
July 11, 2010, 5:27pm
7
We'll give you ZERO help on that one whatsoever.
Sorry I can't resist a punny reponse.
LOL! I guess I would do a 9? As in 1<<8???
system
July 12, 2010, 11:27am
9
Well, I'm not 100% positive on that, but 1<<8 might be automatically promoted to a 16bit variable. What ShiftOut does with that one I don't know.
What ShiftOut does with that one I don't know.
It shifts out only a byte so if you do this you get nothing.
http://www.arduino.cc/en/Reference/ShiftOut
system
July 12, 2010, 12:49pm
11
So it's a good thing I don't use ShiftOut
Apart from me not knowing about its endian-ness, it is too slow for any serious PWM jobs anyway.