2

I have this code which produces a square wave:

int output_state = 0;
unsigned long start;

void setup() {
  Serial.begin(1200);
  start = millis();
}

void loop() {

  if((millis() - start) >=1000){
    output_state = ! output_state;
    //output_state *= 1023;
    start = millis();
  }

    Serial.println(output_state);
}

enter image description here

but should I multiply the signal by 1023:

output_state *= 1023;

this signal is produced by the Serial Plotter:

enter image description here

Mind that the timing is right, just the x axis speeds up in zero values. Why is that happening, and how can be solved?

Any thoughts would be appreciated.

4

It takes twice as long to print "1023" (6 bytes, including the CRLF line terminator) than to print "0" (3 bytes). Thus, in the same time, you are printing twice as many "0" than "1023".

If you want the data points to be printed at a consistent rate, you can impose the printing timing just like you did on the output_state. Keep in mind that you cannot print more than 20 samples per second at 1200 bps. You could also pad the strings so that they always have the same length:

char s[5];  // one byte for the NULL terminator
sprintf(s, "%4d", output_state);
Serial.println(s);

I would prefer the first solution (explicitly control the timing).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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