Go Down

Topic: Shift register 74HC595-switching 2LED at the same (Read 1 time) previous topic - next topic

soulid

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?

brtech

The simplest code to do just what you want, given the definitions and the setup() in the example
Code: [Select]

 // 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:
Code: [Select]

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
Code: [Select]

  setBits(0B10001000);

soulid


brtech

Sorry, too much Hex (0x) on my mind.  The binary constants are B10001000, not 0B10001000.

zenhop

How do you find that Led 3 and 7 = B10001000?
What's the formula?

PaulS

In B10001000, bit 7 (the leftmost) bit is set, as is bit 3. The other bits are cleared.

raron

Quote
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?

soulid

I tried this now and it works :D :D:

Code: [Select]
//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?

AWOL

As long as you're not doing arithmetic on them, "byte"s are "char"s.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

soulid

#9
Apr 07, 2010, 10:18 pm Last Edit: Apr 07, 2010, 10:49 pm by soulid Reason: 1
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:

Code: [Select]
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);
}

:-[

PaulS

Take a look at the bitWrite function. It allows you to set or clear individual bits.

Code: [Select]
byte valToSend = 0;
bitWrite(valToSend, 7, 1); // Set bit 7
bitWrite(valToSend, 3, 1); // Set bit 3


valToSend now contains B10001000

soulid

Dear PaulS
That was the perfect hint! :D

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!

Groove

The reason this:
Code: [Select]
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.
Per Arduino ad Astra

Go Up