Data Type Problems and Broken For Loop

Hello,

This is my first time posting here and I am not used to forums in general, so I am sorry if I have made a mistake in my post.

I am working on an Arduino powered smartphone controlled robot and I cannot to get the Arduino sketch to work correctly.

The controls of the robot are based on a 10 digit number sent via bluetooth which gets broken down into single digits and loaded into an array, each integer in the array is then checked to determine if a part of the system is on or off.

Before I started making the Arduino sketch, I tested out my control data decoding algorithm by making this. Which went well and did everything I needed it to with no issues.

But when it came to implementing it in the Arduino sketch I could not get it to work. Here is what I have got so far for the sketch. I have used all the same data types in the Arduino sketch as I have used in the in the C++ program and also tried using a number of different data types such a long int and float, with limited success. I managed to get the sketch working by declaring the bluetooth_input and the doubles used in my split function as long int, but this meant that it only worked when the control data started with a 1, when it started with 0 it did not work, unlike my original program.

Another problem, is that the first for loop (line 146) gets itself stuck in an endless cycle if the number in the condition is greater than 9 and I am unsure why. I have checked the split function, as the loop just calls the function 10 times, but I cannot see anything which would effect the counter. I have put a work around in place to circumvent the problem but would still like to know if anybody knows what may cause this problem for future reference.

This is the first big project I have done with Arduino and C++, so I am not entirely sure what I am doing. If any anybody knows what could be causing these problems, I would very much appreciate your help.

Thank you, notroot.

The problem is on line 145.

Read the stickies and POST YOUR CODE!

Cheers for the reply, but what exactly is the problem? Line 145 is blank.

And as for posting my code, I take it you mean as an attachment? Didn't see that till there now, thanks for the tip.

Whoosh!

Put these in the appropriate place in my post: SARCASM /SARCASM

If you'd bothered to read my post before replying, you'd see that I included pastebin links to my code. Assuming that's what you're trying to imply with your snarky comments and sarcasm tags.

You should post the code here. Folks might just skip your thread before they'll chase down your code on another site.

If you'd bothered to read my post before replying, you'd see that I included pastebin links to my code.

A lot of us will not look in the rubbish bin for your code. Post the code HERE if you want help. If you want to just whine and complain, please go do that at some other forum.

Hello and welcome,

( At least pastebin has syntax coloring... )

Your problem is most likely that you write out of the array bounds.

int decoded[9];

This creates an array of 9 elements: from 0 to 8.

But later you do

decoded[9] = split(bluetooth_input, count);

By doing this, you write in a memory cell that may be used by another variable, so your program is bugged. This is called a buffer overflow error.

Read this tutorial about arrays ;)

Also number 1000000000 is too big to fit in an int, so your function get_divideby is also bugged, and it can be greatly simplified:

uint32_t get_divideby( uint8_t count )
{
  return (uint32_t)pow( 10, count );
}

Cheers for the feedback, guix. It was really helpful for fixing the loop.

As for the split function not working, turns out when I assigned the number to a variable using cin in my test program, it ignores any zeros at the front of the number. However, in the Arduino sketch, it recognises them as an octal prefix and converts the input number into the decimal equivalent.

I think I can get it working from here, thanks for the help.