Help me with <Shifty.h>

Hi, I'm making a project with the Shift register. What I want to make is that the LED turns off slowly. This code includes <Shifty.h>

Shifty shift;
void setup() {
  // put your setup code here, to run once:
  shift.setBitCount(32);
  shift.setPins(11, 12, 8);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=0;i<24;i++){
    shift.writeBit(i,HIGH);
    delay(50);
    shift.writeBit(i-3,LOW);//turn off led
  }
}

I want to make a code under 'delay(50);' that allows led to turn off slowly. I don't know how to make it.

When I is 0,1 or 2 how much is i-3 worth?

What do you call turn off slowly ? Dimming the led? Explain what your circuit is a schéma goes a long way

To fade an LED, it must be connected to a PWM pin. Try to fit this sketch to fade-out an LED into your sketch...

( also found here: IDE >> FILE >> EXAMPLES >> BUILT-IN >> BASIC >> FADE )

I use 21 LEDs. What I want to make is to fade the led. 'shift.writeBit(i-3,LOW);' just turns off, but I want to make it turn off faintly. It's simple to use only 1 led, but I was using the shift register because I use 21 led. Must use to use the shift register.h> Library. What I want to know is how to fix the 'shift.writeBit(i-3,LOW);' in shift library to fade led.

The TPIC6B595 is a shift register that can be used for leds: https://www.adafruit.com/product/457

Do you want to fade out a single led or all 21 leds ?

Suppose that you want to fade out a single led. At a rate of 50Hz, the human eye does not see much flickering. That is a delay of 20ms between each 'on' and 'off' for the led.
Then you have to create your own PWM signal. For that, you have to increase the 50Hz update rate. You also have to know how many brightness level that you want.

The will be no "shift.writeBit(i-3,LOW);" in the resulting code. I don't even know what that is about.

I think that I have seen libraries for software PWM via a shift register.

Is this an assignment for school ?

there is no such thing really as fading a LED unless you control the current that goes through it - which you won't do with your shift register and a current limiting resistor in series with the LED.

What we do to "fade" the led is make it blink fast enough so that the eye still has the perception of a continuous light emission due to persistence of vision and for that what is commonly used is PWM.

if you have a circuit (which we are still waiting to see) with shift registers then you need to simulate that blink fast enough to achieve the same effect.

i-3 when i is less than 3 will lead to a negative number... have you checked what writeBit() does with an out of range index ?

EDIT: basically the same thing that @Koepel said :slight_smile: , sorry did not see it

yeah..it's a school assignment. You suggest me TPIC6B595 but I have 74HC595 :smiling_face_with_tear:

remember that the 74HC595 must not provide more than 70mA in total. Hope you have calculated the current limiting resistor for your LEDs accordingly

the "persistence of vision" approach will still work with the 74HC595.. you just need to blink the leds. Say you take a 20ms period, if the LED is on 20ms out of those 20 then the LED is full bright. if the LED is on 0ms out of those 20 then the LED is off. but if you vary how long the LED stays on during this period, say 10ms then the LED will be perceived as half bright (not totally because of some color perception effects) but that's the idea.

So if you want to fade your LED say over 5s in 10 steps, each step will take 500ms so 25 cycles of 20ms.

cycle 0 = power on duration = 20ms, power off duration = 0ms (full bright)
cycle 1 = power on duration = 18ms, power off duration = 2ms
cycle 2 = power on duration = 16ms, power off duration = 4ms
cycle 3 = power on duration = 14ms, power off duration = 6ms
...
cycle 9 = power on duration = 2ms, power off duration = 18ms

and then you hit
cycle 10 = power on duration = 0ms, power off duration = 20ms ➜ LED OFF

you get the idea

Without schematic, I made something that might not be the same as your circuit:

The Shifty library works, but I have not PWM code yet: Test for Shifty Library - Wokwi ESP32, STM32, Arduino Simulator

I had to add a translation from the led pin to the index of the row of leds.

// Test for Shifty Library
//
// This Wokwi project: https://wokwi.com/projects/367308421388466177
// Shifty library: https://github.com/johnnyb/Shifty
// Forum: https://forum.arduino.cc/t/help-me-with-shifty-h/1137022
// Circuit based on circuit by arcostasi 
//     https://wokwi.com/projects/301213976182653448
//
// WARNING: This has no PWM yet

#include <Shifty.h>

// The order of the leds after the shift register
// Led 0 (index 0) will be the most right led
const int ledorder[24] = 
{
  7 , 6, 5, 4, 3, 2, 1, 0,
  15,14,13,12,11,10, 9, 8,
  23,22,21,20,19,18,17,16,
};

// Declare the shift register
Shifty shift;

int walkingled = 0;
int direction = 1;
int lastwalkingled = 0;

void setup() {
  // Set the number of bits you have (multiples of 8)
  shift.setBitCount(24);

  // Set the data, clock, and latch pins you are using
  // This also sets the pinMode for these pins
  shift.setPins(11, 12, 8);    // data, clock, latch
}

void loop() 
{
  shift.writeBit(ledorder[lastwalkingled], LOW);
  shift.writeBit(ledorder[walkingled], HIGH);
  lastwalkingled = walkingled;

  walkingled += direction;
  if(walkingled == 0 or walkingled == 23)
    direction = -direction;

  delay(200);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.