I’ve done some changes, to have a condition to the switch/case really helped to speed up things.
#include <LiquidCrystal.h>
#define ENC_A 14 //define analog pins 0 for encoder
#define ENC_B 15 //define analog pins 1 for encoder
#define ENC_PORT PINC // ***Dunno***
// select the pins used on the LCD panel
LiquidCrystal lcd(13, 12, 8, 4, 7, 2);
int pwmLevelStream1 = 8; //lightsource 1
int pwmLevelStream2 = 8; //lightsource 2
int pwmLevelStream31 = 8; //lightsource 31 (only red)
int pwmLevelStream32 = 8; //lightsource 32 (combined RGB)
int colorRGB1 = 0; // red light as default
int outputValue1; // value output to PWM#1 (analog out)
int outputValue2; // value output to PWM#2 (analog out)
int outputValue31; // value output to PWM#3 (analog out) Red
int outputValue32; // value output to PWM#3 (analog out) Combined RGB
int pushEncoderState = 0; // high / low state
int lastPushEncoderState = 0; //high or low last loop
int encoderPushed = 0; //encoderPushed is set high by pushEncoderState & lastPushEncoderState
int selectedMenu = 1; //the active menu set to 1 (default menu)
int activeMenu = 0; //the active menu on the display
int leftOrRight = 0; // counts four ticks to set toLeft or toRight
int toLeft = 0; //rather bad solution,
int toRight = 0; //need a better one!
const int pushEncoderPin = 11; // pushbutton on encoder
const int analogOutPin1 = 5; // Analog output pin that the PWM-controlled LED#1 is attached to
const int analogOutPin2 = 3; // Analog output pin that the PWM-controlled LED#2 is attached to
const int analogOutPin3 = 6; // Analog output pin that the PWM-controlled RGB-LED (GREEN) is attached to
const int analogOutPin4 = 9; // Analog output pin that the PWM-controlled RGB-LED (BLUE) is attached to
const int analogOutPin5 = 10; // Analog output pin that the PWM-controlled RGB-LED (RED) is attached to
char *StrBrightness[73] = {"0", "0,5", "1,0", "1,5", "2,0", "3,0", "4,0", "5,0", "6,0", "8,0", "10", "12", "14", "16", "18", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"};
char *StrBrightnessRGB[73] = {"0", "0,5", "1,0", "1,5", "2,0", "3,0", "4,0", "5,0", "6,0", "8,0", "10", "12", "14", "16", "18", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"};
const byte brightnessLookupRGB [] = {
0, 1, 2, 3, 5, 8, 11, 14, 17, 20, 23, 27, 31, 35, 39, 44, 49, 55, 62, 69, 77, 86, 96, 107, 120, 133, 146, 165, 185, 206, 230, 254}; // 31-value array for brightnessRGB
const byte brightnessLookup [] = {
0, 1, 2, 3, 5, 8, 11, 14, 17, 20, 23, 27, 31, 35, 39, 44, 49, 55, 62, 69, 77, 86, 96, 107, 120, 133, 146, 165, 185, 206, 230, 255}; // 31-value array for brightness
/*
code from http://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino
works like a charm, thank you Oleg!
*/
/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
{
static int8_t enc_states[] = {
0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0 };
static uint8_t old_AB = 0;
/**/
old_AB <<= 2; //remember previous state
old_AB |= ( ENC_PORT & 0x03 ); //add current state
return ( enc_states[( old_AB & 0x0f )]);
}
void setup()
{
TCCR1A = B11110001; // set fast pwm and invert pwm output for pin 9 & 10
TCCR1B = B00001011; // set fast pwm and invert pwm output for pin 9 & 10
TCCR0A = B11000011; // set fast pwm and invert pwm output for pin 6
TCCR0B = B00000011; // set fast pwm and invert pwm output for pin 6
lcd.begin(16, 2); // start the library
pinMode(ENC_A, INPUT); //set up encoder a
digitalWrite(ENC_A, HIGH); //set up encoder a
pinMode(ENC_B, INPUT); //set up encoder b
digitalWrite(ENC_B, HIGH);//set up encoder b
pinMode(pushEncoderPin, INPUT); //set up encoder pushbutton
}
void loop()
{
static uint8_t counter = 0; //this variable will be changed by encoder input
int8_t tmpdata;
tmpdata = read_encoder();
if(tmpdata)
{
if(tmpdata == 1)
{
leftOrRight++;
}
if (leftOrRight == 4)
{
leftOrRight = 0;
toRight = 1; // knob turned CCW
}
if (tmpdata == -1)
{
leftOrRight--;
}
if (leftOrRight == -4)
{
leftOrRight = 0;
toLeft = 1; // knob turned CCW
}
}
pushEncoderState = digitalRead(pushEncoderPin); // read state for encoder push button
if (pushEncoderState != lastPushEncoderState) // has the state changed?
{
if (pushEncoderState == HIGH) { // if state is HIGH set
encoderPushed = 1; //encoderPushed to 1
}
}
[continues in next message]