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
Show how your circuit is wired, including all connections, power sources, ground lines, and any other relevant components.
Clearly label each part in the schematic, especially critical components like displays, relays, contactors, sensors, and controllers.
Technical Information Links:
Provide links to the datasheets or technical documentation for each component. This information helps us understand the specifications, pin configurations, and electrical characteristics of the parts involved.
Avoid links to sales pages like Amazon unless they include complete technical details; instead, look for manufacturer or supplier datasheets.
Why This is Important:
Post an Accurate Diagnosis: An annotated schematic helps identify issues such as incorrect wiring, missing components, or potential sources of interference.
Proper Recommendations: Understanding the exact parts used allows us to suggest the right mitigation techniques, such as specific filters, grounding methods, or alternative components.
Time-Saving: Providing this information as links to technical information upfront saves time and helps avoid multiple back-and-forth exchanges, leading to faster and more effective troubleshooting.
I meant speed in pulses per second. How fast is the encoder spinning during normal operation?
I do not know either. And dont have your nano and mega setups to test. Maybe there's a mistaken connection? But from what you've shown, there's lots of sloppiness in the code that can cause undesired operation. The 9600 baud rate looks very slow compared to trying to print stuff based on interrupts. Besides trying to use delay and print inside interrupts, you are also trying to use and modify counter outside of the interrupts. You should make a copy of or changes to counter within noInterrupts()/interupts() so the value doesn't get changed mid-read/write.
With those sorts of basic issues, I'm not sure how well it actually performs on your Mega, and what suggestions to the code would make it work differently/better on the Nano. Is it a plain old Nano?
The code could be cleaned up yes i agree but the Print out works fine on the mega.
For the pulses per second it is very slow.
Iam using it to build a puzzel for an Escape Room the encoder has a gear on top that is rotated by a second gear which has a keyhole insert to be turned.
Every ISR I have ever written sets 1 bool variable. Everything else is done in the main loop. Get rid of any delay statements and replace with millis loops. Do NOT power the motor/encoder from a board pin, use a separate power supply. That is your problem, the NANO can only supply a few tens of ma.