Syntax for enum?

Being in the beginning of a new project I would like to use enum instead of numerical codes but don't find enum in the reference.

Code:

#include<arduino.h>

//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2

boolean state, old_state;
byte mode = 0; //// 0 = Antal sidor, 1 = vinkel per steg
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4

#define cmd {increas,decrease,Go,Chg}

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

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(10, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)
  pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
  delay(10);//allow pwm timers to start

  int status;

  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("221101a Rotating");
  delay(5000);
  mylcd.print("Running");
  delay(1000);
}

cmd readButtons(void)
{
  if ((digitalIncreaseButton) == 0)
    return (increase);
  if ((digitalDecreaseButton) == 0)
    return (decrease);
  if ((digitalGoButton) == 0)
    return (Go);
  if ((digitalChgButton) == 0)
    return (Go);
  return (nill);
}
// The loop function is called in an endless loop
void loop()
{
  boolean cmd = readButtons();// + - Go Chg mode

  if (( millis() % 1000 ) > 499 )
  {
    state = true;
  }
  else
  {
    state = false;
  }

  if ( state) { //update twice per second;
    if ( mode == 0)  //number of surfaces
    {

      switch (cmd)
      {
        case increase:
          {
            faces++;
            brake;
          }
        case decrease:
          {
            faces--;
            if (faces < 1) faces = 1; //1 rotate one turn
            brake;
          }
        case Chg:
          {
            mode = 1; //switch to angle mode
            //          goFlag = false;//
            brake;
          }
        case Go:
          {
            AccelStepper(anglePos);
            anglePos += nrOfStepsPerRev * 360 / faces;
            brake;
          }
        default:
          brake;
      }
      if (goFlag)
      {
        AccelStepper(anglePos);
      }
    }
    else if ( state == 1 )  // Angle per step
    {

    }
  }
}// End of loop

Unrelated, but if you have hd44780 library version 1.0.2 (released 2019-05-21) or newer, you no longer have to invert the return status from mylcd.begin() before you pass it to fatalError()

--- bill

Thanks Paul!
That pulled the current plug.

Haven't checked what version I use but it's an "old friend" and I just copy the main parts from project to project. Thanks anyway.
Just checked. It's Version-0.9.3-........

Next obstacle. Looking at example codes, the compiler issues erros for:

  myStepper.runToPosition(anglePos);//Blocking until done
.
.
 myStepper.runTonewPosition(anglePos);//Blocking until done

The full code is:

#include<arduino.h>

//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

AccelStepper myStepper (7, 8);
// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2

boolean state, old_state;
byte mode = 0; //// 0 = Antal sidor, 1 = vinkel per steg
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4

enum cmd {increase, decrease, Go, Chg, nobutton};
unsigned long anglePos;

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

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(10, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)
  pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
  delay(10);//allow pwm timers to start

  //  myStepper.begin();
  myStepper.setMaxSpeed(500);
  myStepper.setAcceleration(10);

#define digitalIncreaseButton 2
  pinMode (digitalIncreaseButton, OUTPUT);
#define digitalDecreaseButton 3
  pinMode (digitalDecreaseButton, OUTPUT);
#define digitalChgButton 4
  pinMode(digitalChgButton, OUTPUT);
#define digitalGoButton 5
  pinMode(digitalChgButton, OUTPUT);

  int status;
  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("221101a Rotating");
  delay(5000);
  mylcd.print("Running");
  delay(1000);
}

cmd readButtons(void)
{
  if ((digitalIncreaseButton) == 0)  return (increase);
  if ((digitalDecreaseButton) == 0)  return (decrease);
  if ((digitalGoButton) == 0)        return (Go);
  if ((digitalChgButton) == 0)       return (Go);
  return (nobutton);
}
// The loop function is called in an endless loop
void loop()
{
  boolean cmd = readButtons();// + - Go Chg mode

  if (( millis() % 1000 ) > 499 )
  {
    state = true;
  }
  else
  {
    state = false;
  }

  if ( state) { //update twice per second;
    if ( mode == 0)  //number of surfaces
    {

      switch (cmd)
      {
        case increase:
          {
            faces++;
            break;
          }
        case decrease:
          {
            faces--;
            if (faces < 1) faces = 1; //1 rotate one turn
            break;
          }
        case Chg:
          {
            mode = 1; //switch to angle mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            //            AccelStepper(anglePos);
            myStepper.runToPosition(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 360 / faces;
            break;
          }
        default:
          break;
      }
      //     if (goFlag)
      //     {
      //       AccelStepper(anglePos);
      //     }
    }
    else if ( state == 1 )  // Angle per step
    {
      switch (cmd)
      {
        case increase:
          {
            anglePos += nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            break;
          }
        case decrease:
          {
            anglePos -= nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            //           if (faces < 1) faces = 1; //1 rotate one turn
            break;
          }
        case Chg:
          {
            mode = 0; //switch to face mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            myStepper.runTonewPosition(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 5 / 360;
            break;
          }
        default:
          break;
      }
    }
  }
}// End of loop
/*
   moveTo KEYWORD2
  move  KEYWORD2
  run KEYWORD2
  runSpeed  KEYWORD2
  setMaxSpeed KEYWORD2
  setAcceleration KEYWORD2
  setSpeed  KEYWORD2
  speed KEYWORD2
  distanceToGo  KEYWORD2
  targetPosition  KEYWORD2
  currentPosition KEYWORD2
  setCurrentPosition  KEYWORD2
  runToPosition KEYWORD2
  runSpeedToPosition  KEYWORD2
  runToNewPosition  KEYWORD2
  stop  KEYWORD2
  disableOutputs  KEYWORD2
  enableOutputs KEYWORD2
  setMinPulseWidth  KEYWORD2
  setEnablePin  KEYWORD2
  setPinsInverted KEYWORD2
  maxSpeed  KEYWORD2

  enum flag {const1, const2, ..., constN};
  By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

  // Changing default values of enum constants
  enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3,
  };
*/

Anyone who see the mistake?

The function is runToNewPosition(), not runTonewPosition().

Not sure about runToPosition(), did you intend to use runToNewPosition() there also?

This line is wrong, cmd is the enum, the variable is declared with the enum as the type.

  boolean cmd = readButtons();// + - Go Chg mode

Thanks! Late hour here.... Too late....

The silly mistake was calling mystepper and not myStepper......

Your reply made me wake up!
I'll correct the double use of cmd!
Thanks!

The use of cmd corrected.
runToPosition doesn't work, using runToNewPosition.

You may want to consider upgrading your hd44780 library.
There have been several updates & fixes to the hd44780 and hd44780_I2Cexp code since 0.9.3
Current release is 1.3.2

Thanks for telling. So far I've been all satisfied. Is there any update history included in the newer version, telling what's changed or added?

The current code is now:

#include<arduino.h>

//I2C for LCD
#include <AccelStepper.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

hd44780_I2Cexp mylcd; // declare lcd object: auto locate & config exapander chip

AccelStepper myStepper (7, 8);

// LCD geometry
#define LCD_COLS 16
#define LCD_ROWS 2

boolean state, old_state;
byte mode = 0; //// 0 = Antal sidor, 1 = vinkel per steg
int faces = 4;
unsigned long nrOfStepsPerRev = 90L * 200 * 4; // microstep 4

enum cmd {increase, decrease, Go, Chg, nobutton};
unsigned long anglePos;

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

  //1Hz 90% dutycycle
  pinMode(9, OUTPUT);                               // Set digital pin 9 (D9) to an output
  TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);  // Enable the PWM output OC1A on digital pins 9 and invert output
  TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS12);     // Set fast PWM and prescaler of 256 on timer 1
  ICR1 = 62499;                                     // Set the PWM frequency to 1Hz: 16MHz/(256 * 1Hz) - 1 = 62499
  OCR1A = 6249;                                     // Set the duty-cycle to 10%: 62499 / 10 = 6249

  pinMode(10, OUTPUT);                               // 1 Hz pulse output on digital pin 9 (D9)
  pinMode(13, OUTPUT); digitalWrite(13, LOW);// Make board LED go off
  delay(10);//allow pwm timers to start

  //  myStepper.begin();
  myStepper.setMaxSpeed(500);
  myStepper.setAcceleration(10);

#define digitalIncreaseButton 2
  pinMode (digitalIncreaseButton, OUTPUT);
#define digitalDecreaseButton 3
  pinMode (digitalDecreaseButton, OUTPUT);
#define digitalChgButton 4
  pinMode(digitalChgButton, OUTPUT);
#define digitalGoButton 5
  pinMode(digitalChgButton, OUTPUT);
#define guard1 6
#define guard2 7

#define cuttingSpeed 100 // when cutting during rotation
#define movingSpeed 2000 //when moving without cutting

  int status;
  status = mylcd.begin(LCD_COLS, LCD_ROWS);
  if (status) // non zero status means it was unsuccesful
  {
    status = -status; // convert negative status value to positive number

    // begin() failed so blink error code using the onboard LED if possible
    hd44780::fatalError(status); // does not return
  }
  mylcd.clear();

  // Print start message to the LCD
  mylcd.print("221101a Rotating");
  delay(5000);
  mylcd.print("Running");
  delay(1000);
}

enum cmd readButtons(void)
{
  if ((digitalIncreaseButton) == 0)  return (increase);
  if ((digitalDecreaseButton) == 0)  return (decrease);
  if ((digitalGoButton) == 0)        return (Go);
  if ((digitalChgButton) == 0)       return (Go);
  return (nobutton);
}

boolean guardCheck(void)
{
  if (!digitalRead(guard1) && !digitalRead(guard2))
    return (true);//Unlocked, clear to go
  else
    return false;//Locked, don't go
}

void loop()
{
  enum cmd lcmd;
  lcmd = readButtons();// + - Go Chg mode

  if (( millis() % 1000 ) > 499 )
  {
    state = true;
  }
  else
  {
    state = false;
  }

  if ( state) { //update twice per second;
    if ( mode == 0)  //number of surfaces
    {

      switch (lcmd)
      {
        case increase:
          {
            faces++;
            break;
          }
        case decrease:
          {
            faces--;
            if (faces < 2)
            {
              faces = 1; //1 rotate one turn
              myStepper.setMaxSpeed(cuttingSpeed);
            }
            else
              myStepper.setMaxSpeed(movingSpeed);
            break;
          }
        case Chg:
          {
            mode = 1; //switch to angle mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            //            myStepper.setMaxSpeed(cuttingSpeed);

            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
            myStepper.runToNewPosition(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 360 / faces;
            break;
          }
        default:
          break;
      }
      //     if (goFlag)
      //     {
      //       AccelStepper(anglePos);
      //     }
    }
    else if ( state == 1 )  // Angle per step
    {
      switch (lcmd)
      {
        case increase:
          {
            anglePos += nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            break;
          }
        case decrease:
          {
            anglePos -= nrOfStepsPerRev * 5 / 360; //Increase by 5 degrees
            //           if (faces < 1) faces = 1; //1 rotate one turn
            break;
          }
        case Chg:
          {
            mode = 0; //switch to face mode
            //          goFlag = false;//
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON

            myStepper.setMaxSpeed(movingSpeed);
            myStepper.runToNewPosition(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 5 / 360;
            break;
          }
        default:
          break;
      }
    }
    if ( guardCheck())
      myStepper.run();
  }
}// End of loop
/*
   moveTo KEYWORD2
  move  KEYWORD2
  run KEYWORD2
  runSpeed  KEYWORD2
  setMaxSpeed KEYWORD2
  setAcceleration KEYWORD2
  setSpeed  KEYWORD2
  speed KEYWORD2
  distanceToGo  KEYWORD2
  targetPosition  KEYWORD2
  currentPosition KEYWORD2
  setCurrentPosition  KEYWORD2
  runToPosition KEYWORD2
  runSpeedToPosition  KEYWORD2
  runToNewPosition  KEYWORD2
  stop  KEYWORD2
  disableOutputs  KEYWORD2
  enableOutputs KEYWORD2
  setMinPulseWidth  KEYWORD2
  setEnablePin  KEYWORD2
  setPinsInverted KEYWORD2
  maxSpeed  KEYWORD2

  enum flag {const1, const2, ..., constN};
  By default, const1 is 0, const2 is 1 and so on. You can change default values of enum elements during declaration (if necessary).

  // Changing default values of enum constants
  enum suit {
    club = 0,
    diamonds = 10,
    hearts = 20,
    spades = 3,
  };
*/

The library ships with documentation which includes a change log in the README file.
It can be accessed from the IDE GUI using the "Documentation" sketch and then clicking on the desired information.

You can also access it from the main github page here:

If you scroll down you can see the release date and change log for every revision of the library.

--- bill

Thanks a lot!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.