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() {
long newPosition = myEnc.read();
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!
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
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() {
long newPosition = myEnc.read();
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.
void loop() {
//too
// many
//empty lines
long newPosition = myEnc.read();
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() {
long newPosition = myEnc.read();
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
}
in your second code
void loop() {
int newZ = myEnc.read();
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
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.