Rotary Encoder's code does'nt run as expected

So I coded a program to increase a value if a movement ClockWise was detected and decrease the value
if a movement Counter Clock Wise was detected and then print the value after a change in the value was detected and some how after uploading the program and testing it with the encoder it doesn't work can any help.

Thanks for paying time to read.

Here's the code:

//------- Pin declaration-----

const int Clk = 2;
const int S0 = 3;

//------- Variables declartion-----

int x = 0;
int lastx = 0;

void setup() {

Serial.begin(9600);
pinMode(Clk, INPUT);
pinMode(S0, INPUT);
Serial.println("x value's are:"); // Serial communication test

}

void loop() {

if(clockWise() == 1){ // If a Movement of the rotary CW was detected increas value of x by 1

x = x + 1;
}

if( cclockWise() == 1){ //If a Movement of the rotary CCW was detected decreas value of x by 1

x = x - 1;

}

if(x != lastx){ // Print x value's if got changed

Serial.println(x);
lastx = x;

}

}

boolean c = digitalRead(Clk);
boolean s = digitalRead(S0);

boolean clockWise(){ // Function that return 1 or 0 if a movement CW was detect it

if(c == 0 && s == 1){// detects the turn

return(1);
delay(200);

}
else{
return(0);

}
}
boolean cclockWise(){ // Function that return 1 or 0 if a movement CCW was detect it

if(c == 1 && s == 0){

return(1);
delay(200);

}

else{
return(0);

}

}

I ahve reformatted and corrected your code.

a) the booleans c & s were not correctly defined and placed in your code.
b) you are using delay which never comes into effect, you decide what to do there

//------- Pin declaration-----
const int Clk = 2;
const int S0 = 3;

//------- Variables declaration-----
int x = 0;
int lastx = 0;
boolean c = 0; // need to declare
boolean s = 0; // these variables

void setup()
{
  Serial.begin(9600);
  pinMode(Clk, INPUT);
  pinMode(S0, INPUT);
  Serial.println("x value's are:"); // Serial communication test
}

void loop()
{
  c = digitalRead(Clk);  // these variable must be placed in the loop first off,
  s = digitalRead(S0);   // as your code depends on the contents herein

  // If a Movement of the rotary CW was detected increase value of x by 1
  if (clockWise() == 1) x = x + 1;

  // If a Movement of the rotary CCW was detected decrease value of x by 1
  if (cclockWise() == 1) x = x - 1;
  
  // Print x value's if got changed
  if (x != lastx)
  { 
    Serial.println(x);
    lastx = x;
  }
}

boolean clockWise() // Function that return 1 or 0 if a movement CW was detected
{ 
  // detects the turn
  if (c == 0 && s == 1)
  {
    return (1);
    delay(200);   // < --------------------------------- this delay wont work, as you return right before it
  }
  else return (0);
}

boolean cclockWise() // Function that return 1 or 0 if a movement CCW was detected
{
  // detects the turn
  if (c == 1 && s == 0)
  {
    return (1);
    delay(200);   // < --------------------------------- this delay wont work, as you return right before it
  }
  else return (0);
}

The code looks unlikely - what kind of encoder are you using?

Anders53:
I ahve reformatted and corrected your code.

a) the booleans c & s were not correctly defined and placed in your code.
b) you are using delay which never comes into effect, you decide what to do there

//------- Pin declaration-----

const int Clk = 2;
const int S0 = 3;

//------- Variables declaration-----
int x = 0;
int lastx = 0;
boolean c = 0; // need to declare
boolean s = 0; // these variables

void setup()
{
  Serial.begin(9600);
  pinMode(Clk, INPUT);
  pinMode(S0, INPUT);
  Serial.println("x value's are:"); // Serial communication test
}

void loop()
{
  c = digitalRead(Clk);  // these variable must be placed in the loop first off,
  s = digitalRead(S0);  // as your code depends on the contents herein

// If a Movement of the rotary CW was detected increase value of x by 1
  if (clockWise() == 1) x = x + 1;

// If a Movement of the rotary CCW was detected decrease value of x by 1
  if (cclockWise() == 1) x = x - 1;
 
  // Print x value's if got changed
  if (x != lastx)
  {
    Serial.println(x);
    lastx = x;
  }
}

boolean clockWise() // Function that return 1 or 0 if a movement CW was detected
{
  // detects the turn
  if (c == 0 && s == 1)
  {
    return (1);
    delay(200);  // < --------------------------------- this delay wont work, as you return right before it
  }
  else return (0);
}

boolean cclockWise() // Function that return 1 or 0 if a movement CCW was detected
{
  // detects the turn
  if (c == 1 && s == 0)
  {
    return (1);
    delay(200);  // < --------------------------------- this delay wont work, as you return right before it
  }
  else return (0);
}

Thanks it works now! :wink:

MarkT:
The code looks unlikely - what kind of encoder are you using?

I dunno the model but the natural state is high and it has a button built in

MarkT:
The code looks unlikely - what kind of encoder are you using?

I believe Mike is using an active encoder with build in signal detection.
They are commonly used all over in the industry.

They are not exactly cheap though ...

Anders53:
I believe Mike is using an active encoder with build in signal detection.
They are commonly used all over in the industry.

They are not exactly cheap though ...

You are right it was bloody 1.50$ !!!
But that was worth it I needed it.

Got a part number for that? Sounds way cheaper than a regular encoder and adding a LS7184N.

Bloody $1.50.
OMG its a fortune :fearful: