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
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;
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.
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:
}
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...
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
}
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...