Show Posts
Pages: [1] 2 3 ... 5
1  Using Arduino / Programming Questions / Re: porting perlin noise Java example doesn't work on: December 16, 2013, 06:38:03 am
Great, that works! I did it already with the upper integers, but overlooked this one.

Thanks.
2  Using Arduino / Programming Questions / Re: porting perlin noise Java example doesn't work on: December 16, 2013, 04:50:28 am
They are constants and in the end I will place them in Flash memory (I've already made a version that does that). But in order to compare good and create a minimal working version I've tried to make to versions that are equal in Processing/Arduino.
3  Using Arduino / Programming Questions / Re: porting perlin noise Java example doesn't work on: December 16, 2013, 03:25:32 am
Hi Peter,
The code I've posted is my standard test case and in the screenshots I've showed the trace (and the difference). But I have no clue what goes wrong, probably something with different interpretation of datatypes?
4  Using Arduino / Programming Questions / Re: porting perlin noise Java example doesn't work on: December 15, 2013, 05:10:12 pm
The Arduino code (for what it's worth, since I hardly modified anything as you can read in my first post):

Code:
long p[] = {
   151,160,137,91,90,15,
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  
   151,160,137,91,90,15,
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
};

long fadeArray[] = {
0,    0,    0,    0,    0,    0,    0,    0,    1,    1,    2,    3,    3,    4,    6,    7,
9,    10,   12,   14,   17,   19,   22,   25,   29,   32,   36,   40,   45,   49,   54,   60,
65,   71,   77,   84,   91,   98,   105,  113,  121,  130,  139,  148,  158,  167,  178,  188,
199,  211,  222,  234,  247,  259,  273,  286,  300,  314,  329,  344,  359,  374,  390,  407,
424,  441,  458,  476,  494,  512,  531,  550,  570,  589,  609,  630,  651,  672,  693,  715,
737,  759,  782,  805,  828,  851,  875,  899,  923,  948,  973,  998,  1023, 1049, 1074, 1100,
1127, 1153, 1180, 1207, 1234, 1261, 1289, 1316, 1344, 1372, 1400, 1429, 1457, 1486, 1515, 1543,
1572, 1602, 1631, 1660, 1690, 1719, 1749, 1778, 1808, 1838, 1868, 1898, 1928, 1958, 1988, 2018,
2048, 2077, 2107, 2137, 2167, 2197, 2227, 2257, 2287, 2317, 2346, 2376, 2405, 2435, 2464, 2493,
2523, 2552, 2580, 2609, 2638, 2666, 2695, 2723, 2751, 2779, 2806, 2834, 2861, 2888, 2915, 2942,
2968, 2995, 3021, 3046, 3072, 3097, 3122, 3147, 3172, 3196, 3220, 3244, 3267, 3290, 3313, 3336,
3358, 3380, 3402, 3423, 3444, 3465, 3486, 3506, 3525, 3545, 3564, 3583, 3601, 3619, 3637, 3654,
3672, 3688, 3705, 3721, 3736, 3751, 3766, 3781, 3795, 3809, 3822, 3836, 3848, 3861, 3873, 3884,
3896, 3907, 3917, 3928, 3937, 3947, 3956, 3965, 3974, 3982, 3990, 3997, 4004, 4011, 4018, 4024,
4030, 4035, 4041, 4046, 4050, 4055, 4059, 4063, 4066, 4070, 4073, 4076, 4078, 4081, 4083, 4085,
4086, 4088, 4089, 4091, 4092, 4092, 4093, 4094, 4094, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
};

int amountOfLights = 20;

long perlinTimeInc = 1000L;
long perlinXInc    = 1000L;
long perlinTimePosition = 0;

void setup()
{   Serial.begin(57600);
}

void loop()
{  
    for(int i=0;i<amountOfLights;i++)
    { int val = renderNoise(i*perlinXInc, 0, perlinTimePosition);
      
      if(val>127) Serial.print("*");
      else        Serial.print("-");
    }
    
    // go a step further in time (input for y function in perlin noise)
    perlinTimePosition = perlinTimePosition + perlinTimeInc;  
    
    Serial.println();
    
    delay(50);
}

int renderNoise(long x, long y, long z)
{ return 137 + (inoise(x, y, z)/512);
}

long inoise(long x, long y, long z)
{
  long X = x>>16 & 255, Y = y>>16 & 255, Z = z>>16 & 255, N = 1<<16;
  x &= N-1; y &= N-1; z &= N-1;
 
  long u=fade(x),v=fade(y),w=fade(z), A=p[X  ]+Y, AA=p[A]+Z, AB=p[A+1]+Z,
                                     B=p[X+1]+Y, BA=p[B]+Z, BB=p[B+1]+Z;
  return lerp(w, lerp(v, lerp(u, grad(p[AA  ], x   , y   , z   ),  
                                 grad(p[BA  ], x-N , y   , z   )),
                         lerp(u, grad(p[AB  ], x   , y-N , z   ),  
                                 grad(p[BB  ], x-N , y-N , z   ))),
                 lerp(v, lerp(u, grad(p[AA+1], x   , y   , z-N ),  
                                 grad(p[BA+1], x-N , y   , z-N )),
                         lerp(u, grad(p[AB+1], x   , y-N , z-N ),
                                 grad(p[BB+1], x-N , y-N , z-N ))));
}
  
long lerp(long t, long a, long b) { return a + (t * (b - a) >> 12); }
 
long grad(long hash, long x, long y, long z)
{ long h = hash&15, u = h<8?x:y, v = h<4?y:h==12||h==14?x:z;
  return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
 
long fade(long t) {
  long t0 = fadeArray[t >> 8], t1 = fadeArray[min(255, (t >> 8) + 1)];
  return t0 + ( (t & 255) * (t1 - t0) >> 8 );
}
5  Using Arduino / Programming Questions / porting perlin noise Java example doesn't work on: December 15, 2013, 05:08:57 pm
I'm trying to use an fixed point implementation of Perlin Improved noise in Arduino.
http://mrl.nyu.edu/~perlin/noise/INoise.java

I've already simplified some part and made an Arduino simulator in Processing. In the original code (link above) the arrays are generated on setup, but I put the arrays already in the code . This is modification tested and works in Processing.

I would like to port the Processing to Arduino. Steps made:
- converted all the int's to long, since Java ints are 4 bytes and Arduino ints 2 bytes (but longs are 4 bytes).
- Replaced the Math.min function with the min function in Arduino.

I run the code on an Arduino Mega1280 (since the UNO has not enough memory). I will optimise later, but I first want to have a setup with the less changes possible working.

In Arduino and don't seem to get the right result.
Do I miss something when porting this code to Arduino?


In both my setups I print a result off the noise to the monitor (Serial.print and print).

The Processing code (copy and past this in Processing 2.0):

Code:
// this version works, but porting this to Arduino (in this case the Mega because of memory) not.

int p[] = {
   151,160,137,91,90,15,
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  
   151,160,137,91,90,15,
   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};

int fadeArray[] = {
0,    0,    0,    0,    0,    0,    0,    0,    1,    1,    2,    3,    3,    4,    6,    7,
9,    10,   12,   14,   17,   19,   22,   25,   29,   32,   36,   40,   45,   49,   54,   60,
65,   71,   77,   84,   91,   98,   105,  113,  121,  130,  139,  148,  158,  167,  178,  188,
199,  211,  222,  234,  247,  259,  273,  286,  300,  314,  329,  344,  359,  374,  390,  407,
424,  441,  458,  476,  494,  512,  531,  550,  570,  589,  609,  630,  651,  672,  693,  715,
737,  759,  782,  805,  828,  851,  875,  899,  923,  948,  973,  998,  1023, 1049, 1074, 1100,
1127, 1153, 1180, 1207, 1234, 1261, 1289, 1316, 1344, 1372, 1400, 1429, 1457, 1486, 1515, 1543,
1572, 1602, 1631, 1660, 1690, 1719, 1749, 1778, 1808, 1838, 1868, 1898, 1928, 1958, 1988, 2018,
2048, 2077, 2107, 2137, 2167, 2197, 2227, 2257, 2287, 2317, 2346, 2376, 2405, 2435, 2464, 2493,
2523, 2552, 2580, 2609, 2638, 2666, 2695, 2723, 2751, 2779, 2806, 2834, 2861, 2888, 2915, 2942,
2968, 2995, 3021, 3046, 3072, 3097, 3122, 3147, 3172, 3196, 3220, 3244, 3267, 3290, 3313, 3336,
3358, 3380, 3402, 3423, 3444, 3465, 3486, 3506, 3525, 3545, 3564, 3583, 3601, 3619, 3637, 3654,
3672, 3688, 3705, 3721, 3736, 3751, 3766, 3781, 3795, 3809, 3822, 3836, 3848, 3861, 3873, 3884,
3896, 3907, 3917, 3928, 3937, 3947, 3956, 3965, 3974, 3982, 3990, 3997, 4004, 4011, 4018, 4024,
4030, 4035, 4041, 4046, 4050, 4055, 4059, 4063, 4066, 4070, 4073, 4076, 4078, 4081, 4083, 4085,
4086, 4088, 4089, 4091, 4092, 4092, 4093, 4094, 4094, 4095, 4095, 4095, 4095, 4095, 4095, 4095,
};

int amountOfLights = 20;

int perlinTimeInc = 1000;
int perlinXInc    = 1000;
int perlinTimePosition = 0;

void setup()
{ size(400,400);
  frameRate(25);
}

void draw()
{  
    for(int i=0;i<amountOfLights;i++)
    { int val = renderNoise(i*perlinXInc, 0, perlinTimePosition);
      
      if(val>127) print("*");
      else        print("-");
    }  
    
    // go a step further in time (input for y function in perlin noise)
    perlinTimePosition = perlinTimePosition + perlinTimeInc;  
 
    println();
}

int renderNoise(int x, int y, int z)
{ return 137 + (inoise(x, y, z)/512);
}

int inoise(int x, int y, int z) {
 
  int X = x>>16 & 255, Y = y>>16 & 255, Z = z>>16 & 255, N = 1<<16;
  x &= N-1; y &= N-1; z &= N-1;
 
  int u=fade(x),v=fade(y),w=fade(z), A=p[X  ]+Y, AA=p[A]+Z, AB=p[A+1]+Z,
                                     B=p[X+1]+Y, BA=p[B]+Z, BB=p[B+1]+Z;
  return lerp(w, lerp(v, lerp(u, grad(p[AA  ], x   , y   , z   ),  
                                 grad(p[BA  ], x-N , y   , z   )),
                         lerp(u, grad(p[AB  ], x   , y-N , z   ),  
                                 grad(p[BB  ], x-N , y-N , z   ))),
                 lerp(v, lerp(u, grad(p[AA+1], x   , y   , z-N ),  
                                 grad(p[BA+1], x-N , y   , z-N )),
                         lerp(u, grad(p[AB+1], x   , y-N , z-N ),
                                 grad(p[BB+1], x-N , y-N , z-N ))));
}
 
int lerp(int t, int a, int b) { return a + (t * (b - a) >> 12); }
 
int grad(int hash, int x, int y, int z) {
  int h = hash&15, u = h<8?x:y, v = h<4?y:h==12||h==14?x:z;
  return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
}
 
int fade(int t) {
  int t0 = fadeArray[t >> 8], t1 = fadeArray[Math.min(255, (t >> 8) + 1)];
  return t0 + ( (t & 255) * (t1 - t0) >> 8 );
}

The Arduino code (in a reply, because message length is exceeded).

As attachment 2 screenshots with of the monitor (to see the smooth "perlin" movement in Processing).
6  Using Arduino / Networking, Protocols, and Devices / Re: string of push buttons on 1 dataline on: June 06, 2013, 10:51:23 am
Where can I find good information about maximum lengths supported? I found a good basic introduction, this doesn't mention lengths though.
http://www.byteparadigm.com/applications/introduction-to-i2c-and-spi-protocols/?/article/AA-00255/22/Introduction-to-SPI-and-IC-protocols.html

What do you suggest for protocol? I had a string of led lights that uses TTL levels for distances of 30cm between (20 lights). Or do I need something like RS485 (but that requires an extra IC)?

I would like to use RGB, that would be four pins. However if I use 5050 RGB leds with a WS2812 I can drive them as SPI, needing just one wire.

I found also the BlinkM minM that seems to use I2C. Maybe that's a solution. They have already a protocol and I could even run
Arduino on it. However they state also that long length are a problem).
http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf
http://thingm.com/products/blinkm-minm.html
http://todbot.com/blog/2011/03/22/blinkm-smart-led-as-the-smallest-arduino/comment-page-2/#comment-84106
https://getsatisfaction.com/thingm/topics/length_of_i2c_bus (indeed some problem with longer lengths).



7  Using Arduino / Networking, Protocols, and Devices / Re: New to Arduino and WiFly on: June 06, 2013, 04:26:47 am
Just a suggestion. I found that the WiFly manual provided a good detail. I've shared a WiFly communicator library (actually a fork from the Seeed WiFishield library).With that library you can use the native commands from the WiFly.

More info: http://www.kasperkamperman.com/blog/arduino-leonardo-or-helios-wifly-library/
https://github.com/kasperkamperman/WiFly_communicator

The Wifly can be buggy sometimes with data and opening and closing connections.
8  Using Arduino / Networking, Protocols, and Devices / Re: string of push buttons on 1 dataline on: June 06, 2013, 04:21:31 am
I should have put that information in my first post. A rough estimation:

- About 30cm (12 inch) in between the nodes.
- 20 till 40 nodes.
- Led update frequency about 25/30 frames a second.
- I think for buttons 10 times a second will be sufficient.
9  Using Arduino / Networking, Protocols, and Devices / string of push buttons on 1 dataline on: June 06, 2013, 02:46:15 am
I would like to be able to make a string of buttons and combine this with leds.

A lot of led strings use SPI or an SPI like protocol, needing sometimes only 1 dataline or 1 dataline + clock.

https://www.sparkfun.com/products/10312 (WS2801, 1 dataline, 1 clockline, ground, 5V)
http://www.insomnialighting.com/products/rgbsmdws2811.html (WS2801, 1 dataline, ground, 5V)

I would like to combine leds in this fashion with a push button and I would like to use a minimal amount of wire.

Any idea if there are any IC's that I can use to make a chain of push buttons (or capacitive touch sensors), with a protocol like SPI or I2C. Of course as inexpensive as possible (I don't want to chain one Arduino for each button). Although maybe an ATtiny85 could be a solution (http://forum.arduino.cc/index.php?topic=126771.0)

I tried to search a bit in the forum, but couldn't find anything. Maybe I'm searching with the wrong terms.
 
Any idea's are welcome. Thank you in advance.
10  Using Arduino / Networking, Protocols, and Devices / Re: Zigbee Light Link on: January 20, 2013, 03:57:57 pm
Eventually possible with:
http://tronixstuff.wordpress.com/tag/bitcloud/

Zigduino:
http://logos-electro.com/zigduino/
https://www.sparkfun.com/products/9734

Atmel Bitcloud (is closed source, so you cannot seem to access this from the Arduino IDE).
https://groups.google.com/a/logos-electro.com/forum/?fromgroups=#!topic/zigduino-discuss/NrWzWHNyTxg

http://www.atmel.com/tools/BITCLOUDPROFILESUITE-ZIGBEEPROPUBLICPROFILE.aspx?tab=overview
https://github.com/logos-electromechanical/Zigduino-1.0
11  Using Arduino / Networking, Protocols, and Devices / Re: Zigbee Light Link on: November 23, 2012, 11:57:20 am
Same question over here:
http://electronics.stackexchange.com/questions/49629/zigbee-light-link-and-xbee-interoperability
12  Using Arduino / Networking, Protocols, and Devices / Zigbee Light Link on: November 21, 2012, 07:44:06 am
Hi,

I was curious if there is a possibility to use to "Zigbee Light Link" protocol with the wireless shield (http://arduino.cc/en/Main/ArduinoWirelessShield) and the Digi Xbee modules (http://www.digi.com/products/wireless-wired-embedded-solutions/zigbee-rf-modules/zigbee-mesh-module/).

I've found some more info on a Texas Instruments CC2530 system on a chip. In this document they describe the new standard:
http://www.ti.com.tw/downloads/techday2012/bk/Industrial2_01.pdf

However I don't get any clue if this protocol is supported by current modules, or if this is something Digi has to built into a module.

Can someone who has more knowledge about Zigbee shine a light (smiley) on this?

It would be interesting to be able to communicatie with the new Philips Hue lights (http://www.meethue.com/) from Arduino directly. Or to build lights with Arduino that can be controlled by this protocol (since Philips, Osram and more manufactures are going to support this).
13  Using Arduino / Programming Questions / Re: Using IR Remote library while bit banging on: September 23, 2012, 04:15:15 am
I've solved it till now with an extra Arduino (I had one lying around, so that was easier than the "breadboard" circuit) for the IR remote.

However running everything from one Arduino would be better of course.
14  Using Arduino / Programming Questions / Re: Using IR Remote library while bit banging on: September 20, 2012, 11:26:57 am
Ok, but that seems pretty advanced. I was already glad that I could modify that DMX code. I'm not really into all the assembly stuff.

Do I only have to replace these delay and asm parts? Any hints or resources on how I could do that?

Code:
delayMicroseconds(22);
asm("nop\n nop\n nop\n nop\n");   
15  Using Arduino / Programming Questions / Using IR Remote library while bit banging on: September 20, 2012, 09:28:38 am
I'm working on a light installation that I would like to control with my IRremote.

To control the light string (Triklits) I use a modified DMXcode from the examples on the playground http://arduino.cc/playground/DMX/Examples

As IRlibrary I use shirriff Arduino Infrared Library (https://github.com/shirriff/Arduino-IRremote). This one works with interrupts, I've read that this is every 50microseconds.

Or course controlling the strings and receiving remote data works perfectly in seperate sketches. However the remote doesn't work when I add it to the sketch. I think because when sending the light string data interrupts are disabled (cli() and sei().

I tried to enable receiving IR using attachInterrupt (I use pin 2 (interrupt 0) for the IRreceiver). However the IRreceiving code stays running, so this didn't solve it either. I tried to disable the timer in the library again by adding (TIMER_DISABLE_INTR; to the IRrecv::resume() function). But than I didn't receive any data.

I tried different IRremote libraries and solutions, but none of them seem to work fine so I prefer to use the shirriff ir remote library.

Is it possible what I want? Or is it smarter to use two Arduinos one for receiving the IR and one for controlling the lights.
Any idea's on what I can modify?

I doesn't matter if the lights don't update when receiving IR.

Code IRremote
Code:

#include <IRremote.h>

int RECV_PIN = 2;
IRrecv irRecveiver(RECV_PIN);
decode_results results;

void setup()
{ Serial.begin(57600);
  irRecveiver.enableIRIn(); // Start the receiver
}

void loop() {
      
if (irRecveiver.decode(&results))
{  decodeReceivedIR(&results);
    irRecveiver.resume();
}



Code Triklits
Code:
int pin = 5;  
int port_to_output[] = {
   NOT_A_PORT,
   NOT_A_PORT,
   _SFR_IO_ADDR(PORTB),
   _SFR_IO_ADDR(PORTC),
   _SFR_IO_ADDR(PORTD)
   };

#define ARRAYLENGTH 24*3
int intensity_Array[ARRAYLENGTH];
int arraylength=ARRAYLENGTH;

byte ledNum;
byte redVal;
byte grnVal;
byte bluVal;  

int arrayStartPos;

// ----------------------------------------------------------

void setup()
{ pinMode(5, OUTPUT);
  digitalWrite(13, HIGH);  
  Serial.begin(57600);
}

void loop()
{
    for(int i=0;i<(23*3);i=i+3)
    {
      redVal = 0;
      grnVal = 10;
      bluVal = 128;
      
      if(i==(22*3)) redVal = 128;
      
      arrayStartPos = ledNum*3;
    
      intensity_Array[i]   = redVal;
      intensity_Array[i+1] = grnVal;
      intensity_Array[i+2] = bluVal;
    }
  
  
    cli();    
    sendFrame();
    sei();
}

void sendFrame()
{ int portNumber = port_to_output[digitalPinToPort(pin)];
  int pinMask = digitalPinToBitMask(pin);
  
  byte header = B0100; // reversed by loop to B0010
  byte mask;
  
  // header 0010 --------------------------------  
    
  for (int count = 0; count < 4; count++)
  { if (header & 0x01) _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;
    else               _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask; // 0
    header>>=1;
    delayMicroseconds(22);
    asm("nop\n nop\n nop\n nop\n");    
  }  
  
  // RGB data 24 * 32 bits-----------------------
    
  for(int i=0;i<24;i++)
  { int count=i*3;  
    int r_Byte=intensity_Array[count]; //;intensity_Array[count*3];
    int g_Byte=intensity_Array[count+1];//intensity_Array[count];//=intensity_Array[(count*3)+1];
    int b_Byte=intensity_Array[count+2];//=intensity_Array[(count*3)+2];  
  
    // send interlaced RGB data for 1 light
    for (mask = 0x01; mask>0; mask <<= 1)
    { if (r_Byte & mask)  _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;  // 1
      else                _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask; // 0
    
      delayMicroseconds(22);
      asm("nop\n nop\n nop\n nop\n");
    
      if (g_Byte & mask) _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;  // 1
      else               _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask; // 0
    
      delayMicroseconds(22);
      asm("nop\n nop\n nop\n nop\n");
        
      if (b_Byte & mask) _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;   // 1
      else               _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask;  // 0
      
      delayMicroseconds(22);
      asm("nop\n nop\n nop\n nop\n");
  
      if (b_Byte & mask) _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask; // 0
      else               _SFR_BYTE(_SFR_IO8(portNumber)) |= pinMask;  // 1 // p is opposite of b  
    
      delayMicroseconds(22);
      asm("nop\n nop\n nop\n nop\n");
    
      //delayMicroseconds(16); // shorter because calculation at start.. it works...
    }    
  }
  
  // send trailer bit 0
  _SFR_BYTE(_SFR_IO8(portNumber)) &= ~pinMask;
  delayMicroseconds(17);
}



Pages: [1] 2 3 ... 5