I made an program so if I turn on my rotary encoder the position and the rotation comes on a LCD screen. But I need to change it a bit up for my teacher, she wants to see the graph of the pulses on the serial monitor. I searched it up and can't find a solution? Any help?
My program:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define outputA 2
#define outputB 4
int counter = 0;
int angle = 0;
int aState;
int aLastState;
int lastAngle = 0;
String rotationDirection;
String savedRotationDirection;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
aLastState = digitalRead(outputA);
lcd.begin();
}
void loop() {
aState = digitalRead(outputA);
if (aState != aLastState){
if (digitalRead(outputB) != aState) {
counter ++;
angle ++;
}
else {
counter--;
angle --;
}
if (counter >=30 ) {
counter = 0;
}
if (angle < lastAngle){
rotationDirection = "CCW";
}
else {
if (angle > lastAngle){
rotationDirection = "CW";
}
else {
rotationDirection = savedRotationDirection;
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Position: ");
lcd.print(int(angle)*(-1));
lcd.print((char)223);
lcd.setCursor(0,1);
lcd.print("Rotation: ");
lcd.print(rotationDirection);
savedRotationDirection = rotationDirection;
lastAngle = angle;
}
aLastState = aState;
}
My program:
is hard to read formatted like that – Jaromanda X Feb 24 '18 at 12:19I searched it up
... what did you search for? – jsotola Feb 24 '18 at 18:27counter
, as you never use it. 2.aState
androtationDirection
should be local variables. 3.savedRotationDirection
serves no useful purpose: you can just remove every line in which it appears. 4.lastAngle
is not useful either: you know the angle increases when you doangle++
, and it decreases when you doangle--
. – Edgar Bonet Feb 26 '18 at 9:42