Train_man

Function acting weird in Legoino Arduino code?

Recommended Posts

Afternoon all,

Im trying to create some logic that allows my kids to use the trains via the controller but the aurdino makes the trains stick to speed limits and prevents them from crashing into each other. At the moment it very basic but im having trouble with a function and im not sure what causing it. 

This issue: https://gyazo.com/94758aaeb2044ff75273321a48f989a9

It works fine up untill it hits the speed limit. While it does prevent the train going any faster, as you can see in the terminal it is printing BITS of a string in the "rules" function which im not even calling atm for debugging purpose and im not sure how its doing that?

Have i written the function syntax correctly? Am i calling it correctly??

What should happen is if the users trys to go faster then the limit, it should print out "Serial.print("Cant go faster then speed limit which is " + speedLimit); " 

But its not hitting that but is printing out a random bit of the string in a function im not even calling. Its sending me crazy. 

Any ideas would be much appreciated ! 

Thanks in advance

paste bin for the code should anyone want to test it: https://pastebin.pl/view/7202ee60

 

Share this post


Link to post
Share on other sites

Hi,

just got no idea why code is called.

But: if you change "min(100" in the callback function this should have the desired effect, or am I wrong?

 

Share this post


Link to post
Share on other sites

I think ive figured it out ...

 

The min() just makes sure the train doesnt go faster then 100. It basiclly returns the smaller of the two numbers. I think C++ doesn like concatinating strings and variables in the Serial.print functions. Ive split them out now and all seems to be working. 


      Serial.print("Cant go faster then speed limit which is ");
      Serial.println(speedLimit, DEC);
 

instead of :

      "Serial.print("Cant go faster then speed limit which is " + speedLimit); " 

Share this post


Link to post
Share on other sites
15 hours ago, Train_man said:

The min() just makes sure the train doesnt go faster then 100.

Sure, and replacing that with "min( speedLimit,.." " is just what you want to do. All my programs work with that.

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.