Graphing 2 Values with Arduino

edited April 2014 in Arduino

I am trying to create a graph with 2 lines, I have done so with one line and I want to add a second line. I am using the following code

Arduino

int led1 = 2;
int led2 = 4;


void setup() {

   Serial.begin(9600);
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

}


void loop() {

   int sensorValue1 = analogRead(A0);
   int sensorValue2 = analogRead(A1);

   float Gauss1 = (sensorValue1 - 510) * (1200/1023);
   float Gauss2 = (sensorValue2 - 507) * (1200/1023);


   if(Gauss1 >= 0 && abs(Gauss1 <= 4) && abs(Gauss2)>=14 && abs(Gauss2) <=19)
   {
     digitalWrite(led1, HIGH);
     digitalWrite(led2, LOW);

   }

   if(Gauss2 >= 2 && Gauss2 <= 6 && abs(Gauss1)>=15 && abs(Gauss1) <=26)
   {
     digitalWrite(led1, LOW);
     digitalWrite(led2, HIGH);

   }

   Serial.println (abs(Gauss1));
   Serial.println (abs(Gauss2));


}

Processing

import processing.serial.*;

Serial dataPort;      // The incoming data serial port 
int Xpos;             // horizontal position of the plot.
int Ypos;             // vertical position of the plot

int lastXpos;         // last x coordinate 
int lastYpos;         // last y coordinate

//----------------- setup -----------------------------------------------------
void setup () {
  // window size:
  size(400, 300);        

  println(Serial.list());   // List the available serial ports.


  dataPort = new Serial(this, Serial.list()[0], 9600);  //<-- SET INDEX

  dataPort.bufferUntil('\n');  //wait for a newline char to call serialEvent.

  background(0);          // set background

  Xpos = width+1;         //Set horizontal position off the screen.
}

//----------------- draw ------------------------------------------------------
void draw () {
  // serialEvent() plot the incoming data.
}

//----------------- serialEvent -----------------------------------------------
void serialEvent (Serial dataPort) {

  String inString = dataPort.readStringUntil('\n');  //read-in the string

  if (inString != null) {
    inString = trim(inString);    //eliminate whitespaces:
    // convert to an int and map to the screen height:
    float inData = float(inString); //<-----CHECK THIS
    inData = map(inData, 0, 50, 0, height); //scale data to window size.
    Ypos= int(height-inData);

    if (Xpos > width) {   // if off the screen
      background(0);      // Clear Screen
      Xpos = 0;           // initiate new trace
      lastXpos= Xpos;     // x position of first point 
      lastYpos= Ypos;     // y position of first point
    } 

    stroke(127,34,255);     //stroke color
    strokeWeight(.5);        // stroke size
    line(lastXpos, lastYpos, Xpos, Ypos);
    line(lastXpos, lastYpos, Xpos, Ypos);

    lastXpos= Xpos;
    lastYpos= Ypos;

    Xpos++;                 // increment the horizontal position:

  }
}

plot

I have to comment out the last line in the Arduino ( shown below) to produce the above plot.

//Serial.println (abs(Gauss2));

I would like to produce a second plot over the top of the above plot in a different color.

Answers

  • in arduino ide try the sample "VirtualColorMixer" . explains how to get the most value in processing

Sign In or Register to comment.