Enum trouble again

Already got help from forum regarding enum. Don't find that thread therefor opening a new thread.
I don't get it, what the error message tell.
Anybody that see my mistake and have a suggestion?

The 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

AccelStepper myStepper (7, 8);

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

enum mode {Sides, Angle };
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()
{
  unsigned long lastMillis;
  enum cmd lcmd;
  lcmd = readButtons();// Read button inputs if any    + - Go Chg mode

  if ( millis() - lastMillis > 200 )
  {
    lastMillis = millis();
    if ( mode == Sides )  //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 == Angle; //switch to angle mode
            myStepper.setMaxSpeed(movingSpeed);// used i angle mode
            break;
          }
        case Go:
          {
            while (!guardCheck());;//DON*T GO IF LOCKS ARE ON
            myStepper.runToNewPosition(anglePos);//Blocking until done
            anglePos += nrOfStepsPerRev * 360 / faces;
            break;
          }
        default:
          break;
      }//end of switch
    }//end of "if (mode == Sides)"
    else if ( mode == Angle )  // 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 == Sides; //switch to sides 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,
  };
*/

Error message:

C:\Users\Stefan\Documents\Arduino\Rotation_20221101a\Rotation_20221101a.ino: In function 'void loop()':
Rotation_20221101a:100:15: error: expected primary-expression before '==' token
     if ( mode == Sides )  //number of surfaces
               ^~
Rotation_20221101a:123:18: error: expected unqualified-id before '==' token
             mode == Angle; //switch to angle mode
                  ^~
Rotation_20221101a:138:20: error: expected primary-expression before '==' token
     else if ( mode == Angle )  // Angle per step
                    ^~
Rotation_20221101a:155:18: error: expected unqualified-id before '==' token
             mode == Sides; //switch to sides mode
                  ^~
exit status 1
expected primary-expression before '==' token

mode is an enum... so effectively a type of variable, with a specific set of values.

You need to declare a variable of that "type".

For example

mode myMode;

You can then use myMode in your code.

Another example...

enum direction {up, down, left, right};

direction dir;


void setup()
{
  dir = down;
}


void loop()
{}

Thanks a lot! That pulled out the plug. Somehow I managed to do it for enum cmd. That was yesterday... Today, no.....
The simple way is to use #define member 1 =1, member2 = 2...
Just tried to do better.

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