I thought I grasped the switch case function.
When I press the button the I would expect it to count to 3 then reset it back to case 1 which equals 1.
But this is what's happening.
On Power up inputCounters[0] = 1 which is case 1 and set the Dac_multiplier to 1, increment by 1(correct)
Press button once = inputCounters[0] = 2 which is case 2 and set the Dac_multiplier to 10 increment by 10(correct)
Press button twice = inputCounters[0] = 3 which is case 3 and set the Dac_multiplier to 50, increment by 50correct)
Press button third time = inputCounters[0] = 1 which is case1 and set the Dac_multiplier to 50(wrong)
I thought with it the inputCounters[0] displaying 1 I thought this should be case 1 increment by 1 again. But the Dac_multiplier goes back to 50 and not back to 1 as expected ?
Here is my code that I'm at, I know I must be doing something wrong
#include <Rotary.h> // Rotary encoder: https://github.com/brianlow/Rotary
#include <Wire.h>
#include <TimedAction.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7); //0x27 is the default address of the LCD with I2C bus module
#define stepPin3 3 // Set 'Step' rotary encoder pins
#define stepPin4 2
long unsigned int Dac_volt_Chanel1 = 1060; // Set initial Dac_volt_Chanel1uency.
unsigned long Dac_multiplier = 1;
unsigned long Dac_multiplier1 = 1;
const int numOfInputs = 8; //set Max inputs
const int inputPins[numOfInputs] = {4, 17, 23, 25, 27, 29, 31, 33};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int inputCounters[numOfInputs];
long lastDebounceTime[numOfInputs] = {0, 0, 0, 0, 0, 0, 0, 0};
long debounceDelay = 50;//debounce
Rotary Volts_Rotary = Rotary(stepPin3, stepPin4); // Rotary encoder for frequency connects to interrupt pins
void setup() {
Serial.begin(9600);
///count through inputpins
for (int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
Volts_Rotary.begin();
pinMode(stepPin3, INPUT_PULLUP); // Pins for step rotary encoder
pinMode(stepPin4, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(stepPin3), Volts_encoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(stepPin4), Volts_encoder, CHANGE);
lcd.begin(20, 4);
lcd.setBacklightPin(3, POSITIVE); // BL, BL_POL
lcd.setBacklight(HIGH); //set LCD backlight on
lcd.clear();
}
void loop() {
setInputFlags();
resolveInputFlags();
lcd.setCursor(0, 0);
lcd.print("DAC VAL ");
lcd.setCursor(0, 1);
lcd.print(Dac_volt_Chanel1);//DAC8574_Ch2_Val
lcd.setCursor(0, 2);
lcd.print("SET DM ");
lcd.setCursor(0, 3);
lcd.print(" : ");
lcd.print(Dac_multiplier);
lcd.print(": ");
lcd.print(inputCounters[0]);
lcd.print(" ");
}
void inputAction(int input) {
//ROTARY ENCODER BUTTON 1 PRESS
if (input == 0) {
// Input Toggle Logic
inputCounters[0]++;
getStep();
if (inputCounters[0] > 2) {// I've tried chaning this to 3,4
inputCounters[0] = 1;
}
}
//BUTTON 2 PRESS
//BUTTON 3 PRESS
//BUTTON 4 PRESS
//BUTTON 5 PRESS
//BUTTON 6 PRESS
//BUTTON 7 PRESS
//BUTTON 8 PRESS
}
void getStep() {
switch (inputCounters[0]) {
case 1: Dac_multiplier = 1; break;
case 2: Dac_multiplier = 10; break;
case 3: Dac_multiplier = 50; break;
// case 4: Dac_multiplier = 100; break;
}
}
void Volts_encoder() {
unsigned int result = Volts_Rotary.process();
///Rotary encoder 1 for setting the volts
if (result) {
if (result == DIR_CW) {
if ((Dac_volt_Chanel1 + Dac_multiplier) <= 4095) Dac_volt_Chanel1 += Dac_multiplier;
} else {
if ((Dac_volt_Chanel1 - Dac_multiplier) >= 1060) Dac_volt_Chanel1 -= Dac_multiplier;
}
if (Dac_volt_Chanel1 <= 1060) Dac_volt_Chanel1 = 1060;
if (Dac_volt_Chanel1 >= 4095) Dac_volt_Chanel1 = 4095;
}
}
void setInputFlags() {
for (int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for (int i = 0; i < numOfInputs; i++) {
if (inputFlags[i] == HIGH) {
inputAction(i);
inputFlags[i] = LOW;
}
}
}