Two rotary encoders one output

Hi

I need two rotary encoders to dim one led, the one should start where the other one stopped. I have managed to get one to work. how do I add the other. any help would be appreciated.

#define outputA 6
#define outputB 7
int counter = 0;
int aState;
int aLastState;
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is

void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);

Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}

counter = constrain(counter, 0, 255);
// limits range of sensor values to between 10 and 150

// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = counter;

Serial.print("Position: ");
Serial.println(counter);

}
aLastState = aState; // Updates the previous state of the outputA with the current state
}

I need two rotary encoders to dim one led, the one should start where the other one stopped.

Can you explain what that means? I'm not getting the picture here.

Adding a second encoder should not be too difficult. Using the data might be a different store.

   counter = constrain(counter, 0, 255);
// limits range of sensor values to between 10 and 150
 
    // set the brightness of pin 9:
  analogWrite(led, brightness);
 
  // change the brightness for next time through the loop:
  brightness = counter;

Using the value of brightness and then assigning brightness a value does not make sense.

You have one light, with two rotary dimmers at different locations, say the light is for the stair case, one rotary encoder downstairs, the other upstairs. If you dim the light to 50% downstairs, you must be able to dim from 50% up or down, upstairs. hope it makes sense.

You have one light, with two rotary dimmers at different locations, say the light is for the stair case, one rotary encoder downstairs, the other upstairs. If you dim the light to 50% downstairs, you must be able to dim from 50% up or down, upstairs. hope it makes sense.

The incremental rotary encoders have no zero point and no end stops. They just increment or decrement the count as they go around and around. The counter value is used to write the led brightness and is held in the sketch. It should not matter which encoder changes the counter value.

Thats what I thought, but for someone just starting with arduino, its not that easy, any chance you can help me with the coding?

Give it a try and come back here with revised code. Be sure to use the code tags found at the <> in the tool bar. Place your code in between the tags like this [code]your code here[/code] and it will show in a box like this

your code here

Simply duplicate your code with a second encoder, but have both of them change the same variable count

Choose an identifier for the two encoders. Perhaps upstairs and downstairs. Then every place there is variable with the A or B or a term append the prefix upstairs_ or downstairs_

This will get you started, and I'll leave you to sort out the loop().

#define upstairs_outputA 6
#define upstairs_outputB 7
#define downstairs_outputA 4  //add two new pins for second encoder
#define downstairs_outputB 5
int counter = 0;
int upstairs_aState;
int upstairs_aLastState;
int downstairs_aState;
int downstairs_aLastState;

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is

void setup() {
  pinMode (upstairs_outputA, INPUT);
  pinMode (upstairs_outputB, INPUT);

  pinMode (downstairs_outputA, INPUT);
  pinMode (downstairs_outputB, INPUT);

  Serial.begin (9600);
  // Reads the initial state of the outputA
  upstairs_aLastState = digitalRead(upstairs_outputA);
  downstairs_aLastState = digitalRead(downstairs_outputA)
}
void loop() {
  // put your main code here, to run repeatedly:
}

Thanks, will give it a try and report back after.

Thank you! Working 100%

Hi,
Instead of encoders, a pair of buttons at each location.
Each pair is UP/DOWN brightness.

KISS

Tom... :slight_smile:

rotary encoder must turn 6 full revolutions to get to 255, how can I get it to to go to 255 with less turns?

if (upstairs_aState != upstairs_aLastState)
          {     
                  if (digitalRead(upstairs_outputB) != upstairs_aState) 
                  { 
                   counter ++ ;
                  } 
                  else 
                  {
                    counter -- ;
                  }
            

           counter = constrain(counter, 0, 255);
           
           analogWrite(led, brightness);
        
           brightness = counter;

           Serial.print("Position: ");
           Serial.println(brightness);

Morne_CPT:
rotary encoder must turn 6 full revolutions to get to 255

That fully depends on the encoder....

But uhm, to keep it KISS. Library? And simply parallel the encoders if they need to do the same. Only gives you trouble when you operate both at the same time. But that would also give you trouble when you connect and read them separate...

Hi,
If you are going to use encoders, make each encoder count, increase or decrease the PWM duty by 10, instead of 1.

Tom... :slight_smile:

Or better, use Gamma correction :wink:

Ok, can you please point me to where I can read how to do that.

You ask who about what?

Sorry, any one will work, the easiest one would be the best as I am very new at this. Count or gamma correction. :o

Gama is harder but gives you better results: RGB LEDs: How To Master Gamma And Hue For Perfect Brightness | Hackaday

But you didn't post the full code so I will only post a part as well:
count+= 10 maybe?

But if you want simple, why not grab a library? And parallel the rotaries?

Would rather stick to what I have now, if it can work that is. Attaches is the full code. Please show me where to add the "count+= 10"

Thanks for the help so far!

#define upstairs_outputA 6
#define upstairs_outputB 7
#define downstairs_outputA 4  //add two new pins for second encoder
#define downstairs_outputB 5
int x = 10;
int counter = 0;
int upstairs_aState;
int upstairs_aLastState;
int downstairs_aState;
int downstairs_aLastState;
int led = 9;                 // the pin that the LED is attached to
int brightness = 0;          // how bright the LED is
 
 void setup() { 
   pinMode (upstairs_outputA,INPUT);
   pinMode (upstairs_outputB,INPUT);
   
   Serial.begin (9600);
   // Reads the initial state of the outputA
   upstairs_aLastState = digitalRead(upstairs_outputA);   
 } 
 
 
 
 void loop() { 
   
   upstairs_aState = digitalRead(upstairs_outputA);
   downstairs_aState = digitalRead(downstairs_outputA);
   

   if (upstairs_aState != upstairs_aLastState)
          {     
                  if (digitalRead(upstairs_outputB) != upstairs_aState) 
                  { 
                   counter ++ ;
                  } 
                  else 
                  {
                    counter -- ;
                  }
            

           counter = constrain(counter, 0, 255);
         
           analogWrite(led, brightness);
        
           brightness = counter;

           Serial.print("Position: ");
           Serial.println(brightness);
           
           
           upstairs_aLastState = upstairs_aState; // Updates the previous state of the outputA with the current state
          } 
           
           
    if (downstairs_aState != downstairs_aLastState)
          {     
                  if (digitalRead(downstairs_outputB) != downstairs_aState) 
                  { 
                   counter ++;
                  } 
                  else 
                  {
                   counter --;
                  }
             
           counter = constrain(counter, 0, 255);
       
           analogWrite(led, brightness);
        
           brightness = counter;

           Serial.print("Position: ");
           Serial.println(brightness);
           
           
           downstairs_aLastState = downstairs_aState; // Updates the previous state of the outputA with the current state
          }

What about where you now do a plus 1?

And still, first using brightness and then setting brightness is a bit weird. It's like eating a sandwich and directly after that make a new sandwich for the next day...

Ok, did a " counter +=5" and now have one rev to get 255.

How should I fix the brightness thing? When I remove "brightness = counter;" I dont get any value in serial print and led does not work.