Go Down

Topic: 2pcs 74HC595 with 16ch led + potentiometer. (Read 1 time) previous topic - next topic

gavron04

Hello. I have connected board like this:





+ potentiometer connected to PIN A0.

I have modified code from click

After modified:


Code: [Select]
int latchPin = 4;
int clockPin = 5;
int dataPin = 3;

int potPin = 0;
int potVal = 0;
byte leds = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
}

void loop()
{


  updateShiftRegister();
  potVal = analogRead(potPin);
  if(potVal <= 63&& potVal > 5)
  {
    bitSet(leds,0);
    updateShiftRegister(); 
  }
 
  else if(potVal > 63&& potVal <= 126)
  {
    bitSet(leds,1);
    updateShiftRegister(); 
  }
 
  else if(potVal > 126&& potVal <= 189)
  {
    bitSet(leds,2);
    updateShiftRegister(); 
  }
 
  else if(potVal > 189&& potVal <= 252)
  {
    bitSet(leds,3);
    updateShiftRegister(); 
  }
 
  else if(potVal > 252&& potVal <= 315)
  {
    bitSet(leds,4);
    updateShiftRegister(); 
  }
 
  else if(potVal > 315&& potVal <= 378)
  {
    bitSet(leds,5);
    updateShiftRegister(); 
  }
 
  else if(potVal > 378&& potVal <= 441)
  {
    bitSet(leds,6);
    updateShiftRegister(); 
  }
 
  else if(potVal > 441&& potVal <= 504)
  {
    bitSet(leds,7);
    updateShiftRegister(); 
  }
 
  else if(potVal > 504&& potVal <= 567)
  {
    bitSet(leds,8);
    updateShiftRegister(); 
  }
 
  else if(potVal > 567&& potVal <= 630)
  {
    bitSet(leds,9);
    updateShiftRegister(); 
  }
 
  else if(potVal > 630&& potVal <= 693)
  {
    bitSet(leds,10);
    updateShiftRegister(); 
  }

  else if(potVal > 693&& potVal <= 756)
  {
    bitSet(leds,11);
    updateShiftRegister(); 
  }

  else if(potVal > 756&& potVal <= 819)
  {
    bitSet(leds,12);
    updateShiftRegister(); 
  }
 
  else if(potVal > 819&& potVal <= 882)
  {
    bitSet(leds,13);
    updateShiftRegister(); 
  } 

  else if(potVal > 882&& potVal <= 945)
  {
    bitSet(leds,14);
    updateShiftRegister(); 
  }
 
  else if(potVal > 945&& potVal <= 1023)
  {
    bitSet(leds,15);
    updateShiftRegister(); 
  } 

     
  if(potVal < 945)
  {
    bitClear(leds,15);
  }
  if(potVal < 882)
  {
    bitClear(leds,14);
  }
  if(potVal < 819)
  {
    bitClear(leds,13);
  }
  if(potVal < 756)
  {
    bitClear(leds,12);
  }
  if(potVal < 693)
  {
    bitClear(leds,11);
  }
  if(potVal < 630)
  {
    bitClear(leds,10);
  }
  if(potVal < 567)
  {
    bitClear(leds,9); 
  }
  if(potVal < 504)
  {
    bitClear(leds,8); 
  }
  if(potVal < 441)
  {
    bitClear(leds,7); 
  }
  if(potVal < 378)
  {
    bitClear(leds,6); 
  }
  if(potVal < 315)
  {
    bitClear(leds,5); 
  }
  if(potVal < 252)
  {
    bitClear(leds,4); 
  }
  if(potVal < 189)
  {
    bitClear(leds,3); 
  }
  if(potVal < 126)
  {
    bitClear(leds,2); 
  }
  if(potVal < 63)
  {
    bitClear(leds,1); 
  }
  if(potVal <5)
  {
    bitClear(leds,0);
  }


}

void updateShiftRegister(){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin,clockPin,MSBFIRST,leds);
  digitalWrite(latchPin,HIGH); 
}



and the problem is that I have 2 pcs of 74HC595 and I don't know how can I change the code to work properly. Now I can turn on 2 led at the same time. I can't control each led "one by one" > 1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16.



Regards

AWOL

You'll need a sixteen bit "leds" variable, won't you?
"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.
I speak for myself, not Arduino.

Grumpy_Mike

#2
Aug 08, 2017, 10:52 pm Last Edit: Aug 08, 2017, 10:52 pm by Grumpy_Mike
Quote
I have connected board like this:
Then immediately remove that 1uF capacitor. It could damage your Arduino. That is a crap page you copied the circuit from.

You need a 0.1uF ceramic capacitor between power and ground of each 595 chip.

Wawa

Then immediately remove that 1uF capacitor. It could damage your Arduino. That is a crap page you copied the circuit from.
Long standing error (one of the many) on THIS site.
https://www.arduino.cc/en/Tutorial/ShiftOut
Cap should co between VCC (red wire) and ground.
Leo..

gavron04

*I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.




Wawa

I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.
That's fine, but remove the cap between latch pin and ground.
It does not belong there.
Leo..

Grumpy_Mike

*I have 4,7uF smd + 100nF smd between GND and VCC near 74HC595.
Then why was that not on the schematic you posted?

You need a 0.1uF ceramic capacitor on every 74HC595.

Grumpy_Mike

Code: [Select]
byte leds = 0;
and
Code: [Select]
bitClear(leds,11);
Do not sit nicely together do they.

A byte has only 8 bits, numbered 0 to 7. Read again reply 1

gavron04

If I change:

Code: [Select]
byte leds = 0 ;

to:

Code: [Select]
int leds = 0 ;

nothing changes

aarg

If I change:

Code: [Select]
byte leds = 0 ;

to:

Code: [Select]
int leds = 0 ;

nothing changes
...because you only shift out 8 bits.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.


gavron04

Ok. Got it.

Code: [Select]
int latchPin = 4;
int clockPin = 5;
int dataPin = 3;


int potPin = 0;
int potValue = 0;
int shiftValue = 0;
//uint16_t leds = 0;


void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
}



void loop()
{
// read pot &  update outputs to 2 daisy chained shift registers
potValue = analogRead(A0);
if ( potValue >=0 && potValue <=63)
{
shiftValue = 0b0000000000000001;
}

if ( potValue >=63 && potValue <=126)
{
shiftValue = 0b0000000000000011;
}

if ( potValue >=126 && potValue <=189)
{
shiftValue = 0b0000000000000111;
}

if ( potValue >=189 && potValue <=252)
{
shiftValue = 0b0000000000001111;
}

if ( potValue >=252 && potValue <=315)
{
shiftValue = 0b0000000000011111;
}

if ( potValue >=315 && potValue <=378)
{
shiftValue = 0b0000000000111111;
}

if ( potValue >=378 && potValue <=441)
{
shiftValue = 0b0000000001111111;
}

if ( potValue >=441 && potValue <=504)
{
shiftValue = 0b0000000011111111;
}

if ( potValue >=504 && potValue <=567)
{
shiftValue = 0b0000000111111111;
}

if ( potValue >=567 && potValue <=630)
{
shiftValue = 0b0000001111111111;
}

if ( potValue >=630 && potValue <=693)
{
shiftValue = 0b0000011111111111;
}

if ( potValue >=693 && potValue <=756)
{
shiftValue = 0b0000111111111111;
}

if ( potValue >=756 && potValue <=819)
{
shiftValue = 0b0001111111111111;
}

if ( potValue >=819 && potValue <=882)
{
shiftValue = 0b0011111111111111;
}

if ( potValue >=882 && potValue <=945)
{
shiftValue = 0b0111111111111111;
}

if ( potValue >=945 && potValue <=1023)
{
shiftValue = 0b1111111111111111;
}

digitalWrite (latchPin, LOW);
shiftOut (dataPin, clockPin, MSBFIRST, highByte (shiftValue)); // upper 2 bits
shiftOut (dataPin, clockPin, MSBFIRST, lowByte (shiftValue)); // lower 8  bits
digitalWrite (latchPin, HIGH);

}

gavron04

Only. How Can I turn off first led after rotate potentiometer example 100 then I have first and second led turned on. When I turn back, always first led is blinking.

gavron04

Done.

Added

Code: [Select]

if ( potValue >=0 && potValue <=20)
{
shiftValue = 0b0000000000000000;
}

gavron04

Hello.

I have question about controlling brightness this leds.


How can I add photoresitor to control brightness? I need to set max. brightness when it is sunny and about 5% when it is night.


I have connected 16 leds. About 8mA per one led. All 128mA.


Regards,

Mateusz

Go Up