Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hack star
Published © MIT

Wokwi - Arduino Plotter examples - collections

Here are the few examples of simulating Serial plotter examples on Wokwi Arduino simulator

BeginnerProtip2 hours3,044
Wokwi - Arduino Plotter examples - collections

Things used in this project

Story

Read more

Schematics

Schematics

Code

Arduino serial plotter examples

Arduino
unsigned int period = 1000; // in ms
unsigned int samplingPeriod = 1; // print something every 5ms

double minValue = -1;
double maxValue = 1;

// precalculate some values
double halfPeriod = period / 2.0;
double a1 = (maxValue - minValue) / halfPeriod;
double b1 = 2.0 * maxValue - minValue;

// time management
unsigned long currentTime, relativeTime, previousTime, lastFullPeriod;


void setup() {
  Serial.begin(9600);
  while (!Serial);
  lastFullPeriod = 0;
}

void loop() {
  double y;
  // check if it is time to display a new value
  if (((currentTime = millis()) - previousTime) >= samplingPeriod) {
    if ((relativeTime = currentTime - lastFullPeriod) >= period) {
      relativeTime = 0 ;
      lastFullPeriod = currentTime;
    }

    if (relativeTime < halfPeriod) {
      y = a1 * (double) relativeTime  + minValue;
    } else {
      y = -a1 * (double) relativeTime + b1 ;
    }

    //Serial.print(currentTime);
    Serial.print("\t");
    Serial.println(y, 6);

    previousTime = millis();
  }
}

Credits

Hack star
75 projects • 136 followers
an Arduino enthusiast and an electronic hobbyist
Contact

Comments

Please log in or sign up to comment.