SimPlot‎ > ‎Code Samples‎ > ‎

Plot analog input data

/*
SimPlot Demo
 
Samples Analog Input and sends them over serial port to be plotted in SimPlot.
This Demo software uses the default serial port "Serial".
Upto to 4 channels of data can be plotted.

For details of SimPlot go to
www.negtronics.com/simplot

 */
void setup()
{
  Serial.begin(57600);
  
}

int buffer[20]; //Buffer needed to store data packet for transmission
int data1;
int data2;
int data3;
int data4;

void loop()
{

  //Read Analog channels. You can connect accelerometer, gyro, temperature sensor etc to these channels
  data1 = analogRead(0);
  data2 = analogRead(1);
  data3 = analogRead(2);
  data4 = analogRead(3);
 
  //You can plot upto 4 channels of data. Uncomment only one of the options below
 
   plot(data1,data2,data3,data4);   //Plots 4 channels of data
//  plot(data1,data2,data3);      //Plots 3 channels of data
//  plot(data1,data2);            //Plots 2 channels of data
//  plot(data1);                  //Plots 1 channel of data
 
  delay(10); //Read and plot analog inputs every 10ms. 
}

//Function that takes 4 integer values and generates a packet to be sent to SimPlot.
void plot(int data1, int data2, int data3, int data4)
{
  int pktSize;
 
  buffer[0] = 0xCDAB;             //SimPlot packet header. Indicates start of data packet
  buffer[1] = 4*sizeof(int);      //Size of data in bytes. Does not include the header and size fields
  buffer[2] = data1;
  buffer[3] = data2;
  buffer[4] = data3;
  buffer[5] = data4;
   
  pktSize = 2 + 2 + (4*sizeof(int)); //Header bytes + size field bytes + data
 
  //IMPORTANT: Change to serial port that is connected to PC
  Serial.write((uint8_t * )buffer, pktSize);
}

//Function that takes 3 integer values and generates a packet to be sent to SimPlot.
void plot(int data1, int data2, int data3)
{
  int pktSize;
 
  buffer[0] = 0xCDAB;             //SimPlot packet header. Indicates start of data packet
  buffer[1] = 3*sizeof(int);      //Size of data in bytes. Does not include the header and size fields
  buffer[2] = data1;
  buffer[3] = data2;
  buffer[4] = data3;
   
  pktSize = 2 + 2 + (3*sizeof(int)); //Header bytes + size field bytes + data
 
  //IMPORTANT: Change to serial port that is connected to PC
  Serial.write((uint8_t * )buffer, pktSize);
}

//Function that takes 2 integer values and generates a packet to be sent to SimPlot.
void plot(int data1, int data2)
{
  int pktSize;
 
  buffer[0] = 0xCDAB;             //SimPlot packet header. Indicates start of data packet
  buffer[1] = 2*sizeof(int);      //Size of data in bytes. Does not include the header and size fields
  buffer[2] = data1;
  buffer[3] = data2;
   
  pktSize = 2 + 2 + (2*sizeof(int)); //Header bytes + size field bytes + data
 
  //IMPORTANT: Change to serial port that is connected to PC
  Serial.write((uint8_t * )buffer, pktSize);
}

//Function that takes 1 integer value and generates a packet to be sent to SimPlot.
void plot(int data1)
{
  int pktSize;
 
  buffer[0] = 0xCDAB;             //SimPlot packet header. Indicates start of data packet
  buffer[1] = 1*sizeof(int);      //Size of data in bytes. Does not include the header and size fields
  buffer[2] = data1;
   
  pktSize = 2 + 2 + (1*sizeof(int)); //Header bytes + size field bytes + data
 
  //IMPORTANT: Change to serial port that is connected to PC
  Serial.write((uint8_t * )buffer, pktSize);
}
Comments