Fading effect doesn't work

Hello! I need help to add a fade effect to all my LEDs when pressing button 9 from my infrared remote. I tried to code it but it doesn't work. The function for fading effect is named "void Fading()" and is the very last one from this code:

//www.plusivo.com
//
#include "IRremote.h"
#include "pitches.h"
#include "math.h"

int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
int tDelay = 200;
int latchPin = 11;      // (11) ST_CP [RCK] on 74HC595
int clockPin = 10;      // (10) SH_CP [SCK] on 74HC595
int dataPin = 12;     // (12) DS [S1] on 74HC595
int index = 0;
int buzzerPin = 13;
int brightness = 0;

float R;

const int pwmIntervals = 100;

byte leds = 0;

IRrecv irrecv(receiver);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
    
  case 0xFFA25D: Serial.println("CH-");  break;
  case 0xFFE21D: Serial.println("CH+"); break;
  case 0xFF629D: Serial.println("CH");  break;
  case 0xFF22DD: Serial.println("Play Pacman Song"); Pacman_Song(); break;
  case 0xFF02FD: Serial.println("Play Star Wars Theme"); Star_Wars_Song(); break;
  case 0xFFC23D: Serial.println("Play Christmas Song"); Play_XMas_Song();  break;
  case 0xFFE01F: Serial.println("VOL-");    break;
  case 0xFFA857: Serial.println("VOL+");    break;
  case 0xFF906F: Serial.println("EQ");    break;
  case 0xFF9867: Serial.println("100+");    break;
  case 0xFFB04F: Serial.println("200+");    break;
  case 0xFF6897: Serial.println("Button 0"); break;
  case 0xFF30CF: Serial.println("Button 1 - One After Another"); One_After_Another();  break;
  case 0xFF18E7: Serial.println("Button 2 - One At a Time"); One_At_a_Time();  break;
  case 0xFF7A85: Serial.println("Button 3 - Ping Pong"); Ping_Pong();  break;
  case 0xFF10EF: Serial.println("Button 4 - Random LED ON"); Random_LED_ON(); break;
  case 0xFF38C7: Serial.println("Button 5 - Marquee"); Marquee();  break;
  case 0xFF5AA5: Serial.println("Button 6 - Binary Count"); Binary_Count(); break;
  case 0xFF42BD: Serial.println("Button 7 - Zig Zag"); Zig_Zag(); break;
  case 0xFF4AB5: Serial.println("Button 8 - Rotation"); Rotation(); break;
  case 0xFF52AD: Serial.println("Button 9 - Fading"); Fading(); break;
  case 0xFFFFFFFF: Serial.println("REPEAT");break;  

  default: 
    Serial.println("other button");
  }// End Case
  delay(500); // Do not get immediate repeat
}

void setup() 
{
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode"); 
  irrecv.enableIRIn(); // Start the receiver
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);
  digitalWrite(latchPin, HIGH);
  R = (pwmIntervals*log10(2))/(log10(255));
}

void shiftWrite(int Pin, boolean State){
  bitWrite(leds,Pin,State);
  digitalWrite(latchPin, HIGH);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, LOW);
}

void loop() 
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    translateIR();
    irrecv.resume(); // receive the next value
  }
}

void Pacman_Song(){

  int melody[] = {
  NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5,
  NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_C5,
  NOTE_C6, NOTE_G6, NOTE_E6, NOTE_C6, NOTE_G6, NOTE_E6,
  
  NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_B5,
  NOTE_FS5, NOTE_DS5, NOTE_DS5, NOTE_E5, NOTE_F5,
  NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_B5
};

int durations[] = {
  16, 16, 16, 16,
  32, 16, 8, 16,
  16, 16, 16, 32, 16, 8,
  
  16, 16, 16, 16, 32,
  16, 8, 32, 32, 32,
  32, 32, 32, 32, 32, 16, 8
};

int size = sizeof(durations)/sizeof(int);
  for(int i=0;i<=1;i++){
    for(int note=0; note<size; note++){

    int duration = 1900/durations[note];
    tone(buzzerPin, melody[note], duration);

    int pauseBetweenNotes = duration*1.30;
    delay(pauseBetweenNotes);

    noTone(buzzerPin);
    }
  }

}

void Star_Wars_Song(){
  int melody[] = {
    NOTE_AS4, NOTE_AS4, NOTE_AS4,
    NOTE_F5, NOTE_C6,
    NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F6, NOTE_C6,
    NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F6, NOTE_C6,
    NOTE_AS5, NOTE_A5, NOTE_AS5, NOTE_G5, NOTE_C5, NOTE_C5, NOTE_C5,
    NOTE_F5, NOTE_C6,
    NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F6, NOTE_C6,

    NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F6, NOTE_C6,
    NOTE_AS5, NOTE_A5, NOTE_AS5, NOTE_G5, NOTE_C5, NOTE_C5,
    NOTE_D5, NOTE_D5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F5,
    NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_C5,
    NOTE_D5, NOTE_D5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F5,

    NOTE_C6, NOTE_G5, NOTE_G5, NOTE_G5, NOTE_C5,
    NOTE_D5, NOTE_D5, NOTE_AS5, NOTE_A5, NOTE_G5, NOTE_F5,
    NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, NOTE_D5, NOTE_E5, NOTE_C6, NOTE_C6,
    NOTE_F6, NOTE_DS6, NOTE_CS6, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_G5, NOTE_F5,
    NOTE_C6
  };

  int durations[] = {
    8, 8, 8,
    2, 2,
    8, 8, 8, 2, 4,
    8, 8, 8, 2, 4,
    8, 8, 8, 2, 8, 8, 8,
    2, 2,
    8, 8, 8, 2, 4,

    8, 8, 8, 2, 4,
    8, 8, 8, 2, 8, 16,
    4, 8, 8, 8, 8, 8,
    8, 8, 8, 4, 8, 4, 8, 16,
    4, 8, 8, 8, 8, 8,

    8, 16, 2, 8, 8,
    4, 8, 8, 8, 8, 8,
    8, 8, 8, 4, 8, 4, 8, 16,
    4, 8, 4, 8, 4, 8, 4, 8,
    1
  };

int size = sizeof(durations)/sizeof(int);
  for(int i=0;i<=1;i++){
    for(int note=0; note<size; note++){

    int duration = 1000/durations[note];
    tone(buzzerPin, melody[note], duration);

    int pauseBetweenNotes = duration*1.30;
    delay(pauseBetweenNotes);

    noTone(buzzerPin);
    }
  }
}

void Play_XMas_Song(){
  int melody[] = {
    NOTE_E5, NOTE_E5, NOTE_E5,
    NOTE_E5, NOTE_E5, NOTE_E5,
    NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
    NOTE_E5,
    NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
    NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
    NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
    NOTE_D5, NOTE_G5
  };

  int durations[] = {
    8, 8, 4,
    8, 8, 4,
    8, 8, 8, 8,
    2,
    8, 8, 8, 8,
    8, 8, 8, 16, 16,
    8, 8, 8, 8,
    4, 4
  };

  int size = sizeof(durations)/sizeof(int);
  for(int i=0;i<=1;i++){
    for(int note=0; note<size; note++){

    int duration = 1000/durations[note];
    tone(buzzerPin, melody[note], duration);

    int pauseBetweenNotes = duration*1.30;
    delay(pauseBetweenNotes);

    noTone(buzzerPin);
    }
  }
}

void One_After_Another(){
  for(int i=0;i<=4;i++){
    for(index=0; index<=7; index++){
      shiftWrite(index, HIGH);
      delay(tDelay);
    }
    for(index=7; index>=0; index--){
      shiftWrite(index, LOW);
      delay(tDelay);
    }
  }
}

void One_At_a_Time(){
  for(int i=0;i<=7;i++){
    for(index=0; index<=7; index ++){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
  }
}

void Ping_Pong(){
  for(int i=0; i<=4; i++){
    for(index=0; index<=7; index++){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
    for(index=7; index>=0; index--){
      shiftWrite(index, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
    }
  } 
}

void Random_LED_ON(){
  for(int i=0; i<=69; i++){
    index=random(8);
    shiftWrite(index, HIGH);
    delay(tDelay);
    shiftWrite(index, LOW);
  }
}

void Marquee(){
  for(int i=0; i<=4;i++){
    for(index=0; index<=3; index++){
      shiftWrite(index, HIGH);
      shiftWrite(index+4, HIGH);
      delay(tDelay);
      shiftWrite(index, LOW);
      shiftWrite(index+4, LOW);
    }
  }
}

void Binary_Count(){
  for(int i=0;i<=255;i++){
    int delayCount = 500;
    shiftOut(dataPin, clockPin, MSBFIRST, leds);
    digitalWrite(latchPin, HIGH);
    digitalWrite(latchPin, LOW);
    leds++;
    delay(delayCount);
  }
}

void Zig_Zag(){
  for(int i=0;i<4;i++){
    index=0;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=5;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=2;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=7;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=3;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=6;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=1;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=4;
    shiftWrite(index, HIGH);
    delay(tDelay);
    index=4;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=1;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=6;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=3;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=7;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=2;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=5;
    shiftWrite(index, LOW);
    delay(tDelay);
    index=0;
    shiftWrite(index, LOW);
    delay(tDelay);
  }
}

void Rotation(){
  for(int i=0;i<=4;i++){
  for(index=0;index<=3;index++){
    shiftWrite(index, HIGH);
    delay(tDelay);
     shiftWrite(index, LOW);
  }
  for(index=7;index>=4;index--){
    shiftWrite(index, HIGH);
    delay(tDelay);
    shiftWrite(index, LOW);
  }
  }
}

void Fading(){
  for(int interval=0; interval<=pwmIntervals; interval++){
    brightness=pow(2,(interval/R))-1;

    for(int i=0;i<=7;i++){
      analogWrite(i, brightness);
    }
    delay(30);
  }
}

That is a very poor description of the problem. For instance, does the code ever actually call the Fading() function ?

Does PWM work through a mux?
Did you press #9?

The Fading() function will place decreasing (?) PWM values on pins 0 to 7, or at least the ones that aren't in pin mode OUTPUT and are capable of PWM.

On an UNO, that's pins 3, 5 and 6.

It will do nothing and has nothing to do with any LEDs you may have connected the the shift register.

I suggest you take a moment or two and figure out how your code works. Dimming is not impossible and dimming all LEDs equally might even be easy.

Please post a schematic showing how you have hooked up the shift register and how you are driving the LEDs it controls.

a7

Yes. It does call it.

Unfortunately I don't have a schematic for my circuit. The only thing I can do is to take a picture of it.

I am using the shift register SN74HC595N. I pressed #9 and it shows the message I wrote in the code in serial monitor.

Does it enter Fading()? Put a Serial.print(":)") as the first line of Fading() just to verify the function is entered when #9 is pressed.

So I put Serial.print("I am active" )to check if the function does active when I press #9 and the message does show up when I press it.

for(int i=0;i<=7;i++){
      analogWrite(i, brightness);
    }

As previously pointed out, you are using analogWrite() on several pins that are not PWM capable

May you show me how to change the function so the LEDs fade, please? I just started to learn how to learn to code in arduino 3 days ago.

Start by connecting the LEDs to PWM capable pins and tell us which pins you are using

Shouldn't I use my shift register SN74HC595 and connect it to my PWM capable? All LEDs are already connected to SN74HC595.

Sorry, I had not even realised that you were using a shift register. Reading back through the topic I see that you have been asked to provide details of how the shift register is connected to the Arduino

Please provide those details

Okay. latchPin is conected to pin 11, clockPin to pin 10, dataPin to pin 9 ( receiver is connected to pin 6 and buzzerPin to pin 13). I changed the connection to pins now in case you look at my code and see different connections.
After taking a look I saw that the only DIGITAL PWM on my microcontroller are the pins 3, 5, 6, 9, 10, and 11.

Fading 8 LEDs that are controlled by a shift register is going to be more complicated than merely using analogWrite().

What might work would be to turn on all of the LEDs, wait a while, turn off all the LEDs, wait a while then repeat

If you adjust the periods that you wait with the LEDs on and off then they will appear to fade on or off

1 Like

I try this now. Thank you!

I can turn them on but i can't turn them off. :sweat_smile:

void Fading(){
  shiftOut(dataPin, clockPin, MSBFIRST, 255);
    digitalWrite(latchPin, HIGH);
    delay(1000);
  shiftOut(dataPin, clockPin, LSBFIRST, 0);
    digitalWrite(latchPin, LOW);
    delay(1000);
}

To get a fading effect you need to change the ratio of the time that they are on and the time that they are off and the period must be much shorter that 1 second.

You are, in effect, writing your own analogWrite() function

1 Like

Yes. This is what I was hinting at in #4 above.

A suitable signal might be used to tri-state the output buffers periodically, resulting in an across the board dimming of all LEDs.

@absol125, how is the shift register driving the LEDs?

And where did you get your inspiration for this circuit and code? You seem to have copy/pasted the entirety of thjs project, you'll need to learn some code to modify it or forever rely on the kindness of strangers.

Which I do not consider myself now that we know each other so well and stuff.

a7