• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Requesting help with switch statement

Q&A Forum › Category: ESP32 › Requesting help with switch statement
0 Vote Up Vote Down
William Lucid asked 3 years ago

Gist with switch() problem and error message
Trying to code a rotary encoder sketch to make use of a switch statement.

switch(encoder_positions)  produces compiler error:  Compilation error: switch quantity not an integer I am assigning an ASCII, decimal code to each detent on the 36 detent, incremental rotary encoder.

Have been unable to find correct variable to use with the switch statement. Need to pass the current position value to the switch statement.

William

Question Tags: ESP32, Switch
8 Answers
0 Vote Up Vote Down
paulfjujo answered 3 years ago

hello,

why so complicated ?

numero : select the rotate switch

designed by “enc”

function below to get back the ascii for one rotate swich

“result”  must be declared as global ..

void rotSwitch(char numero) //Rotary encoder conversion to ASCII
{
if (encoder_positions[numero]< 27)
result= 65 + encoder_positions[numero]; 
else
result= 48 + encoder_positions[numero]-27;
}
0 Vote Up Vote Down
William Lucid answered 3 years ago

Hello paulfjujo,
Thank you!  Well done.
 
William
 

0 Vote Up Vote Down
William Lucid answered 3 years ago

Hello paulfjujo,
 
First experience working with rotary encoders.
 
I follow the method of converting letters and numbers; how does numero come into play?  How do I select rotary encoder.  Will be using four rotary encoders in project; result going to a quad alphanumeric display.
 
Have been unable display result to quad, alphanumeric display.   
 
Regards,
William
 
 

0 Vote Up Vote Down
paulfjujo answered 3 years ago

hello,

how does numero come into play?

numéro is only a parameter … it could be used to pass “enc” as parameter
 


void loop()
{
for (uint8_t enc=0; enc<sizeof(found_encoders); enc++)
{
if (found_encoders[enc] == false) continue;

int32_t new_position = encoders[enc].getEncoderPosition();
// did we move around?
if (encoder_positions[enc] != new_position) {
Serial.print("Encoder #");
Serial.print(enc);
Serial.print(" -> ");
//---------------------------------------
if (encoder_positions[enc]< 27)
result= 65 + encoder_positions[enc];
else
result= 48 + encoder_positions[enc]-27;
Serial.write(result); //ASCII Char code in decimal
encoder_positions[enc] = new_position;
// -------------------------------
// change the neopixel color, mulitply the new positiion by 4 to speed it up
encoder_pixels[enc].setPixelColor(0, Wheel((new_position*4) & 0xFF));
encoder_pixels[enc].show();
} if (! encoders[enc].digitalRead(SS_SWITCH)) {
Serial.print("Encoder #");
Serial.print(enc);
Serial.println(" pressed");
}
}

}
0 Vote Up Vote Down
William Lucid answered 3 years ago

Thsnk you paulfjujo,,
Going to give it a go; appreciate the help!
William

0 Vote Up Vote Down
William Lucid answered 3 years ago

I have a value that is the decimal equivalent of an ASCII, upper case character.
Having been unable to display using:

char letter = char(result);
display.printChar(letter, 0);
//———————————————
Have also tried:  display.printChar(char(result), 0);

Code compiles; encoder_position[enc] value is captured, as well as new_position.  result is getting a value.  Still no display on digit 0 of display.  Serial.println(result); produces 84, Serial.write(result); produces T.

Gist of non-displaying code

William

0 Vote Up Vote Down
paulfjujo answered 3 years ago

hello,

Serial.println(result); //produces ascii value=84,

Serial.write(result);// produces ascii ‘T’. ..OK, Normal !

try with :
char letter =(char)result; // Cast int value (16bits) into a char value (8 bits)
you can also use :

display.printChar((char)result, 0); // i suppose result is numerique ‘0’ to ‘9’
display.printChar(‘O’, 1); // is it ‘O’ letter or zero value ‘0’ ?
display.printChar(‘O’, 2);
display.printChar(‘O’, 3);

0 Vote Up Vote Down
William Lucid answered 3 years ago

Thank you paulfjujo!
 
Best Regards,
William
 
 

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.