Relay not triggering from loop function. Variable “volume” not updating after encoder is turned to value?

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 <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 PUSHB 5
#define RelayPin 8
const int sum = 0;

#include <Rotary.h>

long volume = 0; // this may not set the volumue
Rotary r = Rotary(2, 3);

void setup() {

Serial.begin(9600);
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() {
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

    lcd.setCursor(0, 3);
    lcd.print(volume);
    lcd.print("                                ");
   
} else {
 
    volume = volume + 1000;                   // change volume++ to mulpes of 1000
    lcd.setCursor(0, 3);
    lcd.print(volume);
         //initialize display_menu to 1st line
               //initialize display_menu to 1st line
     

    lcd.setCursor(0, 3);
    lcd.print(volume);
    lcd.print("                                 ");
  
 }

}
}

void fn_on() {
int sum =0;

while(sum < volume){
sum = sum + 1;
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);
}

}

Where do you call fn_on?

@calaard, your topic has been moved to a more suitable location on the forum. InstallationandTroubleshooting is not for problems with your project; see About the Installation & Troubleshooting category.

Please read https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966and apply what you've read/learned about code tags to the code in your post.

Hi

sum was defined with global constant,
and within the function "void fn_on() " you again defined sum local.

RV mineirin

I changed some of the logic

Okay I am trying to read a value from a rotary encoder, the value that I am reading is "volume (cycles)". I want to dial the encoder to a number and set it with a push button and run a relay N number of cycles. I am using a "for loop" to run my relay once the push button is pressed and when volume is greater than one

#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
    lcd.setCursor(0, 3);
    lcd.print(volume);
         //initialize display_menu to 1st line
               //initialize display_menu to 1st line
     

    lcd.setCursor(0, 3);
    lcd.print(volume);
    lcd.print("                                 ");
  
 }
}

}

Please use </> tags for post sketchs or printout.
Correct your posts with sketches.

As already said, correct the presentation of your code by using the autoformat option in the IDE and posting it in code tags (use the </> button).

For debugging purposes put a println (sum); in after you do a lcd.print (sum); and do the same for volume.

You appear to have duplicate code for the lcd to display volume - is that intentional or simply a mistake.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.