Page 1 of 1

I need help with coding my robot!

Posted: Sun Feb 14, 2021 6:17 pm
by brotato9898
Hi! I'm haveing trouble building a motion sensor robot (link here https://www.sciencebuddies.org/science- ... #procedure) because I don't know how to code c++. I though it would tell us what to code in the last steps, but it tells us to do it ourselves. can somebody give me a sketch they used to finish the project?

please send stuff ive been google-ing for a while now :(

If any of u can code c++, can u tell me if you can change a variable value only in if else function? what ive been trying to do is change a motor speed to zero while a switch is pressed so it turns the car until the switch is not pressed


(Moderator combining your posts so experts will see that your post has not been answered!)

Re: I need help with coding my robot!

Posted: Mon Feb 15, 2021 5:16 pm
by bfinio
Hi,

Part of the challenge of this project is writing your own working code, so we can't give you completed working code. There are many good tutorials on the Arduino website and you can also open lots of example code that comes with the Arduino software: https://www.arduino.cc/en/Tutorial/HomePage. But to give a basic answer your question, you should be able to change a variable (like Motor1Speed) inside an if/else statement in the loop() function to make your robot turn. Hope that helps.

Re: I need help with coding my robot!

Posted: Mon Feb 15, 2021 8:58 pm
by brotato9898
Hi, thanks but how would i do that? Would i redeclare the function in the if statement? I tried doing that but there was no effect

Re: I need help with coding my robot!

Posted: Tue Feb 16, 2021 7:37 am
by bfinio
Do you mean re-declare the variable, not the function? You should not need to re-declare the variable, you should just be able to write

VariableName = 0;

inside the if statement.

If you are not that familiar with an Arduino and programming in C, then I highly recommend Tinkercad Circuits (you will need to create an account on the Autodesk website to access tehse).

https://www.tinkercad.com/learn/circuits/projects

It is an Arduino simulator that runs in a web browser. It has lots of tutorials that you may find useful. For example in this case, look at the Pushbutton (Digital Input) tutorial, which has a loop with an IF statement to monitor whether a button is pressed, and use that to control the LED. You could declare another variable (int motorSpeed = 0;) at the top of the program, and then you can change motorSpeed inside that IF statement (or anywhere else in the loop).

The "Using the Serial Monitor" tutorial is also useful for debugging, you can print out your variable values which can help you figure out what is going on in your code.

Re: I need help with coding my robot!

Posted: Sun Feb 21, 2021 7:00 pm
by brotato9898
well i figured it out. I had to reverse the current on one and i did so yea

Re: I need help with coding my robot!

Posted: Mon Feb 22, 2021 6:20 am
by bfinio
Glad you got it working!