Hello to all.
I want to create a program to manage 2 incremental rotary encoders and a stepper motor, but now I have left the stepper motor aside because I want to solve this problem first.
I am using an arduino Mega, even if I used the Uno before, but since I thought the program was not working due to the few Interrupt pins, I switched to the Mega (photos of the Arduino Uno pins: https: //i.stack .imgur.com / dVkQU.jpg and the Arduino Mega pin photos: https://arduino-projekte.info/wp-content/uploads/2017/03/mega_pinout.png)
I bought the two encoders I'm using on Amazon: https://www.amazon.it/Wisamic-incrementale-tensione-alificazione-millimetri/dp/B015GYY7XU/ref=sr_1_6?__mk_it_IT=ÅMÅ % C5% BD% C3% 95% C3% 91 & dchild = 1 & keywords = encoder & qid = 1605898632 & sr = 8-6
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(3, INPUT_PULLUP); // internalเป็น pullup input pin 3
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
}
void loop() {
// Send the value of counter
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter++;
}else{
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(2)==LOW) {
counter--;
}else{
counter++;
}
}
This is the code I had copied and pasted from a guy who used an Arduino Uno and the same encoder to see the counter increase and decrease as I turned the encoder shaft.
And so far everything is fine.
Then I realized that on the Arduino Uno I couldn't put another encoder, if I'm wrong PLEASE tell me, and so I switched to the Mega.
#define PinBiancoEncoder1 20
#define PinVerdeEncoder1 21
#define PinBiancoEncoder2 18
#define PinVerdeEncoder2 19
volatile long temp, counter = 0; //This variable will increase or decrease depending on the rotation of encoder
volatile long temp1, counter1 = 0; //This variable will increase or decrease depending on the rotation of encoder
void setup() {
Serial.begin (9600);
pinMode(PinVerdeEncoder1, INPUT_PULLUP);
pinMode(PinBiancoEncoder1, INPUT_PULLUP);
pinMode(PinVerdeEncoder2, INPUT_PULLUP);
pinMode(PinBiancoEncoder2, INPUT_PULLUP);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(4, ai2, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(5, ai3, RISING);
}
void loop() {
// Send the value of counter
if( counter != temp ){
Serial.println (counter);
temp = counter;
}
if( counter1 != temp1 ){
Serial.println (counter1);
temp1 = counter1;
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(PinBiancoEncoder1)==LOW) {
counter++;
}else{
counter--;
}
}
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(PinVerdeEncoder1)==LOW) {
counter--;
}else{
counter++;
}
}
void ai2() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(PinBiancoEncoder2)==LOW) {
counter1++;
}else{
counter1--;
}
}
void ai3() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if(digitalRead(PinVerdeEncoder2)==LOW) {
counter1--;
}else{
counter1++;
}
}
In this code I tried to put the second encoder as well, but nothing.
The code you see now is the final result of dozens and dozens of changes I made to the code, from the point of view of the pins, both the encoder pins and the attachInterrupt pins.
I looked on the internet about dozens of sites about attachInterrupt and how to program more than one encoder on the same program, BUT NOTHING.
I can't get mad guys and I don't even want to think about how many hours I spent trying and making mistakes, without finding a solution.
I repeat, I have 2 incremental rotary encoders and an Arduino Mega / Uno, you decide which one to use, and my goal is to have and read on the Serial Monitor the values, COUNTER, of each of the two encoders.
OR, instead of reading the values of each of the two encoders, it would be fine for me if the counter is the same, so that if one encoder increases and the other decreases there is the difference between the values of the two encoders.
I ask you to give me a hand if possible, I can not get out of this situation.
THANK YOU SO MUCH GUYS for your attention