Hi all,
pls move this topic if it is in the wrong section.
I have a simple Encoder counter for increase and decrease for every 100th step it sets an output nothing fancy.
The program works fine without a problem on a Arduino Mega 256 but due to its overkill on this project i have decided to move it to a nano.
But now the issue starts iam not getting any readings on the nano.
I have added a serial start up print to see if the nano is processing.
se blow my code for the MEGA does anyone have a suggestion why this is not working on the nano?
volatile unsigned int temp, counter = 0; // This variable will increase or decrease depending on the rotation of the encoder
void setup() {
Serial.begin(9600);
Serial.println("Setup complete. Starting program...");
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2 (interrupt 0 on Arduino Nano)
pinMode(3, INPUT_PULLUP); // internal pullup input pin 3 (interrupt 1 on Arduino Nano)
pinMode(7, OUTPUT); // D7 as output
pinMode(8, OUTPUT); // D8 as output
pinMode(9, INPUT_PULLUP); // D9 as input with internal pullup
// Setting up interrupt
// A rising pulse from encoder activates ai0(). AttachInterrupt 0 is DigitalPin nr 2 on Arduino Nano.
attachInterrupt(digitalPinToInterrupt(2), ai0, RISING);
// B rising pulse from encoder activates ai1(). AttachInterrupt 1 is DigitalPin nr 3 on Arduino Nano.
attachInterrupt(digitalPinToInterrupt(3), ai1, RISING);
}
void loop() {
// Send the value of counter
if (counter != temp) {
Serial.println(counter);
temp = counter;
}
// If D9 is read as HIGH, reset counter to 0
if (digitalRead(9) == LOW) {
counter = 0;
}
// If counter exceeds 14000, reset counter to 0
if (counter > 14000) {
counter = 0;
}
}
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++;
// For every 100th rising count of counter, set D7 high
if (counter % 100 == 0) {
digitalWrite(7, HIGH);
Serial.print("D7 HIGH on increment: ");
Serial.println(counter);
delay(10); // Brief delay to ensure the signal is detectable
digitalWrite(7, LOW);
}
} else {
counter--;
// For every 100th falling count of counter, set D8 high
if (counter % 100 == 0) {
digitalWrite(8, HIGH);
Serial.print("D8 HIGH on decrement: ");
Serial.println(counter);
delay(10); // Brief delay to ensure the signal is detectable
digitalWrite(8, LOW);
}
}
}
void ai1() {
// ai1 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check pin 2 to determine the direction
if (digitalRead(2) == LOW) {
counter--;
// For every 100th falling count of counter, set D8 high
if (counter % 100 == 0) {
digitalWrite(8, HIGH);
Serial.print("D8 HIGH on decrement: ");
Serial.println(counter);
delay(10); // Brief delay to ensure the signal is detectable
digitalWrite(8, LOW);
}
} else {
counter++;
// For every 100th rising count of counter, set D7 high
if (counter % 100 == 0) {
digitalWrite(7, HIGH);
Serial.print("D7 HIGH on increment: ");
Serial.println(counter);
delay(10); // Brief delay to ensure the signal is detectable
digitalWrite(7, LOW);
}
}
}
The rotary encoder is working on 5-24V and has A & B outputs for clockwise and counterclock
11 posts - 5 participants