hello everyone! does anyone know how to plot a graph with Arduino output? i have read tutorials about plotting using processing and the analog output of the Arduino.
I want to plot a polar graph using the magnitude of an ultrasonic sensor against the angle of a servo.
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7); //the pin for the servo control
Serial.println("Input angle in Serial");
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
if((uS / US_ROUNDTRIP_CM)<=5 && (uS / US_ROUNDTRIP_CM)!=0 )
Serial.print("Too Close");
}
the outputs are
magnitude = (uS / US_ROUNDTRIP_CM)!=0 )
Angle = Servo Angle
I must be starting to sound like some droning evangelist on this but I have to say exporting the data to Excel would have to be the way to go first. This can be done by:
Recording the data as a CSV file on SD for subsequent use by Excel
Sending the data to a terminal programme for subsequent transfer to Excel
Using the PLX-DAQ macro whereby Excel becomes the terminal and receives the data directly.
I recognise that proper polar charts are not in Excel 2003's repertoire but I understand it can be persuaded to deliver. Later versions of Excel may be more able to do them solo, but probably not!
I can't give you the code (because I am not familiar enough with Processing), but I think I can point you in the right direction, if you are able to follow along.
First, take a look at this page:
This is the basics of plotting a circle, using what is call the "parametric equation of a circle" - which is:
x = cx + r * cos(ang)
y = cy + r * sin(ang)
...where:
x, y = end point coordinates on the circle
cx, cy = centerpoint coordinates of the circle
r = radius of the circle
ang = angle (in radians)
So - first convert your angle from the servo (0-359?) into radians via:
ang = (PI * deg) / 180
...where:
ang = angle in radians
PI = 3.14159265359
deg = angle in degrees
Then plug that into the above "parametric equation of a circle", along with your "r" - which will be the magnitude value from your ping response (you may need to do some scaling depending on what you get out).
So - say you have a window/screen for graphics opened in processing (however you do that), and that window is sized 640 x 480. So you read the angle of the servo in degrees (deg) and magnitude of the sensor (dist) - to plot it:
ang = (PI * deg) / 180; // convert angle in degrees to radians
x = 320 + dist * cos(ang); // find x-coordinate centered on screen
y = 240 + dist * sin(ang); // find y-coordinate centered on screen
drawline 320, 240 to x, y; // draw a line from the center of the screen to the length given for the "ping"
Ok - note that the above is all basically "pseudocode" and not real processing code - but it should be enough to get you going - hopefully.
DMond:
hi! that sounds like a great idea! but does it support real time plotting?
Indeed it does. Check the background notes I linked, that is what it is all about. And its a freebie........
I first thought there was some live graphing magic in PLX-DAQ and it has taken me a couple of years to realise that it is merely a terminal interface for Excel, but that is all it needs to be. All the magic is actually in Excel and any Excel graph may be plotted in real time.
first i have to say, that i havnt tried plotting a graph for myself. but i read and saw a code wich is plotting a sensor-value in a x-y-system. it looks not very difficult.
here you can find a example, plotting random numbers in a x-y-system.