Just got Maker Kit w/ Arduino UNO - attempting to mix two beginner projects

Hello all!

First off, I’m excited to join the crowd of Arduino lovers and all here. I went and got the Arduino Uno Maker Kit to begin learning, and having fun.

Quick background- when in HighSchool (the years ago that that was, haha! :sweat_smile:) I built and wrote my own little 2D video game; think Zelda on the Gameboy. It was simple, but a blast to do. I wrote it in Java, and since then, that’s the last I did anything with Code. Now I’m teaching myself C because it seems to be the most used in embedded systems, and that’s what I’m interested in learning. Software to hardware real life applications are the coolest! :smiley:

Anyways, here is what I am attempting.

I have done a lot of basic tutorials over the last few months, and after learning some things, I am mostly intrigued by servo’s and DC motors. I learned through study how to control servo’s with a button, and learned how to work with the little DC motors. Fun!

Now I am trying to learn how to combine a servo, button, and motor into one application.

Here’s my goal-

Press & release button → small motor turns on for set amount of time → servo turns 180 degrees → motor turns on for set amount of time → servo turns back 180 degrees to beginning position - end cycle.

I think if I learn how to do something like this, I can really build on it, and do all kinds of fun little projects.

I tried asking good ole’ Professor Google for some help, but most of the things I saw were “hey, look at me press a button and see a servo turn” or “look, a potentiometer turns this servo.”

Chances are I was wording my queries incorrectly, so Google just misunderstood me. :stuck_out_tongue:

Well, my brain is a sponge, and I look forward to hearing back from y’all!

Thanks!

Hi, Welcome to the forum.

Look into the Servo.h library in the Learning Section, go from there.
Do not power your servo(s) from the Arduino - provide a separate power source.
Do connect all Grounds together, need that common reference point for signal levels.

Same with DC motors - do not connect directly to Arduino pins. Use a NPN transistor or N-channel MOSFET to sink current thru the motor; use an H-bridge if you want two direction control.

I have done a lot of basic tutorials over the last few months, and after learning some things, I am mostly intrigued by servo's and DC motors. I learned through study how to control servo's with a button, and learned how to work with the little DC motors.

Given this, it seems you have pretty much all the tools you need. Can you simply make a sketch that has these four things happening with suitable calls to delay() between them?

Note that later on, you'll want to drop the use of delay, but for your current requirement, it should work fine.

Hey guys, thanks for the replies!

It's interesting to hear that I need to power everything separately from the Arduino. All the tutorials out there have everything directly connected it seems? But that's probably just because they're showing one or a few servo's at a time doing something simple. The H bridge setup makes total sense though for a multi-direction motor.

As far as writing the sketch, that's what I'm trying to learn how to do at the moment. I'm pretty fresh with C. I understand programming logic to a degree from my experience with JAVA, but I'm still reading up on C and trying to teach myself.

I'll post a sketch up here once I've figured out how to write some of it in.

Thanks again! :smiley:

It’s very easy to write and test short sketches on the Arduino. So start with a small sketch that does a single part of your project. When you are comfortable that you have it working and understand it do another one. When they are all done you can start to consider how to amalgamate them. (By the way this is not just a beginner’s technique).

It is common for beginners to make use of the delay() function - and it is very convenient for quick and dirty bits of code. But it quickly starts to get in the way of slightly more complex sketches because the Arduino can’t do anything else (such as check for a button push) while the delay() is running. The Blink Without Delay example sketch shows how to manage timing without using delay().

…R

I see what you're saying Robin. I'll try to avoid using delay() then! I'll try practicing those bits and see how to piece it together, because I feel like it's important to be able to read a button push without having the delay() function preventing that.

That would be kind of ridiculous if you had a small robot having to wait out a delay before you could make it do something else! :roll_eyes:

By the way, I have been making each separate part work so far. My Servo is doing what I want it to, and my little motor is doing what I want it to, but it's the putting it all together in the code section I'm working on now.

Thanks for the heads up about the delay() issues though!

Just now looking into the Blink without delay tutorial- this is absolutely something I needed to learn! Super helpful!

You people are full of awesome 8)

I'll practice this, then come back for more!

By the way, would it be a correct statement that if delay() prevents other functions from taking place during the programs execution, that it could be used in that way effectively and intentionally?

Like say if you didn't want your little robot to try to move forward or backward while it's performing a lifting or bending over function? Could you use a delay() command in there to make sure you were only performing the lifting/bending movements and making sure you didn't get thrown off balance by trying to move?

ProtoFun:
Like say if you didn't want your little robot to try to move forward or backward while it's performing a lifting or bending over function? Could you use a delay() command in there to make sure you were only performing the lifting/bending movements and making sure you didn't get thrown off balance by trying to move?

Unfortunately not. The delay() function takes over the whole Arduino while it is running. The best way to restrict the program to one activity is to have what is called a "state" variable (any old variable) that records the state of the system. For example it might be called "bending" and if that variable is TRUE other functions would know not to operate.

...R