3

I'm trying to plot the value of the pwm that increases (every 2 sec) then decreases to compare it to the value of an encoder. I was expecting a continued value (like a step ) during 2 seconds but instead I have a triangle signal. Here is my code

const int pwmG = 2;
const int DirG = 8 ;
int a =0;


#define encoder1_CHA 18  //Left encoder
#define pin_channelB_left 6
int b =0;

void setup() {
  Serial.begin(9600);
  pinMode(pwmG,OUTPUT) ; 
  pinMode(DirG,OUTPUT) ; 
  attachInterrupt(digitalPinToInterrupt(encoder1_CHA),interruptUp,RISING);
  
  }
  
void interruptUp(){
  b = !b;
  
}

void loop() {
  Serial.println(b);
  //Serial.print(" ");
  pwm();
  
  //Serial.println(a);
  

 
  //Serial.println(b);
}

void pwm(){
   for (int i =0; i< 10; i++){
      a = a +5;
      analogWrite(pwmG, a);
      
      Serial.println(a);
      delay(200);
      }
      //Serial.println(b);
      
  for (int i =0; i< 10; i++){
    a = a -5;
    analogWrite(pwmG, a);
    
    Serial.println(a);
    delay(200);
  }
}

What I have :

What I expected (exemple found on the internet : What I expected (exemple found on the internet

1
  • 1
    Look at the output of your sketch on the serial monitor, not on the plotter. It should become immediately clear what is going on. Jun 7 at 12:44

1 Answer 1

3

The first point here is, that the Serial Plotter will plot the values against time by connecting them with a straight line. This is just how it represents the data in the plot (it is just for ad-hoc visualization, not for detailed plotting and data analysis).

The second point is, that for each step you are sending only one value through the Serial interface, so only one data point per step. The Serial Plotter then plots this data point against time and connects it with the points for the other steps with a straight line. Thus you get sloped lines/triangle signal in the Serial Plotter.

The image, that you expected, looks like from an oscilloscope. This will have many more data points along the x-axis, not just one per step. Thus you can actually see the steps. If you want to reproduce the image with your code, then you can send the current value of a more often in each iteration of the for loop (like looping 20 times over sending the value and delaying about 10ms).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.