Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help converting some code.


  • Please log in to reply
44 replies to this topic

#1 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 23 January 2011 - 10:03 PM

I am trying to convert this arduino codeto the netduino. I picked up two of these LED strips. The arduino code works perfectly.

After debugging the led array is correct. I simplified the debugging by trying to output to a single LED. Again, works on arduino but not netduino. What is happening on the netduino is twice the specified LEDS is changing to random colors. So if I specifiy 1 LED it will affect 2 LEDs, if I specify 3 it will affest 6.

Here's the post_frame function, I commented out the arduino line and put the .NET line below it. I don't see why it would cause those problems.

//Takes the current strip color array and pushes it out
        private void post_frame()
        {
            //Each LED requires 24 bits of data
            //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 
            //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
            //Pulling the clock low for 500us or more causes the IC to post the data.

            for (int LED_number = 0; LED_number < _LEDS.Length; LED_number++)
            {
                int this_led_color = _LEDS[LED_number]; //24 bits of color data

                for (byte color_bit = 23; color_bit != 255; color_bit--)
                {
                    //Feed color bit 23 first (red data MSB)

                    //digitalWrite(CKI, LOW); //Only change data when clock is low
                    _clock.Write(false);

                    //int mask = 1L << color_bit;
                    int mask = 1 << color_bit;
                    //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.

                    if ((this_led_color & mask) != 0)
                        //digitalWrite(SDI, HIGH);
                        _data.Write(true);
                    else
                        //digitalWrite(SDI, LOW);
                        _data.Write(false);

                    //digitalWrite(CKI, HIGH); //Data is latched when clock goes high
                    _clock.Write(true);
                }
            }

            //Pull clock low to put strip into reset/post mode
            //digitalWrite(CKI, LOW);
            //delayMicroseconds(500); //Wait for 500us to go into reset
            _clock.Write(false);
            DelayMicroSec(500);
        }
        /// <summary>
        /// Blocks thread for given number of microseconds
        /// </summary>
        /// <param name="microSeconds">Delay in microseconds</param>
        private void DelayMicroSec(int microSeconds)
        {
            DateTime startTime = DateTime.Now;

            int stopTicks = microSeconds * 10;

            TimeSpan divTime = DateTime.Now - startTime;
            while (divTime.Ticks < stopTicks)
            {
                divTime = DateTime.Now - startTime;
            }
        }


#2 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 24 January 2011 - 01:25 AM

Hm. I've stared at it for a while and I don't see anything wrong with the code. Maybe you should show us the whole program? I'm grasping at straws, but it would be nice to find out how _clock and _data are defined, what's in _LEDS, and also whether you've swapped the wiring for your clock and data wires.

#3 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 02:28 AM

When I get a chance I will post the code, it's ugly right now with all the stuff I tried to figure out what is going on. The wiring should be the same. I have the netduino and arduino on the same breadboard and I am using pin 2 and 3 the same way on both.

#4 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 03:34 AM

source

#5 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 03:35 AM

source

Attached Files



#6 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 24 January 2011 - 03:43 AM

Hm, don't see a problem yet. Colors.RandomGreen, RandomRed, and Color(red,green,blue) are all incorrect (the shift amounts should be 8 and 16, not 2 and 4) but you're not using that code anyway

#7 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 03:52 AM

Yeah, I haven't cleaned up all of the code yet. Some copy and past issues. The basics need to work first. I've stepped through every line to verify the values are correct. I am starting to wonder if there is a timing issue, like something on the netduino is taking more than 500us thus latching the LEDs. But even then if it was latching too early it still shouldn't be pushing out more information than it has. I tried changing the delay for 500us to a thread.sleep(1) to make sure it was long enough. Same problem.

#8 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 24 January 2011 - 04:18 AM

ugh. sorry, I have no idea. By the way you want _colors.Length, not Length-1 in your RandomSystemColor()

I keep triggering on this line in the documentation for the chip, but it's probably not relevant:

To prevent the reflection, it is necessary to connect a 50Ω resistor at the data input or output port for impedance match.



#9 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 04:29 AM

I think I missed that in the docs. I will have to give it a try. Though it is odd the arduino code works fine. If you looks at RandomSystemColor is is a random number between 1 and 7, not 0 and 7. return _colors[rand.Next(_colors.Length-1)+1] The solid colors - red, white, blue, and green before the random - still have the issue.

#10 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 04:38 AM

Actually, could it be an issue that the arduino uses 5v and the netduino uses 3.3v? This is where my inexperience in electronics shows.

#11 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 04:53 AM

I've been thinking about it. If I can't figure this out worse case scenario I use my arduino as the led driver then have the netduino communicate with that.

#12 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 24 January 2011 - 11:05 PM

I am going to order a level converter from Sparkfun and see if the 3.3v I/O is the issue. I could see that might be the problem. If the led strip doesn't detect every clock change then bits may or may not go out and it may miss the 500us delay to latch because it missed the clock change to low.

#13 W1N9Zr0

W1N9Zr0

    New Member

  • Members
  • Pip
  • 1 posts

Posted 25 January 2011 - 11:27 PM

The code may be running too slow and the 500us timeout occurs randomly before it should. I timed post_frame on my netduino and it takes about 14 milliseconds per led.

#14 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 26 January 2011 - 04:04 AM

Well, if the latch happens too early it should affect more leds. However, I tried to time it and I get 4.8ms per led. Hmmmm...

#15 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 27 January 2011 - 09:48 PM

I did some more testing. Updating all 32 LEDs with the arduino takes 14ms. So is the increased time on the netduino due to the .NET overhead - like garbage collection?

#16 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 January 2011 - 09:51 PM

I did some more testing. Updating all 32 LEDs with the arduino takes 14ms. So is the increased time on the netduino due to the .NET overhead - like garbage collection?


SirPoonga,

The extra overhead is due to the interpreted nature of .NET MF. It takes about a thousand assembly-level instructions to execute each .NET MF instruction.

You can use native code to get the full raw-speed of the ARM MCU (and execute the whole series incredibly fast). If that's a bit too much to chew, have you looked at Corey's Fluent native interop?

Chris

#17 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 28 January 2011 - 09:19 PM

I didn't realize it was going to be that much slower with a 4x faster CPU. The speed isn't my problem with the LEDs though. I don't see how the slower speed would cause this problem. If the leds get latched early because the 500us wait happened early it would affect less leds, not more. I am sure it is the 3.3v to 5v. I should know next week when I get my level converter. Speed is an issue for this project though because that limits how fast animations can be. What I like about the netduino plus for my project is the built in ethernet and sd card. That's more pins available. So I am thinking about getting a protoshield and soldering my boarduino or arduino nano to that. That will be the LED driver while the netduino can provide the animation information from the sd card and control commands from ethernet. Since I will have plenty of idle power on the netduino I might as well use one of the simple http servers instead of getting php working on my fonera. The netduino and arduino can communicate over spi - maybe serial or i2c to reduce pin usage. It will actually save a lot of memory on the arduino as sdfatlib takes up a lot of space.

#18 SirPoonga

SirPoonga

    Advanced Member

  • Members
  • PipPipPip
  • 96 posts

Posted 01 February 2011 - 05:54 AM

The level converter did the trick. However, it is way too slow for me. I cycled all 32 LEDs between red, blue, and green. On the arduino they are fast enough you get a flickering white. On the netduino there is no color blending. Pushing only one LED the netduino is fast enough that it barely flickers white. I'll post the cleaned up code into the projects forum.

#19 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 01 February 2011 - 07:25 AM

The level converter did the trick. However, it is way too slow for me. I cycled all 32 LEDs between red, blue, and green. On the arduino they are fast enough you get a flickering white. On the netduino there is no color blending. Pushing only one LED the netduino is fast enough that it barely flickers white.


Have you thought about using Corey's Fluent interop? It should give you _plenty_ of speed...

Chris

#20 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 01 February 2011 - 01:19 PM

Have you thought about using Corey's Fluent interop? It should give you _plenty_ of speed...

I'm willing to help you out on this if you like. It will be easy to translate your program. But to use it, you'll need to reflash your firmware.

(side note to Chris: I guess I should inject my entry point into the latest firmware you've posted. Hopefully it compiles with some version of gcc, and you have the source on codeplex or something)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.