Digital Addressable Led Strip

Hi,

I use the code from here to control a digital addressable led strip:
http://interface.khm.de/index.php/news/digital-addressable-led-strip-arduino/

Works fine on an Arduino Uno and Arduino Pro.
As I need some more analog pins I want to switch to an Arduino Mega 2560.
It seems the that are problems regarding the "PORTD" command see below.
Any ideas where to make changes in code for the Arduino Mega?
Thanks!!!

//*********************************************************
// send color values from pixel array to led strip
void show() {
  unsigned int ii,b1;
  for (ii=0; ii < nled; ii++ ) {

    bitWrite(PORTD,datapin,HIGH);
    bitWrite(PORTD,clockpin, HIGH);
    bitWrite(PORTD,clockpin, LOW);

    for ( b1=0x4000; b1; b1 >>= 1) {

      if(rgbPixel[ii] & b1) bitWrite(PORTD,datapin, HIGH);
      else                bitWrite(PORTD,datapin, LOW);
      bitWrite(PORTD,clockpin, HIGH);
      bitWrite(PORTD,clockpin, LOW);
    }
  }
  latchLeds(nled);
}
//*********************************************************
// activate new color pattern in ledstrip
void latchLeds(int n) {

  bitWrite(PORTD,datapin, LOW);
  for(int i = 8 * n; i>0; i--) {
    bitWrite(PORTD,clockpin, HIGH);
    bitWrite(PORTD,clockpin, LOW);
  }

}

The point is that a Mega does not have the same port / pin number bit assignment as the smaller Arduinos so you need to either change the code or change the pins you have wired things to.
This thread will explain what is happening:-
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1266009870

thanks for the quick respons.
to be honest I don't understand everything in detail.

I found this link:
http://docs.macetech.com/doku.php/non-hardware_spi_example

is it "PORTE" instead of "PORTD"?
I replaced it in the code, but it's still not working.

Thanks!

is it "PORTE" instead of "PORTD"?

No.
You might be able to use PORTE but then you have to change the wiring to reflect the new pins that it maps to.

Direct port mapping like this is processor specific so you need to know how it is wired up to the original port and then see what pin numbers this corresponds to on your Mega, then wire your strip up to those pins.

To whom it may concern.
I just solved the problem by using the "digitalwritefast" library:
http://code.google.com/p/digitalwritefast/

This way I am able to use "digitalWriteFast(datapin,HIGH);" instead of "bitWrite(PORTD,datapin,HIGH);".
Works fine now on the Mega too.

I just solved the problem by using the "digitalwritefast" library:

Sigh :~
The library despite its name is a lot slower than direct port access.
It saddens me to think you couldn't make the effort to change the pins.

I see.
I had a look on this diagramm of the Mega:

Arduino Mega Pin Port Pin
13.................PORTB.......7
12.................PORTB.......6
11.................PORTB.......5
10.................PORTB.......4
9..................PORTH.......6
8..................PORTH.......5
7..................PORTH.......4
6..................PORTH.......3
5..................PORTE.......3
4..................PORTG.......5
3..................PORTE.......5
2..................PORTE.......4
1 (TX).............PORTE.......1
0 (RX).............PORTE.......0
A7 (Analog)........PORTF.......7
A6 (Analog)........PORTF.......6
A5 (Analog)........PORTF.......5
A4 (Analog)........PORTF.......4
A3 (Analog)........PORTF.......3
A2 (Analog)........PORTF.......2
A1 (Analog)........PORTF.......1
A0 (Analog)........PORTF.......0

To write something superfast on pin 2 I should be able by using "bitWrite(PORTE,2,HIGH);" right?
But this didn't worked.
Any idea?

Thanks!

To write something superfast on pin 2 I should be able by using "bitWrite(PORTE,2,HIGH);" right?

No the bit write is slow as well, you should use:-

PORTE |= 0x80;

Will set bit 7 on PORTE without affecting any of the other bits. Similarly to clear bit 7 use:-
PORTE &= ~0x80;
That symbol in front of the 0x80 is a tilde and means the inverse bit pattern of the number.

Sorry for being kind of stupid :wink:
Can you give me a hint where to make changes in the code snippet for the Mega:

void show() {
  unsigned int ii,b1;
  for (ii=0; ii < nled; ii++ ) {

    digitalWriteFast(datapin,HIGH);
    digitalWriteFast(clockpin, HIGH);
    digitalWriteFast(clockpin, LOW);

    for ( b1=0x4000; b1; b1 >>= 1) {

      if(rgbPixel[ii] & b1) digitalWriteFast(datapin, HIGH);
      else                digitalWriteFast(datapin, LOW);
      digitalWriteFast(clockpin, HIGH);
      digitalWriteFast(clockpin, LOW);
    }
  }
  latchLeds(nled);
}

Many, many thanks!

Sorry forgot to mention the pin settings:

int datapin = 2;  // Led Strip St Pin
int clockpin = 3; // Led Strip Ci Pin

Hey Grumpy_Mike,

you are my hero and thanks for the hints!
Finally I got it working with the help of this diagramm:
https://spreadsheets.google.com/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0

And here is the working code:

void show() {
  unsigned int ii,b1;
  for (ii=0; ii < nled; ii++ ) {

    //digitalWriteFast(datapin,HIGH);
    PORTE |= 0x10;
    PORTE |= 0x20;
    PORTE &= ~0x20;

    for ( b1=0x4000; b1; b1 >>= 1) {

      //if(rgbPixel[ii] & b1) digitalWriteFast(datapin, HIGH);
      if(rgbPixel[ii] & b1) PORTE |= 0x10;
      else                PORTE &= ~0x10;
      PORTE |= 0x20;
      PORTE &= ~0x20;
    }
  }
  latchLeds(nled);
}