ClickEncoder library, switch case proper code

Hi,

I am using ClickEncoder library and modifying the switch case as below, but arduino fails to compile.

This is original code from library example:

 ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    #define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      VERBOSECASE(ClickEncoder::Pressed);
      VERBOSECASE(ClickEncoder::Held)
      VERBOSECASE(ClickEncoder::Released)
      VERBOSECASE(ClickEncoder::Clicked)
      case ClickEncoder::DoubleClicked:
          Serial.println("ClickEncoder::DoubleClicked");
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled")

...and this one is my modified :

ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    //#define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      case ClickEncoder::Pressed:
          bool Pressed=1; 
          Serial.print("Pressed");
          break;
      case ClickEncoder::Held: 
          bool Held=1; 
          Serial.print("Held");
          break;
      case ClickEncoder::Released: 
          bool Released=1; 
          Serial.print("Released");
          break;
      case ClickEncoder::Clicked: 
          bool Clicked=1; 
          Serial.print("Clicked");
          break;
      case (ClickEncoder::DoubleClicked)
          Serial.println("ClickEncoder::DoubleClicked"); bool DoubleClicked=1;
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
          break;
    }
    bool Pressed = 0;
    bool Held = 0;
    bool Released = 0;
    bool Clicked = 0;
    bool DoubleClicked = 0;
  }

arduino 1.6.5r2 error is: jump to case label [-fpermissive]
on this line:
case (ClickEncoder::DoubleClicked)

There is more to the message than your synopsis. Post the COMPLETE message.

Post the sketch, too.

Hi,

Here is original code, from ClickEncoder example:

#define WITH_LCD 1

#include <ClickEncoder.h>
#include <TimerOne.h>

#ifdef WITH_LCD
#include <LiquidCrystal.h>

#define LCD_RS       8
#define LCD_RW       9
#define LCD_EN      10
#define LCD_D4       4
#define LCD_D5       5
#define LCD_D6       6
#define LCD_D7       7

#define LCD_CHARS   20
#define LCD_LINES    4

LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#endif

ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}

#ifdef WITH_LCD
void displayAccelerationStatus() {
  lcd.setCursor(0, 1);  
  lcd.print("Acceleration ");
  lcd.print(encoder->getAccelerationEnabled() ? "on " : "off");
}
#endif

void setup() {
  Serial.begin(9600);
  encoder = new ClickEncoder(A1, A0, A2);

#ifdef WITH_LCD
  lcd.begin(LCD_CHARS, LCD_LINES);
  lcd.clear();
  displayAccelerationStatus();
#endif

  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = -1;
}

void loop() {  
  value += encoder->getValue();
  
  if (value != last) {
    last = value;
    Serial.print("Encoder Value: ");
    Serial.println(value);
#ifdef WITH_LCD
    lcd.setCursor(0, 0);
    lcd.print("         ");
    lcd.setCursor(0, 0);
    lcd.print(value);
#endif
  }
  
  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    #define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      VERBOSECASE(ClickEncoder::Pressed);
      VERBOSECASE(ClickEncoder::Held)
      VERBOSECASE(ClickEncoder::Released)
      VERBOSECASE(ClickEncoder::Clicked)
      case ClickEncoder::DoubleClicked:
          Serial.println("ClickEncoder::DoubleClicked");
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
#ifdef WITH_LCD
          displayAccelerationStatus();
#endif
        break;
    }
  }    
}

...and here is my code:

#include <ClickEncoder.h>
#include <TimerOne.h>

ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}


void setup() {

  Serial.begin(9600);
  encoder = new ClickEncoder(2, 3, 4, 4 );

  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = -1;
}

void loop() {  
  value += encoder->getValue();
  
  if (value != last) {
    Serial.print("Encoder Value: ");
    Serial.println(value);

    if (last > value){
      bool Decreasing = 1;
      Serial.println("Decreasing");
    }
    else if (last < value){
      bool Increasing = 1;
      Serial.println("Increasing");
    }
    last = value; 
  }
  else if (value = last){
    bool Decreasing = 0;
    bool Increasing = 0;
    
  }
  
  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    //#define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      case ClickEncoder::Pressed:
          bool Pressed=1; 
          Serial.print("Pressed");
          break;
      case ClickEncoder::Held: 
          bool Held=1; 
          Serial.print("Held");
          break;
      case ClickEncoder::Released: 
          bool Released=1; 
          Serial.print("Released");
          break;
      case ClickEncoder::Clicked: 
          bool Clicked=1; 
          Serial.print("Clicked");
          break;
      case (ClickEncoder::DoubleClicked)
          Serial.println("ClickEncoder::DoubleClicked"); bool DoubleClicked=1;
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
          break;
    }
    bool Pressed = 0;
    bool Held = 0;
    bool Released = 0;
    bool Clicked = 0;
    bool DoubleClicked = 0;
  }    
}

...and arduino 1.6.5 fails to compile it, in this line ** case (ClickEncoder::DoubleClicked)**

  else if (value = last){

It is unusual for the assignment operator to be the only operator in an if statement.

The local variables in the bodies of the if and else statements are useless, as the go out of scope immediately.

Why are there parentheses around the case value - the only one that has them appears to be the one that the compiler doesn't like. Though it's hard to be sure as you won't post the COMPLETE error message.

I didn't look at the expanded code, but perhaps

case (ClickEncoder::DoubleClicked)

Could use a trailing colon...?

Hi All,

Thanks for your help. My code is now working.

#include <ClickEncoder.h>
#include <TimerOne.h>

ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}


void setup() {

  Serial.begin(9600);
  encoder = new ClickEncoder(2, 3, 4, 4 );

  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr); 
  
  last = -1;
}

void loop() {  
  value += encoder->getValue();
  
  if (value != last) {
    Serial.print("Encoder Value: ");
    Serial.println(value);

    if (last > value){
      bool Decreasing = 1;
      Serial.println("Decreasing");
    }
    else if (last < value){
      bool Increasing = 1;
      Serial.println("Increasing");
    }
    last = value; 
  }
  else if (last = value){
    bool Decreasing = 0;
    bool Increasing = 0;
    
  }
  
  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    //#define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      /*
      VERBOSECASE(ClickEncoder::Pressed);
      VERBOSECASE(ClickEncoder::Held)
      VERBOSECASE(ClickEncoder::Released)
      VERBOSECASE(ClickEncoder::Clicked)
      */
      case ClickEncoder::Pressed:
          Serial.print("Pressed");
          break;

      case ClickEncoder::Held:
          Serial.print("Held");
          break;

      case ClickEncoder::Released:
          Serial.print("Released");
          break;

      case ClickEncoder::Clicked:
          Serial.print("Clicked");
          break;
      
      case ClickEncoder::DoubleClicked:
          Serial.println("ClickEncoder::DoubleClicked");
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
          break;
    }
    bool Pressed = 0;
    bool Held = 0;
    bool Released = 0;
    bool Clicked = 0;
    bool DoubleClicked = 0;
  }    
}

many thanks!!!

but do not know why?:wink: As PaulS said i think the statement should be:

else if (value == last){

instead of:

else if (value = last){

as: else if (value = last){ means that you're assigning 'value' with 'last', not checking if they are equal...

    bool Decreasing = 0;
    bool Increasing = 0;

    bool Pressed = 0;
    bool Held = 0;
    bool Released = 0;
    bool Clicked = 0;
    bool DoubleClicked = 0;

These statement have no function.
Create some variables and let them go out of scope (existence).