Encoder.h Value Transfer

Hi everyone,

I’ve written some code to handle some relay switching that uses two rotary encoders to handle setting on and off periods. I’m using two encoders with push-buttons to control the four time values for the two relays by shifting the encoder values back and forth when the button is pressed.

The issue I’m running into is i can’t figure out how to maintain the set value when switching between the values. When the button is pressed both values that encoder controls update to the last setting of the encoder.

I know I’m missing something obvious but I’m stumped… Any help would be greatly appreciated

#define RELAY_1 11
#define RELAY_2 12
#define BUTTON_1 3
#define BUTTON_2 4
#include <Encoder.h>

// Variables
long positionLeft1 = -999;
long positionRight1 = -999;
long positionLeft2 = -999;
long positionRight2 = -999;
long lastPosition1 = 10;
long lastPosition2 = 10;
long lastPosition3 = 10;
long lastPosition4 = 10;
long mult1 = 0;
long mult2 = 0;
long mult3 = 0;
long mult4 = 0;
long onDelay1, onDelay2;
long offDelay1, offDelay2;
unsigned long ms;
unsigned long msLast1;
unsigned long msLast2;
boolean relayState1;
boolean relayState2;
int buttonPushCount1 = 0;
int buttonPushCount2 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;

Encoder knobLeft(5, 6);
Encoder knobRight(7, 8);

void setup() {

    pinMode(RELAY_1, OUTPUT);
    pinMode(RELAY_2, OUTPUT);
    pinMode(BUTTON_1, INPUT);
    pinMode(BUTTON_2, INPUT);
    SerialUSB.begin(9600);
    
}

void loop(void) {
  ms = millis();
  button1();
  button2();
  relayPulse1();
  relayPulse2();
        SerialUSB.print(" On Delay 1 = ");
        SerialUSB.print(onDelay1);
        SerialUSB.print(" Off Delay 1 = ");
        SerialUSB.print(offDelay1);
        SerialUSB.print(" On Delay 2 = ");
        SerialUSB.print(onDelay2);
        SerialUSB.print(" Off Delay 2 = ");
        SerialUSB.print(offDelay2);
        SerialUSB.print(" Button 1 = ");
        SerialUSB.print(buttonState1);
        SerialUSB.print(" Button 2 = ");
        SerialUSB.print(buttonState2);
        SerialUSB.println();

}

void button1(void)
{
    buttonState1 = digitalRead(BUTTON_1);

    
    if (buttonState1 != lastButtonState1) {

    if (buttonState1 == HIGH) {

      buttonPushCount1++;
      
     
    } 
    
    }
  

  lastButtonState1 = buttonState1;

  if (buttonPushCount1 % 3 == 0) {
    delayCount2();
  } else { 
   delayCount1();
  }
}

void button2(void)
{
    buttonState2 = digitalRead(BUTTON_2);
  
    if (buttonState2 != lastButtonState2) {

    if (buttonState2 == HIGH) {

      buttonPushCount2++;
     
    } 
    
    }
  

  lastButtonState2 = buttonState2;

  if (buttonPushCount2 % 3 == 0) {
    delayCount4();
  } else {
    delayCount3();
  }
}

void delayCount1(void)
{
long newLeft1;
  mult1 = positionLeft1 * 10;
  onDelay1 = constrain(mult1, 0, 10000);
  newLeft1 = knobLeft.read();
  if (newLeft1 != positionLeft1) {
    positionLeft1 = newLeft1;
    lastPosition1 = newLeft1;
  
    
  }
}

void delayCount2(void)
{
long newLeft2;

  mult2 = positionLeft2 * 10;
  offDelay1 = constrain(mult2, 0, 10000);
  newLeft2 = knobLeft.read();
  if (newLeft2 != positionLeft2) {
    positionLeft2 = newLeft2;
    lastPosition1 = newLeft2;
 
  }
}

void delayCount3(void)
{
long newRight1;

  mult3 = positionRight1 * 100;
  onDelay2 = constrain(mult3, 0, 10000);
  newRight1 = knobRight.read();
  if (newRight1 != positionRight1) {
    positionRight1 = newRight1;
    lastPosition4 = newRight1;
  }
}

void delayCount4(void)
{
long newRight2;

  mult4 = positionRight2 * 100;
  offDelay2 = constrain(mult4, 0, 10000);
  newRight2 = knobRight.read();
  if (newRight2 != positionRight2) {
    positionRight2 = newRight2;
    lastPosition3 = newRight2;
  }
}

void relayPulse1(void)
{
   if (ms - msLast1 > (relayState1 ? onDelay1 : offDelay1)) {
        digitalWrite(RELAY_1, relayState1 = !relayState1);
        msLast1 = ms;
        
    }
}

void relayPulse2(void)
{
   if (ms - msLast2 > (relayState2 ? onDelay2 : offDelay2)) {
        digitalWrite(RELAY_2, relayState2 = !relayState2);
        msLast2 = ms;
    }
}

.

  if (newLeft1 != positionLeft1) {
    positionLeft1 = newLeft1;
    lastPosition1 = newLeft1;
  }

<snip>

  if (newLeft2 != positionLeft2) {
    positionLeft2 = newLeft2;
    lastPosition1 = newLeft2;
  }

The first snippet is from delayCount1(). The second is from delayCount2(). The second one looks like a copy/paste/edit failure.

Arrays and one function would make such copy/paste/edit errors a thing of the past.

Good catch! I was erasing some of the serial code so I didn't have a mile long post and cleared that line by mistake and you caught me, I copied from the other code and missed that line.. Running code doesn't have that mistake. Although I think I'll take your suggestion for the arrays to clean up the final code.

I know the function knobLeft.write() or "Right" is what overwrites the encoder value I'm just not sure of how to set up the conditional statement to make it function properly.

I'm just not sure of how to set up the conditional statement to make it function properly.

Under what circumstances do you want to write to the encoder? In general, encoders are output devices.

The write function in the encoder library is used to set the encoder position to specific value ex. 0. What I’m trying to do is set the value to the last known value of the of the new variable when it’s toggled.

Say variable 1 is 259 and variable 2 is 654. If you’re in variable 2 and toggle to variable 1 without performing a write function variable 1 will immediately update to 654 because that’s the known position of the encoder.

So the conditional statement needs to pull the value from variable 1 and perform a write to the library but all of the ways I’ve tried so far are ending up in a looping code that locks the value. If that makes any sense.

I need the write function to only happen once during the change from one variable to the other then not execute again until it switches back. I'm thinking I might need to implement a loop counter and a while statement but I might be totally off base..

Say variable 1 is 259 and variable 2 is 654. If you're in variable 2 and toggle to variable 1

You can't be "in" a variable. You can be in some mode that writes to some variable, and switch modes.

Enough tap-dancing. You're not very good, and you look awful in a tutu. Post your code, so we have some idea what you are talking about.

Code is posted at the top.

Dustinmikl:
Code is posted at the top.

It wasn't when I told you to post your code.