bug ?

This forum is for all topics related to Simulator for Arduino v0.98. Please include sample code where possible.

Moderator: Adrian

adolfotregosa
Posts: 2
Joined: Thu Nov 15, 2012 10:49 pm

bug ?

Postby adolfotregosa » Thu Nov 15, 2012 10:53 pm

Hello. I think i found a bug..


#define position 20
word someSensor[position];
byte i;
word total;
word average;

void setup()
{

}

void loop()
{

for (i = 0; i < position; i++)
{
total = total - someSensor[i];
someSensor[i] = analogRead(0);
total = total + someSensor[i];
average = total / position;
}
}

this code does not work on the simulator (works on the arduino) because variable "average" is always 20... if i change "average = total / position" to "average = total / 20" it works.

adolfotregosa
Posts: 2
Joined: Thu Nov 15, 2012 10:49 pm

Re: bug ?

Postby adolfotregosa » Fri Nov 16, 2012 11:08 pm

another one..
void loop()
{
if(thing == 1 && something() < 100)
{
}
}

int something()
{
{
do something_2;
}
return result;
}

my if condition only works if I do this..

int something_3 = something();
if(thing == 1 && something_3 < 100)
{
}

arduino accepts functions on if conditions

Simulator_admin
Site Admin
Posts: 239
Joined: Thu Feb 02, 2012 6:07 pm

Re: bug ?

Postby Simulator_admin » Fri Nov 23, 2012 6:27 pm

Hi thanks for pointing these issues out. Yes two bugs. The first is caused by the #define which for some reason prevents the number average being set correctly. Inside the Simulator, average is just about to be set to the correct value of 0, when the Simulator sees the define and sets the number back to the #define value. We have a simple fix for this which will need some testing.

With the subroutines, this is a known issue. Having subroutines inside expressions makes it very hard to substitute the return value. Expression such as if (subroutine()) should work fine but expressions such as if (subroutine1()+subroutine2()) cannot work yet until the code is written to handle this. Anyway, these issues are logged here for further work Quality Control

User avatar
Adrian
Super user
Posts: 56
Joined: Thu Jan 10, 2013 10:22 pm

Re: bug ?

Postby Adrian » Fri Jan 11, 2013 12:07 am

I'm pretty sure that I read on the Arduino site (can't find it now) that there were issues with this form of compound evaluation. Clearly the simulator ought not be better at evaluation than the target system. It may be prudent to research this further rather than create a new bug.

Since reading that, if I have any issues, I simplify my code and then start debugging. Once working I attempt to return to the compact form. However (without delving into the compiler), it my be that simplified code creates a faster/more efficient output anyway ;)

So something like

Code: Select all

if(evalX(val1) + evalY(val2) < evalZ(val3)){

}


would become

Code: Select all

eX = evalX(val1);
eY = evalY(val2);
eZ = evalZ(val3);

eX += eY;

if(eX < eZ){

}

Maybe easier to read too?
Apologies for being opinionated - it's nothing personal, feel free to complain.
usual system: Pro V0.98 Arduino Mega 2560 Samsung i5-3210M 2.5Ghz 6GB windows home premium SP1


Return to “Version 0.98”

Who is online

Users browsing this forum: No registered users and 1 guest