Pages: [1]   Go Down
Author Topic: Shift register 74HC595-switching 2LED at the same  (Read 1421 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Full Member
***
Karma: 0
Posts: 139
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The simplest code to do just what you want, given the definitions and the setup() in the example
Code:
 // 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:
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:
  setBits(0B10001000);
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'll try this soon. Thanks for your help!
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 139
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried this now and it works smiley-grin smiley-grin:

Code:
//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 Offline
Brattain Member
*****
Karma: 310
Posts: 26635
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

valToSend now contains B10001000
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 140
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear PaulS
That was the perfect hint! smiley-grin

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 Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The reason this:
Code:
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

Pages: [1]   Go Up
Jump to: