Go Down

Topic: Why isn't my LED fading? (Read 1 time) previous topic - next topic

Jxmorris12

I'm using a Shiftbrite. My first time writing code!   :) Any improvements? Also... why doesn't it work? I tried to copy the sample LED fade as best as I could.

Code: [Select]
int datapin  = 10; // DI
int latchpin = 11; // LI
int enablepin = 12; // EI
int clockpin = 13; // CI
unsigned long CommandPacket;
int Command;
int Blue = 1023; // Maximum Blue
int Red = 1023; // Maximmum Red
int Green = 1023; // Maximum Green
int Fade = 5;

void setup() {
   pinMode(datapin, OUTPUT);
   pinMode(latchpin, OUTPUT);
   pinMode(enablepin, OUTPUT);
   pinMode(clockpin, OUTPUT);

   digitalWrite(latchpin, LOW);
   digitalWrite(enablepin, LOW);
}

void SendPacket() {
   CommandPacket = Command & B11;
   CommandPacket = (CommandPacket << 10)  | (Blue & 1023);
   CommandPacket = (CommandPacket << 10)  | (Red & 1023);
   CommandPacket = (CommandPacket << 10)  | (Green & 1023);

   shiftOut(datapin, clockpin, MSBFIRST, CommandPacket >> 24);
   shiftOut(datapin, clockpin, MSBFIRST, CommandPacket >> 16);
   shiftOut(datapin, clockpin, MSBFIRST, CommandPacket >> 8);
   shiftOut(datapin, clockpin, MSBFIRST, CommandPacket);

   delay(1); // adjustment may be necessary depending on chain length
   digitalWrite(latchpin,HIGH); // latch data into registers
   delay(1); // adjustment may be necessary depending on chain length
   digitalWrite(latchpin,LOW);
}

void loop() {
   
   Command = B00;
   Blue = Blue - Fade;
   
   delay (10);
   
   Fade = Fade + 5;
   
   if (Blue = 0);
      Fade = -Fade;
 
}


Thanks!
Jack

pedroferrer

Hi

Where are you calling 'void SendPacket' !?

Best regards
Pedro Ferrer

Jxmorris12

Hi! Can you explain that a little bit more? Or maybe provide some sample code? I'm new to coding.. and electronics in general. Thanks :)

Jack

pedroferrer

Hi Jack

On loop() procedure, you should call SendPacket()

void loop(){
      SendPacket();
}

Best regards
Pedro Ferrer

Jxmorris12

Thanks, Pedro. It still isn't working for me. Did I put it in the wrong place?
Code: [Select]
int datapin  = 10; // DI
int latchpin = 11; // LI
int enablepin = 12; // EI
int clockpin = 13; // CI
unsigned long Packet;
int Command;
int Blue = 0; // Maximum Blue
int Red = 1023; // Maximmum Red
int Green = 1023; // Maximum Green
int Fade = 50;

void setup() {
   pinMode(datapin, OUTPUT);
   pinMode(latchpin, OUTPUT);
   pinMode(enablepin, OUTPUT);
   pinMode(clockpin, OUTPUT);

   digitalWrite(latchpin, LOW);
   digitalWrite(enablepin, LOW);
}

void SendPacket() {
   Packet = Command & B11;
   Packet = (Packet << 10)  | (Blue & 1023);
   Packet = (Packet << 10)  | (Red & 1023);
   Packet = (Packet << 10)  | (Green & 1023);

   shiftOut(datapin, clockpin, MSBFIRST, Packet >> 24);
   shiftOut(datapin, clockpin, MSBFIRST, Packet >> 16);
   shiftOut(datapin, clockpin, MSBFIRST, Packet >> 8);
   shiftOut(datapin, clockpin, MSBFIRST, Packet);

   delay(1); // adjustment may be necessary depending on chain length
   digitalWrite(latchpin,HIGH); // latch data into registers
   delay(1); // adjustment may be necessary depending on chain length
   digitalWrite(latchpin,LOW);
}

void loop() {
   SendPacket();
   
   Command = B00;
   Blue = Blue + Fade;
   
   Command = B00;
   if (Blue = 0);
      Fade = -Fade; 
     
   delay (10);
 
}


pedroferrer

Hi

Your 'Blue' variable has to be placed on
analogWrite(IO_Intended, Blue);

Eg: IO_Intended should be between 1 and 13 (D1, D2...) on Mega...

Best regards
Pedro Ferrer


Jxmorris12

I am so confused. I used the wrong output pins?

pedroferrer

Hi

analogWrite enables PWM signal... that way you can fade the leds...
digitalwrite is like ON/OFF... LOW/HIGH state...

Best regards
Pedro Ferrer

Jxmorris12

Oh! Got it, thanks! From my code, which pin do I want to set?

johnwasser

Your code is close.  Since your are using the 'shiftbrite', ignore the PWM suggestions.
Code: [Select]

void loop() {
   Command = B00;

   SendPacket();

   Blue = Blue - Fade;  // same as  Blue -= Fade;

    if (Blue < 1  || Blue > 1023)
       {
       Blue += Fade;  // Step back one
       Fade = -Fade;  // Reverse direction of fade
       }
       
   Fade = Fade + 5;  // Increase rate of fade, decrease rate of brightening.
   
    delay (10);
}

Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up