Stap 2: Maak een grafiek met een LV-MaxSonar
We wilden zien hoe een grafiek zou blijken bij het lezen van objecten in een kamer. Om het lezen had om te physicaly gaan we de MaxSonar. Bij het plaatsen van Arduino en MaxSonar richting de vloer die erin geslaagd te maken van de lezingen van de vloer. Dus je moet te halen zulks werkzaamheden met het digitale lezen plaats je het ten minste 30 cm boven de vloer. Zoals je op de afbeelding van de grafiek het soms ziet ondervindt bij het meten van de afstand.
Fritzing:
Zie de eerste afbeelding.
Verwerking van code:
/* Processing code for this example<br> // Graphing sketch // This program takes ASCII-encoded strings // from the serial port at 9600 baud and graphs them. It expects values in the // range 0 to 1023, followed by a newline, or newline and carriage return // Created 20 Apr 2005 // Updated 18 Jan 2008 // by Tom Igoe // This example code is in the public domain. */ import processing.serial.*; Serial myPort; // The serial port int xPos = 1; // horizontal position of the graph void setup () { // set the window size: size(400, 300); // List all the available serial ports println(Serial.list()); // I know that the first port in the serial list on my mac // is always my Arduino, so I open Serial.list()[0]. // Open whatever port is the one you're using. myPort = new Serial(this, Serial.list()[5], 9600); // don't generate a serialEvent() unless you get a newline character: myPort.bufferUntil('\n'); // set inital background: background(0); } void draw () { // everything happens in the serialEvent() } void serialEvent (Serial myPort) { // get the ASCII string: String inString = myPort.readStringUntil('\n'); if (inString != null) { // trim off any whitespace: inString = trim(inString); // convert to an int and map to the screen height: float inByte = float(inString); inByte = map(inByte, 0, 1023, 0, height); // draw the line: stroke(127,34,255); line(xPos, height, xPos, height - inByte); // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; background(0); } else { // increment the horizontal position: xPos++; } } }
We vonden de code hier: http://arduino.cc/en/Tutorial/Graph (Arduino, 09.10.14)
Arduino code:
//Original author: Bruce Allen<br>//Date: 23/07/09 //Changed by Marte and Runar 09/10/14, to only have cm as output and to fit the processing code for a graph //Digital pin 7 for reading in the pulse width from the MaxSonar device. //This variable is a constant because the pin will not change throughout execution of this code. const int pwPin = 7; //variables needed to store values long pulse, inches, cm; int output; void setup() { //This opens up a serial connection to shoot the results back to the PC console Serial.begin(9600); } void loop() { pinMode(pwPin, INPUT); //Used to read in the pulse that is being sent by the MaxSonar device. //Pulse Width representation with a scale factor of 147 uS per Inch. pulse = pulseIn(pwPin, HIGH); //147uS per inch inches = pulse/147; //change inches to centimetres cm = inches * 2.54; // Serial.print(inches); // Serial.print("in, "); // Serial.print(cm); // Serial.print("cm"); // Serial.println(); output= map(cm,0,645,0,1023); Serial.println(output); delay(1); }
We vonden de code hier, onder de PW: http://playground.arduino.cc/Main/MaxSonar (Allen, 09.10.14)
De output is slechts cm (niet de cm en inch), en de output waarden worden gewijzigd als gevolg van de waarden van de grafiek.