Arduino Forum

Using Arduino => Programming Questions => Topic started by: Jxmorris12 on Mar 31, 2011, 12:25 am

Title: Why isn't my LED fading?
Post by: Jxmorris12 on Mar 31, 2011, 12:25 am
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
Title: Re: Why isn't my LED fading?
Post by: pedroferrer on Mar 31, 2011, 12:34 am
Hi

Where are you calling 'void SendPacket' !?

Best regards
Pedro Ferrer
Title: Re: Why isn't my LED fading?
Post by: Jxmorris12 on Mar 31, 2011, 12:37 am
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
Title: Re: Why isn't my LED fading?
Post by: pedroferrer on Mar 31, 2011, 12:43 am
Hi Jack

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

void loop(){
      SendPacket();
}

Best regards
Pedro Ferrer
Title: Re: Why isn't my LED fading?
Post by: Jxmorris12 on Mar 31, 2011, 12:46 am
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);
 
}

Title: Re: Why isn't my LED fading?
Post by: pedroferrer on Mar 31, 2011, 12:56 am
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

Title: Re: Why isn't my LED fading?
Post by: Jxmorris12 on Mar 31, 2011, 01:00 am
I am so confused. I used the wrong output pins?
Title: Re: Why isn't my LED fading?
Post by: pedroferrer on Mar 31, 2011, 01:37 am
Hi

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

Best regards
Pedro Ferrer
Title: Re: Why isn't my LED fading?
Post by: Jxmorris12 on Mar 31, 2011, 02:35 am
Oh! Got it, thanks! From my code, which pin do I want to set?
Title: Re: Why isn't my LED fading?
Post by: johnwasser on Mar 31, 2011, 02:59 am
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);
}