How do I use enum?

HI Paul

I started a new topic for this.
As you suggested instead of using strings or pointer for choices I should use enum.

Please let me know what I m doing wrong.

void setup() 
{
  Serial.begin(9600);

  enum motion {
    UP, DOWN, STOP  };

  //ControlWinch(UP);
}

void loop() 
{

}

void ControlWinch(motion dir)

{
  if(dir == UP)
  {
    Serial.println("UP");
  }
  else if(dir == DOWN)
  {
    Serial.println("DOWN");
  }
  else if(dir == STOP)
  {
    Serial.println("STOP");
  }
}

This is the errors:

test:1: error: variable or field 'ControlWinch' declared void
test:1: error: 'motion' was not declared in this scope
test:19: error: variable or field 'ControlWinch' declared void
test:19: error: 'motion' was not declared in this scope

Thank you

Regards

Luan :astonished:

The motion enum is local to setup, so it is not accessible outside of setup(). Move the declaration before setup() to make it global in scope (and accessibility).

Hi Paul

I move it right to the top and this it the error:

sketch_aug30c:-1: error: variable or field 'ControlWinch' declared void
sketch_aug30c:-1: error: 'motion' was not declared in this scope
sketch_aug30c:-1: error: variable or field 'ControlWinch' declared void
sketch_aug30c:-1: error: 'motion' was not declared in this scope
sketch_aug30c.cpp: In function 'void ControlWinch(motion)':
sketch_aug30c:31: error: expected `}' at end of input

Do I need to include something?

Thanx for you help and patience

ttfn

Please read - Arduino Playground - Enum Resource - about how to use enum.
and - scope - Arduino Reference - about variable scope

Thank you

Will do

I really appropriate the help from you guys

ttfn

The enum page will tell you to create a new tab. I added stuff.h, and put this in it:

enum motion {UP, DOWN, STOP};
void ControlWinch(motion dir);

This, then, compiles:

#include "stuff.h"

void setup() 
{
  Serial.begin(9600);
  ControlWinch(UP);
}

void loop() 
{
}

void ControlWinch(motion dir)
{
  if(dir == UP)
  {
    Serial.println("UP");
  }
  else if(dir == DOWN)
  {
    Serial.println("DOWN");
  }
  else if(dir == STOP)
  {
    Serial.println("STOP");
  }
}

Thank you very much Paul

When I created the .h file it did not “see” then new .h file.

It is working now after I restarted the IDE .

Have a great day.

Best Regards

Luan :slight_smile: