Rotary Encoder Programming Struggle

#include <LiquidCrystal_I2C.h>;
#define UltrasonicDistance;
int analogPin= A3;
int val = 0;
#define encdir
#define inputCLK 0
#define inputDT 1
int counter = 0;
int currentStateCLK;
int previousStateCLK;
float distance = 0;

String encdir ="";

long readUltrasonicDistance(int triggerPin, int echoPin);

LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {
pinMode (inputCLK, INPUT);
pinMode (inputDT, INPUT);
Serial.begin (9600);
previousStateCLK = digitalRead(inputCLK);
lcd.init();
lcd.setBacklight(1);
//lcd.display(1);
lcd.print("Initializing");
delay(2000);
lcd.clear();
delay(1000);
lcd.print("Standby...");
delay(1000);
lcd.clear();
delay(500);

}
void loop() {
val = analogRead(analogPin);
distance = (.0104 * val) + 1.18;
lcd.print(distance);
delay(1000);
if ((distance >= 2) && (distance < 7)){
lcd.clear();
lcd.print("Material Present");
delay(2500);
}
else {
lcd.clear();
lcd.print("Material Absent");
}
delay(2500);
lcd.clear();
delay(1000);
currentStateCLK = digitalRead(inputCLK);
if(currentStateCLK != previousStateCLK){
counter--;
encdir = "CCW";
}

}

Error Message as make of Follow:

C:\Users\alecg\OneDrive\Documents\Arduino\sketch_aug13a\sketch_aug13a.ino:13:16: error: expected unqualified-id before '=' token
String (encdir ="");
^
C:\Users\alecg\OneDrive\Documents\Arduino\sketch_aug13a\sketch_aug13a.ino:13:16: error: expected ')' before '=' token
C:\Users\alecg\OneDrive\Documents\Arduino\sketch_aug13a\sketch_aug13a.ino: In function 'void loop()':
C:\Users\alecg\OneDrive\Documents\Arduino\sketch_aug13a\sketch_aug13a.ino:57:12: error: expected primary-expression before '=' token
encdir = "CCW";
^

exit status 1

Compilation error: expected unqualified-id before '=' token

Why my programming no make of work? Please help of do make work?

Please edit your post to mark it up as <CODE>

#define encdir
#define inputCLK 0
#define inputDT 1
int counter = 0;
int currentStateCLK;
int previousStateCLK;
float distance = 0;

String encdir = "";

Note that the first line of these defines encdir as empty, which then turns the last line into this nonsense:

String = "";

I don't know what you expect that to do. I think it would be best to remove it.

1 Like

You have

delay(2500);
lcd.clear();
delay(1000);
currentStateCLK = digitalRead(inputCLK);
if(currentStateCLK != previousStateCLK){

You never read the other encoder input inputDT.

And if you are planning to do this by polling, you have to look at the encoder inputs way faster than every 3.5 seconds.

a7

1 Like

You also don't want to use pins 0 and 1 since those are used for Serial communication with your PC during programming and a great benefit when trying to debug your code. I would change your wiring and your pin numbers to some other non-used pins.

1 Like

Hi @some_gei

welcome to the arduino-forum.

english seems to be not your native language. And your english-knowledge seems to be limited.
You should improve your postings by using the deepl-translation-service
Write down everyhing in your mother-language and let do deepL the translation

best regards Stefan