 Rotary Encoder set between two values

Hello Im coding a project and Im having a little issue. Im trying to set my encoder between two values which would be 60-120. I want it to start at 60 and stop at 120 which will cause me to turn the dial in the opposite direction which will result in the numbers dropping back down >=60
Here is my code so far

#include <Encoder.h>
int CLK=39;
int DT=40;
int SW=41;

Encoder myEnc(CLK,DT);

void setup() {

Serial.begin(9600);
Serial.println("Encoder Position");
}

long oldPosition = -999;
void loop() {

if(newPosition != oldPosition){
oldPosition = newPosition;
if(newPosition >= 121){
oldPosition=60;
newPosition=60;
myEnc.write(60);
}
Serial.println(newPosition);
}
}

As of now it'll start at 1 go to 120 loop back to 60 . It is also displaying negative numbers which is what i don't want. Any help appreciated!

Try the constrain() function:
https://www.arduino.cc/reference/en/language/functions/math/constrain/

void loop() {
if(newZ<60){myEnc.write(60);}
if(newZ>120){myEnc.write(120);}
newZ = constrain(newZ,60,120);

Serial.println(newZ);
}

Okay so that worked but now it just keeps looping and printing the current value over and over i tried setting a delay but that didn't work

Put the Serial.print inside the new/old position compare block.

Im not exactly sure what you mean by this?

As long as those values are between and including 60 and 120, it is working as expected.

What kind Arduino Board? Do you have external interrupt-capable pins available for the encoder? If so, take a look at the "SingleEncoder" example in this library: https://github.com/gfvalvo/NewEncoder

it works it just keeps looping so if 60 is displayed on the serial print it keep printing out that number.

Mega2560

OK, then the library I linked will only work with the encoder connected to these pins: 2, 3, 18, 19, 20, 21.

The code works only issue is it keeps looping whatever the current value and i cant figure out why.

Is the encoder still working? Try simply:

Still loops So, the encoder or wiring is broken? Or did you forget to twist the knob?

Start over with the basic, unmodified example.

when i twist the knob it'll change value but just starts looping that current value again

That makes sense. You do understand that the loop function loops, don't you?

It is very rapidly and repeatedly called by the hidden function main().

so i would have to create its own function? I just figured i can do it inside the loop function cause on the encoder example given in arduino its in the loop function and that works perfectly fine

void loop() {
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}

The encoder example above prints only when the encoder value has changed. Your code continuously reads and prints the encoder value.

Both approaches work "fine".

I suggest that you sit down and study the example code until you understand what each line does and why. Only then will you be in a good position to make changes to the code.

Hi jdoc,

you seem to not care about a few things that are important when writing code.

indention

void loop() {

if(newPosition != oldPosition){
oldPosition = newPosition;
if(newPosition >= 121){
oldPosition=60;
newPosition=60;
myEnc.write(60);
}
Serial.println(newPosition);
}
}

void loop() {
//too
// many
//empty lines
if(newPosition != oldPosition){
oldPosition = newPosition;
if(newPosition >= 121){
oldPosition=60;  // wrong indention
newPosition=60; // wrong indention
myEnc.write(60);
}
Serial.println(newPosition); // wrong indention and you do not know what te curly bracket is for
}
}

Your code should look like this

#include <Encoder.h>
int CLK = 39;
int DT = 40;
int SW = 41;

Encoder myEnc(CLK, DT);

void setup() {
Serial.begin(9600);
Serial.println("Encoder Position");
}

long oldPosition = -999;

void loop() {
if (newPosition != oldPosition)
{ // <<<<<<<=== opening curly bracket
oldPosition = newPosition;
if (newPosition >= 121) {
oldPosition = 60;
newPosition = 60;
myEnc.write(60);
}

Serial.println(newPosition);  //<<<===== printed only if if-condition is true
}

void loop() {
if (newZ < 60)
{
myEnc.write(60); // <<=== executed only when if-condition is true
}

if (newZ > 120)
{
myEnc.write(120); // <<=== executed only when if-condition is true
}

newZ = constrain(newZ, 60, 120); // executed EACH time

Serial.println(newZ);            // executed EACH time
}

the curly brackets { } mark start and end of a code-block

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan