Shift register 595 fade in effect and multiple PWMs

Hi, I think i have burnt enough night oils to ask question on this forum (also my family is now getting irritated with my craze :P) . I will be glad tp have any help or direction as i am going crazy. Pardon me if i sound strange, headbanging for 3 days (and nights).

My project and Dilemma

I have wired 4 X 74HC595, 3 of which are for RGB leds and 1 for common purposes (thermostat fan, night light and casing RGB light).

*(No i cannot use TLC5940 due to availability in my contrary and due to perks of living in 3rd world and not able to order online either :smiley: LIFE!)

  • I tried using libraries, but I am unable to convert them as per my need (perhaps lack of my coding skills - i have no background in coding - learning ) I can make single leds work in my way, but its the PWM thats killing me (because i want to mix colors)

My vision

  1. I want to make RGB led chaser with controllable PWM duty cycle and custom colors
  2. 10W Led nigh light led with different frequency (that i can set as per taste i.e. reducing flickering) (yes power transistor are there to manage the load)
  3. DC fan very low frequency (with a capacitor wired parallel to load to reduce noise (done this with 555 circuit so i know atleast this works)

Problem

After joining bits from around the internet i am able to make this, where a class PWM is called for each led I want to do something with.

Q1. Internet says i have to tell all registers in one go what to do or they will mess things, so i call them all at once, those which i dont need turned on I told them to lit pin99 (since on pin 99 , nothing happens - bad coding, but worked :stuck_out_tongue: )

Q2. I was able to turn leds with pwm, but they start suddenly instead of fading in (once they are on, they do fade in out) but first attempt is always without fade in

//****************** 74HC595 FOR LED *********************//
//Define which pins will be used for the shift register control can be any digital pin on the Arduino
int latchPin = 2;  //Pin connected to ST_CP(pin 12) of 74HC595
int dataPin = 3;  //Pin connected to DS(pin 14) of 74HC595
int clockPin = 4; //Pin connected to SH_CP(pin 11) of 74HC595

int shiftregisterBlue;
int shiftregisterGreen;
int shiftregisterRed;
int shiftregisterGP;
void PushOut();



class PWM {
    byte ledstate;
    unsigned long previoustime;
    unsigned long previousfadetime;
    int ontime;
    int offtime;
    int TPmath;  // just divides for calculation
    byte up;

  public:
    byte ledpinBlue;
    byte ledpinGreen;
    byte ledpinRed;
    byte ledpinGP;
    int timeperiod;
    byte dutycycle = 0;
    byte frequency;
    byte i = 0;
    byte myspeed;
    byte mylimit;
    byte resetto;
    byte fadeout;


    PWM (byte freq) {
      frequency = freq;
      ledstate = LOW;
      previoustime = 0;
    }

    void Update() {
      unsigned long currenttime = micros();  // start microsecond clock

      // Formula for PWM
      timeperiod = 1000000 / frequency ; //20,000
      TPmath = timeperiod / 100;
      ontime = dutycycle * TPmath;
      offtime = timeperiod - ontime  ;

      if ((currenttime - previoustime >= ontime) && (ledstate == HIGH)) {
        ledstate = LOW;
        bitClear(shiftregisterBlue, ledpinBlue);
        bitClear(shiftregisterGreen, ledpinGreen);
        bitClear(shiftregisterRed, ledpinRed);
        bitClear(shiftregisterGP, ledpinGP);
        PushOut();
        previoustime = currenttime;
      }

      else if ((currenttime - previoustime >= offtime) && (ledstate == LOW)) {
        ledstate = HIGH;
        bitSet(shiftregisterBlue, ledpinBlue);
        bitSet(shiftregisterGreen, ledpinGreen);
        bitSet(shiftregisterRed, ledpinRed);
        bitSet(shiftregisterGP, ledpinGP);
        PushOut();
        previoustime = currenttime;
      }

      //********************** fade in or not function *********************//

      if (currenttime - previousfadetime >= 20000)  {
        previousfadetime = currenttime;

        // Fade up and down
        if (up == true && i <= mylimit)
        {
          i = i + myspeed; // increase duty cycle
          if (i == mylimit) {
            up = false;
          }
        }

        else {
          if ( fadeout >= 1) {  // if we decide to fadeout
            up = false;
            i = i - myspeed; // decrease duty cycle to fadeout

            if (i == resetto)
            {
              up = true;
            }
          }

          else if ( fadeout >= 0) { // if we don’t want fade-out only fade-in
            up = false;
            i = mylimit;
          }
          else {
            up = true;
            i = resetto;
          }
        }
        // End - Fade up and down

      } // end if fade in

      //********************** fade in or not function *********************//

    } // end void update


}; // class flasher end

// PWM (int pin, int freq) 

PWM Blue (64);
PWM Green (64);
PWM Red (64);
PWM LCDBacklit (64);



//*************************************

void LCDbacklight () { // default color of Casing Leds
  // send data only when you receive data:
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    noAscii = incomingByte - 48;
    Serial.print("I received: ");
    Serial.println(noAscii , DEC);
    if (noAscii == 1) {
      LCDBacklit.ledpinGP = 5;
      LCDBacklit.dutycycle = 50;
      //LCDBacklit.dutycycle = LCDBacklit.i;
      
    }
    else {
      bitClear(shiftregisterGP, 5);
      LCDBacklit.ledpinGP = 99;
    }
  }


  LCDBacklit.fadeout = 0; // 0 for Stay, 1 for fadeout
  LCDBacklit.ledpinRed = 99; // since there is no 9 pin , 9 == no led lights up
  LCDBacklit.ledpinGreen = 99;
  LCDBacklit.ledpinBlue = 99;
  LCDBacklit.myspeed = 1;
  LCDBacklit.mylimit = 80;
  LCDBacklit.resetto = 0;
  LCDBacklit.Update();
}

//*************************************

void PushOut() {
  digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, shiftregisterBlue);
  shiftOut(dataPin, clockPin, MSBFIRST, shiftregisterGreen);
  shiftOut(dataPin, clockPin, MSBFIRST, shiftregisterRed);
  shiftOut(dataPin, clockPin, MSBFIRST, shiftregisterGP);
  digitalWrite(latchPin, HIGH);
}

circuit for understanding my layout

Additional. I am not using SPI since, using those pins for LCD as shown in circuit above. ( can we use more than one circuits on SPI pins ? )

anyone ?

Your circuit is too fuzzy to read.

pdf attached

command_center.PDF (136 KB)

Hi all,

I figured it out. The Shiftpwm worked on pwm pins (trying it on non pwm pins)

meet Sparrow. my CPU fan controller / Digital Thermometer / Dancing leds / humidity sensor.

Thankyou again to everyone for helping everybody.