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;

}
user43154
  • 9
  • 3
  • My program: is hard to read formatted like that – Jaromanda X Feb 24 '18 at 12:19
  • 1
    almost every Arduino example uses prints to Serial Monitor – Juraj Feb 24 '18 at 12:21
  • I searched it up ... what did you search for? – jsotola Feb 24 '18 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--. – Edgar Bonet Feb 26 '18 at 09:42

1 Answers1

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

Juraj
  • 18,037
  • 4
  • 29
  • 49
  • Nothing's happening. My question is how to see the graph of the pulse in the serial monitor? – user43154 Feb 24 '18 at 12:56
  • Serial Plotter in IDE? – Juraj Feb 24 '18 at 13:17
  • Used it but still nothing happens. – user43154 Feb 24 '18 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 Feb 24 '18 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))); (https://www.norwegiancreations.com/2016/01/tutorial-multiple-values-in-the-arduino-ide-serial-plotter/) @user43154 – Maximilian Gerhardt Feb 24 '18 at 14:46
  • Yes, that worked thanks. Further information I'll search first by myself. Thanks! – user43154 Feb 24 '18 at 15:19