Posted January 9, 20214 yr 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
January 9, 20214 yr 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?
January 9, 20214 yr Author 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); "
January 10, 20214 yr 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.
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.