I need help with coding my robot!

Ask questions about projects relating to: aerodynamics or hydrodynamics, astronomy, chemistry, electricity, electronics, physics, or engineering.

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
brotato9898
Posts: 5
Joined: Sun Feb 14, 2021 5:10 pm
Occupation: Student

I need help with coding my robot!

Post 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!)
bfinio
Expert
Posts: 740
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: I need help with coding my robot!

Post 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.
brotato9898
Posts: 5
Joined: Sun Feb 14, 2021 5:10 pm
Occupation: Student

Re: I need help with coding my robot!

Post 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
bfinio
Expert
Posts: 740
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: I need help with coding my robot!

Post 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.
brotato9898
Posts: 5
Joined: Sun Feb 14, 2021 5:10 pm
Occupation: Student

Re: I need help with coding my robot!

Post by brotato9898 »

well i figured it out. I had to reverse the current on one and i did so yea
bfinio
Expert
Posts: 740
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Science Buddies Staff
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: I need help with coding my robot!

Post by bfinio »

Glad you got it working!
Locked

Return to “Grades 6-8: Physical Science”