0

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;
}
asked Feb 24, 2018 at 12:17
4
  • My program: is hard to read formatted like that Commented Feb 24, 2018 at 12:19
  • 1
    almost every Arduino example uses prints to Serial Monitor Commented Feb 24, 2018 at 12:21
  • I searched it up ... what did you search for? Commented Feb 24, 2018 at 18:27
  • Just a few comments on your code: 1. You can remove counter, as you never use it. 2. aState and rotationDirection 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 do angle++, and it decreases when you do angle--. Commented Feb 26, 2018 at 9:42

1 Answer 1

2

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

answered Feb 24, 2018 at 12:25
6
  • Nothing's happening. My question is how to see the graph of the pulse in the serial monitor? Commented Feb 24, 2018 at 12:56
  • Serial Plotter in IDE? Commented Feb 24, 2018 at 13:17
  • Used it but still nothing happens. Commented 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? Commented Feb 24, 2018 at 14:41
  • 1
    If 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/…) @user43154 Commented Feb 24, 2018 at 14:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.