Individual brightness control with a 74HC595 shift register

Just getting started with Arduino, following Adafruit’s guides.
Breadboarding: Breadboard Layout | Arduino Lesson 8. Analog Inputs | Adafruit Learning System

/*
Adafruit Arduino - Lesson 8. Analog Inputs - LEDs
*/

int potPin = 0;
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;

int leds = 0;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
  int reading  = analogRead(potPin);
  int numLEDSLit = reading / 114;  //1023 / 9, number of LEDs to turn on
  double exBright = ((double)reading/114)-numLEDSLit; //decimal number of LEDs to turn on
  int eBright = (int)(exBright*255);//Conversion from decimal to int brightness
  leds = 0;
  int i;
  for (i = 0; i < numLEDSLit; i++)
  {
    bitSet(leds, i); 
  }
  analogWrite(i+1, eBright); //attempt to use int brightness for lighting up an LED partially
  updateShiftRegister();
}

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

TL;DR 8 LEDs and the shift register and a potentiometer, turning the knob brings a light on at a time. I’m trying to make it so turning the knob brings the brightness of the next one up instead of one light on or off at a time. Is this possible with the setup I have?

To vary the light, you need to use PWM. You cannot vary the voltage out in a direct analogue manner, ie, output a variable voltage at the actual pin.

PWM rapidly switches the output pin on and off. By setting the parameters you vary the ratio of on to off and this varies the brightness.

Weedpharma

A shift register only has digital outputs, so that you can turn attached LED either on or off. As already mentioned, PWM can be used to turn a LED continuously on for a short time, then off again, to achieve a variable brightness. A dedicated PWM output pin produces such a signal in hardware, when you specify with analogWrite a duty cycle between 0 (fully off) and 255 (fully on). But this works only with specific PWM pins.

With a shift register you have two choices. If the shift register has an output enable pin, that allows to disable all output pins at the same time, you can connect a PWM signal to that pin, in order to control the brightness of all attached and turned on LED, at the same time.

Otherwise you have to shift out the on and off patterns continuously, so that the on time of every LED reflects their individual brightness.

You need to use the ShiftPWM library
http://www.elcojacobs.com/shiftpwm/

const byte CLEAR = 8; const byte DATA = 11; const byte LATCH = 12; const byte CLOCK = 13; const byte ENABLE = 10;

void setup() { pinMode(CLEAR, OUTPUT); //Clear pin pinMode(DATA, OUTPUT); //Data pin pinMode(LATCH, OUTPUT); //latch pin pinMode(CLOCK, OUTPUT); //Clock pin pinMode(ENABLE, OUTPUT);

} void loop() {

blueled(); delay(2); redled(); delay(5); greenled(); delay(0.1); } void blueled() { digitalWrite(CLEAR, LOW); //Clear Register Value digitalWrite(CLEAR, HIGH); digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH);

shiftOut(DATA, CLOCK, MSBFIRST, 1); //BLUE Light Turn On digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH); } void greenled() { digitalWrite(CLEAR, LOW); //Clear Register Value digitalWrite(CLEAR, HIGH); digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH);

shiftOut(DATA, CLOCK, MSBFIRST, 2); //BLUE Light Turn On digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH);

} void redled() { digitalWrite(CLEAR, LOW); //Clear Register Value digitalWrite(CLEAR, HIGH); digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH);

shiftOut(DATA, CLOCK, MSBFIRST, 4); //BLUE Light Turn On digitalWrite(LATCH, LOW); digitalWrite(LATCH, HIGH);

}

use my code and

blueled(); delay(2); redled(); delay(5); greenled(); delay(0.1);

change these delay times range 0.1-5ms

  delay(0.1);You haven't tested this, have you?

Grumpy_Mike: You need to use the ShiftPWM library http://www.elcojacobs.com/shiftpwm/

That link doesn't work. Here's the GitHub repo: https://github.com/elcojacobs/ShiftPWM

Pieter

TolpuddleSartre:  delay(0.1);

You haven't tested this, have you?

Well clearly he has trouble understanding things, like not to resurrect a two year old thread.

And he can’t be arsed reading the how to use this forum sticky post because the code is not correctly posted.

Whoops, I didn't even notice ...