user77878: I am reading a value from an encoder "volume knob" and I have a function called void fn_on were I am having sum int to zero and counting until it reaches the set value of volume. I tried posting to serial but my sum counter does not work. I want my relay to run "volume" number of times until sum > volume in my function "fn_on" I believe I am not storing or updating my volume correctly. Thank you for taking the time to look through this.
#include <LiquidCrystal_I2C.h>
#include <Rotary.h>
#include <LCD_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

/* Rotary Encoder - Polling Example

The circuit:
  encoder pin A to Arduino pin 2
  encoder pin B to Arduino pin 3
  encoder ground pin to ground (GND)
*/
#define PINA 2 
#define PINB 3 
#define RelayPin 8 

const char PUSHB = 5;
bool pressed = false;
long volume = 0; // this may not set the volumue

Rotary r = Rotary(2, 3);

void setup()
{
    Serial.begin(115200); // setup Pin modes
    pinMode(PUSHB, INPUT);
    r.begin(true);
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 3);
    lcd.print(volume);
    digitalWrite(PINA, HIGH); // enable pull-ups
    digitalWrite(PINB, HIGH);
    // digitalWrite (PUSHB, HIGH); 
    pinMode(RelayPin, OUTPUT); // Relay Pin ini
}

void loop()
{
    bool currentState = digitalRead(PUSHB);

    if (currentState == pressed)
    {
        Serial.println("Pressed");

        while (digitalRead(PUSHB) == pressed)
        {
            // do nothing 
        }
    }

    unsigned char result = r.process();

    if (result)
    {
        // Serial.println(result == DIR_CW ? "Right" : "Left"); 
        if (result == DIR_CCW)
        {
            volume = volume - 1000; // change ++ to mulpes of 1000
            Serial.println(volume);
            lcd.setCursor(0, 3);
            lcd.print(volume);
            lcd.print("                                ");

            int sum = 0;
            for (sum = 1; sum < volume; sum++ && currentState = true)
            {
                lcd.setCursor(0, 2);
                lcd.print(sum);
                digitalWrite(RelayPin, LOW); // run test
                delay(3000);
                digitalWrite(RelayPin, HIGH); // run test
                delay(3000);
                digitalWrite(RelayPin, LOW); // run test
                delay(3000);
            }
        }
        else
        {
            volume = volume + 1000; // change volume++ to mulpes of 1000
            // initialize display_menu to 1st line
            lcd.setCursor(0, 3);
            lcd.print(volume);
            lcd.print("                                 ");
        }
    }
}


user18934: I believe the issue is that you're setting your encoder pins high with digitalWrite().  Those pins need to be configured as inputs for the interrupts to work correctly.  This will be handled by the encoder library when you call r.begin(true).  In fact, true in that call tells the library to enable the pullups for you.
Removing those lines should fix your issue.

77878: <p>I am reading a value from an encoder &quot;volume knob&quot; and I have a function called void fn_on were I am having sum int to zero and counting until it reaches the set value of volume. I tried posting to serial but my sum counter does not work. I want my relay to run &quot;volume&quot; number of times until sum &gt; volume in my function &quot;fn_on&quot; I believe I am not storing or updating my volume correctly. Thank you for taking the time to look through this.</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;LiquidCrystal_I2C.h&gt;
#include &lt;Rotary.h&gt;
#include &lt;LCD_I2C.h&gt;

LiquidCrystal_I2C lcd(0x27, 20, 4);

/* Rotary Encoder - Polling Example

The circuit:
  encoder pin A to Arduino pin 2
  encoder pin B to Arduino pin 3
  encoder ground pin to ground (GND)
*/
#define PINA 2 
#define PINB 3 
#define RelayPin 8 

const char PUSHB = 5;
bool pressed = false;
long volume = 0; // this may not set the volumue

Rotary r = Rotary(2, 3);

void setup()
{
    Serial.begin(115200); // setup Pin modes
    pinMode(PUSHB, INPUT);
    r.begin(true);
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0, 3);
    lcd.print(volume);
    digitalWrite(PINA, HIGH); // enable pull-ups
    digitalWrite(PINB, HIGH);
    // digitalWrite (PUSHB, HIGH); 
    pinMode(RelayPin, OUTPUT); // Relay Pin ini
}

void loop()
{
    bool currentState = digitalRead(PUSHB);

    if (currentState == pressed)
    {
        Serial.println(&quot;Pressed&quot;);

        while (digitalRead(PUSHB) == pressed)
        {
            // do nothing 
        }
    }

    unsigned char result = r.process();

    if (result)
    {
        // Serial.println(result == DIR_CW ? &quot;Right&quot; : &quot;Left&quot;); 
        if (result == DIR_CCW)
        {
            volume = volume - 1000; // change ++ to mulpes of 1000
            Serial.println(volume);
            lcd.setCursor(0, 3);
            lcd.print(volume);
            lcd.print(&quot;                                &quot;);

            int sum = 0;
            for (sum = 1; sum &lt; volume; sum++ &amp;&amp; currentState = true)
            {
                lcd.setCursor(0, 2);
                lcd.print(sum);
                digitalWrite(RelayPin, LOW); // run test
                delay(3000);
                digitalWrite(RelayPin, HIGH); // run test
                delay(3000);
                digitalWrite(RelayPin, LOW); // run test
                delay(3000);
            }
        }
        else
        {
            volume = volume + 1000; // change volume++ to mulpes of 1000
            // initialize display_menu to 1st line
            lcd.setCursor(0, 3);
            lcd.print(volume);
            lcd.print(&quot;                                 &quot;);
        }
    }
}
</code></pre>
 18934: <p>I believe the issue is that you're setting your encoder pins high with <code>digitalWrite()</code>.  Those pins need to be configured as inputs for the interrupts to work correctly.  This will be handled by the encoder library when you call <code>r.begin(true)</code>.  In fact, <code>true</code> in that call tells the library to enable the pullups for you.</p>
<p>Removing those lines should fix your issue.</p>
