? Has there been a change in C++ programming logic

Hi all,

I just wired up a SR1230rotary encoder to a nano and it works, using the below code. (arduino 2.3.3, linux appimage on Fedora40)

Wondering how it's calling the encoder routine here, as the output value does change with rotation of the encoder. All the void loop () appears to be doing is printing out the value, doesn't appear to be calling the subroutine for the value - has there been a change in logic that I'm not aware of.

Glad it works, but would like to understand how the subroutine is being called, if that makes sense.

Purchased the encoder from here:
https://www.electusdistribution.com.au/dbdocument/746336/sr1230_manual_26279.pdf , and figured their instructions would be a good starting point.

used INPUT_PULLUP without the capacitors to see if it worked.

Any advice you can offer would be greatly appreciated.

A

#define enc_a 2
#define enc_b 3
volatile int encoderValue = 0;
void setup() {
Serial.begin(9600);
pinMode(enc_a, INPUT_PULLUP);
pinMode(enc_b, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(enc_a), encoder, FALLING);
}
void loop() {
Serial.println(encoderValue);
delay(100);
}
void encoder() {
if (digitalRead(enc_a) == digitalRead(enc_b)) {
encoderValue++;
}
else {
encoderValue--;
}
}

Background,
I'm a classically trained musician as well as an engineer.

Setting up a metronome on Arduino to mimic the conductor's baton (two servos crossed at 90Ā°) as there's no way I can hear a regular metronome while I'm practising.

Going to set up a rotary encoder to adjust tempo, and two rotary switches for time signature (eg: 12/3, 12/8, etc...)

āžœ you enable interrupts on pin #2 (when there is a falling front) which will call the encoder() function which is in charge of modifying the value that gets printed in the loop

1 Like

aah - yep.

Long days - thanks, Jackson. Makes perfect sense now.

I'll leave this up so others can find the answer too - missed it when I went over the code. Brain's in 4000 places at the moment.

Note that this member is not called Jackson. That is his status on the forum. Status is measured by the number of posts he has made. They are all (now) named after famous people in the development of electricity and electronics.

Note there is a God level you can get with, as I remember 1000 posts, left over from the early days of the forum.

1 Like

I always thought it was neat that Tesla outranked God here.

Where is that not true?

a7

"Tesla was the electric Jesus."
https://youtu.be/3gOR91oentQ?si=J3O1dDCT0VWlqBIu