APA102C LED Strip Control

Hi all, I’ve recently bought an Arduino Mega 2560 and some RGB APA102C LED Strips.

I started off using FastLED which was fine, but I wanted to work with the ports myself to maximise speed and minimise code. And just because I fancied a play to be honest.

I’ve written a simple test below which seems to just stop working and wondered if you helpful peeps might be able to see what’s wrong.

The expected behaviour is as follows…

  1. LED on arduino comes on.
  2. Strip turns blue 1 LED at a time until all LEDs are lit.
  3. LED on arduino goes off.
  4. Strip turns red 1 LED at a time until all leds are lit red.

Whereas what actually happens is steps 1 and 2 complete just fine, and then steps 3 & 4 never happen at all.

Any ideas please. :slight_smile:

#define DATA_PIN     12 //Port B Pin 6
#define CLK_PIN      11 //Port B Pin 5
#define NUM_LEDS     288

int cnt;
long leds[NUM_LEDS]; //byte Global Brightness, byte Blue, byte Green, byte Red

void setup() {  

    //Set Pins 11, 12, 13 as output
    DDRB |= 0xE0;
    //Turn On board LED
    PORTB |= 0x80;    
    
    //Send Start Bits to Strip
    ShiftLong(0x0L);
    
    //Loop thru all LEDs
    for (cnt = 0; cnt < NUM_LEDS; cnt++){
        //temporary delay to see things happening
        delay(10);
        //Set LEDs to half brightness(ish) blue
        leds[cnt] = 0xff800000;
        //Shift Out LED data
        ShiftLong(leds[cnt]); 
    }
    
    //Turn Off Board LED
    PORTB &= ~0x80;
    
    //Send End Bits to strip.
    ShiftLong(0xffffffff);
    
}

void loop()
{     
  
    //Loop thru all LEDs
    for (cnt = 0; cnt < NUM_LEDS; cnt++){
        //temporary delay to see things happening
        delay(10);
        //Set LEDs to half brightness(ish) red
        leds[cnt] = 0xff000080;
        //Shift Out LED data
        ShiftLong(leds[cnt]); 
    }
    
}

void ShiftLong(long Data){
    
    //Count down to zero for speed
    for (cnt = 32; cnt != 0; cnt--){      
        
        //Send MSB First
        if (Data & 0x80000000){            
            PORTB |= 0x40;  
        } else {
            PORTB &= ~0x40; 
        }
        
        //Shift data
        Data = (Data << 1);
        
        //Pulse Clock Line
        PORTB |= 0x20;
        PORTB &= ~0x20;         
             
    }
    
}