0
Offline
Full Member
Karma: 1
Posts: 137
|
 |
« on: April 02, 2010, 02:42:33 pm » |
Looking to the sample code I found in the tutorial section ( http://www.arduino.cc/en/Tutorial/ShiftOut) I can switch "on" LEDs one by one using the second example. In my specific case I want to light up for example LED3 and LED7 I need to switch between LED3 and LED7 which reduces the lumen output by at least 50%, correct? How would a code look like to have the cases as described above, so switchin on two or three LEDs the same time?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 139
Arduino rocks
|
 |
« Reply #1 on: April 03, 2010, 08:11:56 am » |
The simplest code to do just what you want, given the definitions and the setup() in the example // turn off the output so the pins don't light up // while you're shifting bits: digitalWrite(latchPin, LOW);
// shift the bits out: 0B10001000 sets pins 3 and 7 shiftOut(dataPin, clockPin, MSBFIRST, 0B10001000);
// turn on the output so the LEDs can light up: digitalWrite(latchPin, HIGH);
Probably you would be wise to use a procedure to set the value: void setBits(int val) { // turn off the output so the pins don't light up // while you're shifting bits: digitalWrite(latchPin, LOW);
// shift the bits out: shiftOut(dataPin, clockPin, MSBFIRST, val);
// turn on the output so the LEDs can light up: digitalWrite(latchPin, HIGH); } and then call it like setBits(0B10001000);
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 137
|
 |
« Reply #2 on: April 04, 2010, 01:53:01 am » |
I'll try this soon. Thanks for your help!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 139
Arduino rocks
|
 |
« Reply #3 on: April 04, 2010, 07:28:22 am » |
Sorry, too much Hex (0x) on my mind. The binary constants are B10001000, not 0B10001000.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #4 on: April 06, 2010, 06:20:37 pm » |
How do you find that Led 3 and 7 = B10001000? What's the formula?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #5 on: April 07, 2010, 09:07:33 am » |
In B10001000, bit 7 (the leftmost) bit is set, as is bit 3. The other bits are cleared.
|
|
|
|
|
Logged
|
|
|
|
|
Norway
Offline
Sr. Member
Karma: 4
Posts: 422
microscopic quantum convulsions of space-time
|
 |
« Reply #6 on: April 07, 2010, 11:07:55 am » |
I can switch "on" LEDs one by one using the second example. In my specific case I want to light up for example LED3 and LED7 I need to switch between LED3 and LED7 which reduces the lumen output by at least 50%, correct? Not neccesarily. That second example you mention, is a simple example limited to only set one output pin at a time via serial. To set more simultanously, you would have to modify the code. You can set all 8 bits, or any combination, in a shiftregister as the posts above shows. Did you try the first example?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 137
|
 |
« Reply #7 on: April 07, 2010, 02:57:41 pm » |
I tried this now and it works  : //Pin connected to ST_CP of 74HC595 int latchPin = 8; //Pin connected to SH_CP of 74HC595 int clockPin = 12; ////Pin connected to DS of 74HC595 int dataPin = 11; //holder for information you're going to pass to shifting function
void setup() { //set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);
}
void loop() {
// turn off the output so the pins don't light up // while you're shifting bits: digitalWrite(latchPin, LOW);
// shift the bits out: 0B10001000 sets pins 3 and 7 shiftOut(dataPin, clockPin, MSBFIRST, B10001000); digitalWrite(latchPin, HIGH); }
Now my next step is to develop a code connecting single bytes from different variables to some kind of a "string of bytes" I can shift out with the sketch above. I tried to do it with the "strcat" command, but it works with char only :-[ Is there a similar command for bytes?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: April 07, 2010, 03:02:54 pm » |
As long as you're not doing arithmetic on them, "byte"s are "char"s.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 137
|
 |
« Reply #9 on: April 07, 2010, 03:18:38 pm » |
I do not wan to make arithmetics with them. I (think I) tried to execute what you proposed, but the result is failure message: In function 'void loop()': error: invalid conversion from 'char' to 'const char*/The code I used is the following: int latchPin = 8; int clockPin = 12; int dataPin = 11;
char bytestosend[50]; char first = 1; char second = 0; char third = 0; char fourth = 1;
void setup() { pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); }
void loop() {
digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, B01000100); digitalWrite(latchPin, HIGH); bytestosend[0] = 0; strcat (bytestosend, first); strcat (bytestosend, second); strcat (bytestosend, third); strcat (bytestosend, fourth); // just to check what I have done // expecting 1001 Serial.println(bytestosend); } :-[
|
|
|
|
« Last Edit: April 07, 2010, 03:49:42 pm by soulid »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #10 on: April 07, 2010, 05:02:13 pm » |
Take a look at the bitWrite function. It allows you to set or clear individual bits. byte valToSend = 0; bitWrite(valToSend, 7, 1); // Set bit 7 bitWrite(valToSend, 3, 1); // Set bit 3 valToSend now contains B10001000
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 137
|
 |
« Reply #11 on: April 09, 2010, 07:02:49 am » |
Dear PaulS That was the perfect hint!  My system works fine with 8bit messages. I need to etent that to 24 bit but I think that isn't an issue. Thanks to the other members of the Thread leading to that solution!
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #12 on: April 09, 2010, 07:25:08 am » |
The reason this: bytestosend[0] = 0; strcat (bytestosend, first); failed to compile is because "strcat" expects two strings (char arrays) as arguments, whereas "first" is a simple "char" scalar. However, it wasn't a viable solution in the first place.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
|