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
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);
}
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);
}