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;
}
1 Answer 1
Add Serial.begin(115200); in setup() and use Serial.println() or Serial.print() to print to Serial.
In Arduino IDE open Serial Monitor and set baud rate to 115200.
EDIT: The question turned out to be about Serial Plotter. Maximilian Gerhardt answered in a comment :
If you want multiple data streams (graphs) you need to seperate them by a comma: Serial.println( String(digitalRead(outputA)) + "," + String(digitalRead(outputB)));
-
Nothing's happening. My question is how to see the graph of the pulse in the serial monitor?user43154– user431542018年02月24日 12:56:55 +00:00Commented Feb 24, 2018 at 12:56
-
-
Used it but still nothing happens.user43154– user431542018年02月24日 14:38:16 +00:00Commented Feb 24, 2018 at 14:38
-
Used this: Serial.println(digitalRead(outputA)); Serial.println(digitalRead(outputB)); Now I only get one of the 2. Is there a way to get both but under eachother?user43154– user431542018年02月24日 14:41:16 +00:00Commented Feb 24, 2018 at 14:41
-
1If you want multiple data streams (graphs) you need to seperate them by a comma:
Serial.println( String(digitalRead(outputA)) + "," + String(digitalRead(outputB)));
(norwegiancreations.com/2016/01/…) @user43154Maximilian Gerhardt– Maximilian Gerhardt2018年02月24日 14:46:02 +00:00Commented Feb 24, 2018 at 14:46
My program:
is hard to read formatted like thatI searched it up
... what did you search for?counter
, 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--
.