2017-01-24 2 views
0

Graphen Ich versuche, etwas ziemlich einfaches zu tun, das nur die Werte von einer Fotozelle an einem Arduino zu einem Verarbeitungsgraphen ausgegeben wird. Ich habe es geschafft, die Werte als Floats auf die Processing-Konsole auszugeben und ein Skizzenfenster erscheint, aber es gibt kein Diagramm - ich bin mir sicher, dass es etwas Einfaches ist, aber ich weiß nicht was. Ich habe ein Gefühl damit zu tun das Ausmaß dessen, was ich zeichne (das println gibt mir Werte zwischen 0,2 und 1,1), aber ich denke, dass ich in der Skalierung des Graphen verloren bin, also keine Ausgabe. Hier ist mein Verarbeitungscode:Wie in der Verarbeitung von einem Arduino

import processing.serial.*; 

Serial myPort;  // The serial port 
int xPos = 1;   // horizontal position of the graph 

//Variables to draw a continuous line. 
int lastxPos=1; 
int lastheight=0; 

void setup() { 
    // set the window size: 
    size(600, 400);   

    // List all the available serial ports 
    // Check the listed serial ports in your machine 
    // and use the correct index number in Serial.list()[]. 

    myPort = new Serial(this, "/dev/tty.usbmodem1421", 9600); // 

    // A serialEvent() is generated when a newline character is received : 
    myPort.bufferUntil('\n'); 
    background(0);  // set inital background: 
} 
void draw() { 
    // everything happens in the serialEvent() 
} 

void serialEvent (Serial myPort) { 
    // get the ASCII string: 
    String inString = myPort.readStringUntil('\n'); 
    inString = trim(inString); // trim off whitespaces. 
    //println(inString); 
    if (inString != null) { 

    float inByte = float(inString);   // convert to a number. 
    inByte = map(inByte, 0, 1023, 0, height); //map to the screen height. 
println(inByte); 
    //Drawing a line from Last inByte to the new one. 
    stroke(127,34,255);  //stroke color 
    strokeWeight(4);  //stroke wider 
    line(lastxPos, lastheight, xPos, height - inByte); 
    lastxPos= xPos; 
    lastheight= int(height-inByte); 

    // at the edge of the window, go back to the beginning: 
    if (xPos >= width) { 
     xPos = 0; 
     lastxPos= 0; 
     background(0); //Clear the screen. 
    } 
    else { 
     // increment the horizontal position: 
     xPos++; 
    } 
    } 
} 

Antwort

0

Während ich nicht den Arduino eingegeben, durch die Teile zu entfernen konnte ich eine lila Linie zeichnen. Es wurde jedoch einfach am unteren Rand des Fensters gezeichnet, da ich einen statischen "0.5" Zeichenfolgenwert für inString verwendete, um in der gleichen Größenordnung wie Ihre Eingabewerte zu bleiben. Einfach den Wert multipliziert wird, wenn, d.h .:

weiter oben

float inByte = float(inString)*500;,

zog die Linie schweben zu konvertieren. Sie könnten versuchen, den float-Eingang mit height oder vielleicht sogar 1023 zu multiplizieren, und es sollte innerhalb der Grenzen bleiben. Multiplizieren mit height auf dem 0.5-Wert, den ich verwendete, zieht die Linie immer näher an den Boden als die Mitte (d. H. height/2).

Unten ist der vollständige Code, den ich verwendet habe, um eine violette Linie über die Leinwand zu zeichnen. Wie Sie sehen können, zog ich einfach alles in draw(), und entfernt die seriellen Eingangsteile:

int xPos = 1;   // horizontal position of the graph 

//Variables to draw a continuous line. 
int lastxPos=1; 
int lastheight=0; 

void setup() { 
    // set the window size: 
    size(600, 400);   

    // List all the available serial ports 
    // Check the listed serial ports in your machine 
    // and use the correct index number in Serial.list()[]. 
    background(0);  // set inital background: 
} 
void draw() { 
    // get the ASCII string: 
    String inString = "0.5"; 
    inString = trim(inString); // trim off whitespaces. 
    //println(inString); 
    if (inString != null) { 
    float inByte = float(inString)*1023;   // convert to a number. 
    inByte = map(inByte, 0, 1023, 0, height); //map to the screen height. 
    println(inByte); 
    //Drawing a line from Last inByte to the new one. 
    stroke(127,34,255);  //stroke color 
    strokeWeight(4);  //stroke wider 
    line(lastxPos, lastheight, xPos, height - inByte); 
    lastxPos= xPos; 
    lastheight= int(height-inByte); 

    // at the edge of the window, go back to the beginning: 
    if (xPos >= width) { 
     xPos = 0; 
     lastxPos= 0; 
     background(0); //Clear the screen. 
    } 
    else { 
     // increment the horizontal position: 
     xPos++; 
    } 
    } 
} 
Verwandte Themen