1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Using Enumerators

  1. Mar 13, 2010 #1
    I'm working on a project for my microcontrollers course. It involves programming a pathfinding robot that uses 5 reflected light sensors to follow a track. Since the code that we were provided that was written I don't know how long ago fails as hard as it does I decided to re-write the whole control system.

    Only thing is to simplify things when programming the logic into this thing I want to use enums to control the speed of each motor.

    I have NO clue how this guy put the code together, but it's just a big partially commented mess.

    Here is the applicable code from the header file
    Code (Text):

    enum motor_speed_setting { rev_fast, rev_medium, rev_slow, stop, slow, medium, fast };

    enum motor_selection { left, right };

    void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier);
    and here is the code from the source file

    Code (Text):

    void set_motor_speed(enum motor_selection the_motor, enum motor_speed_setting motor_speed, int speed_modifier)


        const static int motor_speeds[] = { -800, -600, -400, 0, 400, 600, 800};

        int duty_cycle;

        enum e_direction {reverse,forward} dir_modifier= forward;
    Now what I want to do is have my array (or vector, I like them better) with similar values, with enumerated values for each setting. I just don't get how this stuff goes together.

    How do I go about enumerating values to the values in the array/vector? I know how to assign them when creating the array by making the values equal something, but this fellow seemed to create the values, then enumerate them.
    Last edited: Mar 13, 2010
  2. jcsd
  3. Mar 13, 2010 #2


    Staff: Mentor

    You're making this much more complicated than it really is. An enum (short for enumeration or enumerated type) is nothing more than a type that lists a bunch of named constants. A variable of this type can be one of the values in the enumeration. That's all.

    You don't enumerate anything; you just use those named constants. Do a search for C++ enum and you'll find lots of pages that describe enumerated types. Here's one that I found: http://www.cppreference.com/wiki/keywords/enum.

    Your set_motor function takes three arguments: the first is the_motor, the second is a motor speed, and the third is a speed modifier. A call to this function would look like this:

    set_motor( left, rev_medium, 2);

    This call would apparently set the motor speed to medium for the left motor (the motor in the left wheel?). I don't know what the 2 would do, since you don't show enough of the code for me to be able to tell.
  4. Mar 13, 2010 #3
    I didn't code that, it was provided. The massive confusion I had when I tried to figure out what was going on is the reason I'm re-writing everything. I did manage to figure it out with that though. Thanks for the help.
  5. Mar 13, 2010 #4


    Staff: Mentor

    Right, I knew that you didn't code it. The thing about using enums is that the named constants give you a better idea of what's going on than just constants would.

    For example, a call to set_motor( 1, 3, 2); would be much harder to figure out.
  6. Mar 13, 2010 #5
    That makes sense.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook