0

sorry if I sound incompetent, but I know nothing about coding. I have a group project where I must sort various balls into different tracks. The final issue I have is that I must use an arduino for this portion. However, none of us know how to program, and the only experience we have is from doing the lessons from adafruit (copy and paste written code).

What I am attempting to do is attach a paddle to the tip of a high torque servo motor. I will then automatically feed the servo signals, ranging from 0 to 5. Each signal tells which position for the servo to turn to. Could anyone please help guide me in how to write this code? I was thinking of using this guide for my motor. However it uses a POT. My project must be completely automated. The positons also are all within 180*.

Thank you!

  • Re “I will then automatically feed the servo signals, ranging from 0 to 5”, where do the numbers come from? Are the positions equally spaced, or arbitrary angles? – James Waldby - jwpat7 Dec 2 '15 at 5:01
  • The numbers come from another sensor, which detects which ball is coming. I was not told which type of sensor this is, but I do know that what is given from it is a number corresponding to a different ball. As far as position, there is only 3 positions I wish for this servo to turn. 90* vertical, 45*, and 315*. So in total It will only need to turn 135* degrees from the farthest two points, 90*-315* – Andrew Dec 2 '15 at 5:08
  • Welcome to Arduino SE. Be sure to take the tour at arduino.stackexchange.com/Tour – SDsolar Apr 17 '17 at 4:47
1

So you have a "number" and you want to set the servo to some angle. The logic is very simple:

  • If number is 0 then set angle to 90
  • If number is 1 then set angle to 45

etc.

How you "get" the number hasn't been specified, so I will assume you just have an integer with a value between 0 and 5 inclusive.

Setting a servo's angle is as simple as writing that angle to the servo:

servo.write(45); // Set the angle to 45 degrees

So how do you join the two together? Well, there's three different ways that spring to mind.

  1. A string of if statements
  2. A switch construct
  3. An array

The first one is just like the bullet points above, just written as C:

if (number == 0) servo.write(90);
if (number == 1) servo.write(45);
... etc ...

The second method is very similar but can be more efficient when you know you only ever select one item from a list of options. Read this to see how: http://hacking.majenko.co.uk/time-to-make-a-switch

The third option, the array, is especially good when you have a lot of options to manage and they are all the same result with a different value:

const int angles[6] = {90, 45, 315, 0, 0, 0};

// ... later ...
servo.write(angles[number]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.