NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Parsing a string in a function

February 17, 2011
by MichaelJE2
MichaelJE2's Avatar

Ok, so this code compiles, but doesn't run properly. When I call the function strToBlink("michael"); (which I wrote in a separate .c file, and did all the necessary things to make work) it simply skips over that bit of code. Here is the function (I've tested the functions it calls as well, they work just fine).

void strToBlink(char* input)
{
    int l, i;
    l = strlen(input);

    for(i = 0; i<l; ++i)
    {
        charToBlink(input[i]);
    }
}
February 19, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi MichaelJE2,

I see no reason why your code would just skip over the function here. My suggestion is to print out some debug statements. Maybe check out what the value of l is, perhaps strlen is doing something weird.

Humberto

February 19, 2011
by bretm
bretm's Avatar

When you say strToBlink("Michael") it won't work with the default way that Nerdkits makefiles are configured. Sram-based strings don't get initialized. Even if you fix the makefile you would be wasting valuable Sram space on a constant string value which would have to start out in program memory anyway. Use the PSTR macro around the string and pgm_read_byte to read the characters from the char* locations. Check for a zero byte to end the string instead of using strlen, otherwise you end up making two passes over the sring.

February 19, 2011
by MichaelJE2
MichaelJE2's Avatar

Ah hah! I will try your suggestion when I get the ATmega328 upgrade (I hooked up my 168 to power wrong, and fried it) But I'll bet that'll fix it.

February 26, 2011
by BobaMosfet
BobaMosfet's Avatar

Why are you incrementing 'i' BEFORE you make your call to charToBlink()? --- '++i' tells it to increment 'i' and then execute the statement in the loop. I would think you would want to increment it afterwards like this: 'i++'

Without seeing charToBlink(), we're not seeing the entire algorithm.

BM

February 27, 2011
by MichaelJE2
MichaelJE2's Avatar

I tried hevans' suggestion, and it worked. Thanks! :)

Post a Reply

Please log in to post a reply.

Did you know that you can connect to certain car computers via the OBD-II port with a microcontroller? Learn more...