I'm trying to make a graph -Part 1

edited March 2017 in Arduino

im trying to make a graph (added to the bottom of this post) with data acquired by a serial feed But it won't draw right no matter how many variables I tweak.

I am not asking anyone to code for me, just to point out to a first timer(usually play with arduino) where my problem lies.

here is my code:`

import processing.serial.*;


Serial myPort;        // The serial port
float inByte = 0;     // defines starting value for the serial input
int xPos = 1;         // horizontal position of the graph
int yPos = 300;         // current vertical position of the graph
int lastxPos = xPos;     // previous horizontal position of the graph
float lastyPos = (inByte);     // last vertical position of the graph


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

  // List all the available serial ports
  // if using Processing 2.1 or later, use Serial.printArray()
  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()[1], 115200);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');

  // set inital background:
  background(0);
}
void draw () {
  // draw the line:
  stroke(0, 205, 0);
  line(xPos, height-lastyPos*5, xPos, height - inByte*5);



  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    background(0);
  } else {


     // increment the horizontal position:
    xPos =xPos + 10;
    //lastxPos = xPos;

  }
}


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:
    inByte = float(inString);
    println(inByte);
    inByte = map(inByte, 0, 1023, 0, height);
  }
}

` dammit

Answers

  • it won't draw right

    that picture looks fine to me. you need to describe what the problem is, not just 'it won't draw right'.

    line(xPos, height-lastyPos*5, xPos, height - inByte*5);
    

    where do you update lastyPos?

  • edited March 2017
    1. The picture is how I want it to look, ie: an example.
    2. line 9. It was line 48, mimicking 47, but moving it up to 9 made no difference.
    3. Posting a pic of how I want it to look, and the code that is not displaying that result is all that is needed, is it not?

    I don't really know what else I can give you.

    Cheers J

  • edited March 2017

    After reading that again, I think it comes across.....Well a bit crappy. Tbh, I'm on a laaaarge amount of painkillers at the moment and it tends to mess with my comprehension and retaining of info too

  • float lastyPos = (inByte); 
    

    but this sets lastyPos to the value of inByte AT THAT INSTANT IN TIME. if inbyte changes after this lastyPos won't reflect that.

    The picture is how I want it to look, ie: an example.

    but you didn't actually say that was an 'expected' picture and not an 'actual' picture...

    try adding add line 48:

    lastyPos = inByte;
    

    (i can't run your code, no serial port)

  • You don't set lastyPos

    It's not enough to set it before setup, you need to set it in the right spot

    Does println in line 62 good results? Did you try println in line 64?

    Line 34: the multiply by 5 is surprising : wrong

    Move 46 to line 63 maybe? Then you move your writing position only when there is an input

  • I have tried the lastyPos=inByte at line 48, and that gives an error of (paraphrased) can't assign a glitch to an integer

  • edited March 2017

    Q: Does println in line 62 good results? A: yes

    Q: Line 34: the multiply by 5 is surprising A: It's only to blow the data up to where I can see it on the scale, as I'm manually triggering the pin on the arduino that reports to Processing via serial

    Q: Move 46 to line 63 maybe? Then you move your writing position only when there is an input A : the graph is recorded over time, so I need the constant increments

  • edited March 2017

    Here's how it actually displays for me.

    actual_result

  • lastyPos Must be type float

    Then follow koogs advice

  • I have tried the lastyPos=inByte at line 48, and that gives an error of (paraphrased) can't assign a glitch to an integer

    can't assign a float to an integer, probably. so try:

    lastyPos = (int)inByte;
    

    Q: Does println in line 62 good results? A: yes

    what kind of values? the map in line 63 suggests 0 to 1023 (10 bits)

  • Anything you put in setup is called once.

    If you put it in draw, it is called continuously. Think of an infinite loop(@koogs) being called 30 times per second.

    To draw a line, it is easy. You need to points labeled x1,y1 and x2,y2. In your case, for any x1, x2=x1+10. For the second part of your data, you need to keep track of previous values. In other words, line 46 in your code should be like:

    lastxPos = (xPos+10)%width;  
    xPos =xPos + 10;
    lastyPos = (int)inByte;
    

    and line 34:

    line(lastxPos, height-lastyPos*5, xPos, height - inByte*5);

    Kf

  • edited March 2017

    I'm trying to log up to and maybe above 400,000rpm times up to 16 pulses per rpm 0 to 1023 is for.....oh damn the range that the arduino can see, its name eludes me. (ffs, buprenorphine, let me think) EDIT ADC is what i was thinking of

    (int)inByte fixed that issue

  • Getting closer!

    actual_result

  • Answer ✓

    (beware calling println() too much (and 400,000 sounds like too much) - the console in processing is dodgy and can lock up you code.)

  • I think I'm only pulling maybe 150 pulses at best, by hand. end result will be more like 400,000. I'm trying to read Turbocharger rpm

  • Answer ✓

    Ding Ding Ding Ding We have a weiner! Thankyou very much Guys!! Much Gratitude :)

    actual_result

  • well done!

  • edited March 2017

    ok, so I got quite a lot more code now, and in the midst of trying to make this graph display 6 sensors worth of data, I've gotten stuck on the part where it strips the string back down into 6 parts(after the arduino put them together) I think that part actually may work, but I'm confused as to how to get the stripped string into my existing x/y display. if what I've messed up is obvious, please just point out where the problem is, not how to fix it.

    the basic idea is just a 6 channel datalogger- I've not gotten to the saving parts, I'm still on the displaying live data side of things.

    1st data feed is in Hz next 5 are just 0-5v

    ------------------Arduino code------------------

    const uint8_t turboSpdPin = 5; //TurboSpeed Input Digital Pin
    const uint8_t map1Pin = A0; // Map Sensor 1 Input Analog pin
    const uint8_t map2Pin = A1; // Map Sensor 2 Input Analog pin
    const uint32_t oneSecond = 1000000; //Meassured time
    uint32_t timer = 0;
    uint32_t sts = 0;
    const uint32_t c = 1; // wait for 3000 pulses
    uint32_t ets = 0;
    int turboSpdPinState = LOW;
    int turboSpd = 0;
    int prevturboSpdPinState = LOW;
    int map1inputValue = 0;  //set sensor value to 0 
    int map2inputValue = 0;  //set sensor value to 0
    int aux1Value = 0; // set start value to 0
    int aux2Value = 0; // set start value to 0
    const int Aux1 = A2; //Define Aux Input Pin number as Analog 2
    const int Aux2 = A3; //Define Aux Input Pin number as Analog 3
    const int extPwr = 5;
    const int turbineActivity = 6;
    const int map1connected = 7;
    const int map2connected = 8;
    const int pwrOn = 9;
    
    
    
    void setup()
    {
     Serial.begin(115200);
     pinMode(turboSpdPin, INPUT); //Declared Digital Pin 5 as Input
     digitalWrite(turboSpdPin,LOW); 
     }
    void loop() {
    
      map1inputValue = analogRead(map1Pin);   //read map sensor input
      map2inputValue = analogRead(map2Pin);   //     " "
      aux1Value = analogRead(Aux1);
      aux2Value = analogRead(Aux2);
      pulseIn(turboSpdPin,HIGH);
      sts = micros(); // start time stamp
       for (uint32_t i=c; i>0; i--)
        pulseIn(turboSpdPin,LOW);
    
     ets = micros(); // end time stamp
     //Serial.print("$");
     Serial.println((c*1e6/(ets-sts))); // output Hz
    }
    

    -------------------Processing Code---------------------

    void setup () {
      // set the window size:
      size(1024, 300);
    plot = new GPlot(this, 0, 0, 1024, 300);
      // List all the available serial ports
      println(Serial.list ());
      myPort = new Serial(this, Serial.list()[1], 115200); //open COMport(3) on PC
      myPort.bufferUntil('\r'); // don't generate a serialEvent() unless you get a carriage return
      background(0);    // set inital background:
                     }
    void draw () {
      // draw the line:
      stroke(lastyPos,205, 0);
      line(lastxPos, height-(lastyPos+20), xPos, height - (inByte+20));
      lastxPos = xPos;
      lastyPos =inByte; // (int)inByte;
    
    
      if (xPos >= width) {                                      // at the edge of the screen, go back to the beginning:
       inByte=0;                                                //drop x/y to bottom of window
        line(lastxPos, height-lastyPos+20, xPos, height-20);    // bring x/y back to start of window
    
        xPos = 20;               //reset
        lastxPos = 20;           //reset
         background(0);          
      } else {     
        xPos =xPos + 3;   // increment the horizontal position:
              }
    }
    
    
    void serialEvent (Serial myPort) {
    
      String inString = myPort.readStringUntil('\r');    // read serial buffer:
      if (inString != null) {
           inString = trim(inString);          // trim off any whitespace:
       //next 4 lines worked as one data feed, everything after that was (i think ) added from another sketch to try to make a 6 value feed. I am stuck here.
    
       // convert to an int and map to the screen height:
       // inByte = float(inString);
       // println(inByte);
       // inByte = map(inByte, 0, 1023, 0, height-20);
       int mysensors[] = int(split(inString, '\t'));
        count = mysensors.length;
     println(mysensors.length);
        for (int i=0; i<count; i++)  //
        {
          // set sensor[i] value for use in draw()
          sensors[i] = mysensors[i];   //<--- you'll need to change this line to put
                                                 //    the values where you want them
          // print out the values we got:
          print(i + ": " + sensors[i] + "\t");
        }
      }
    }
    
  • I can't seem to get this to view properly in here, in the edit window, it's fine.

  • Answer ✓

    edit post, highlight code, press ctrl-o

  • edited March 2017

    Much better, thanks koogs. Ill move this new info to another thread, as its a different issue

Sign In or Register to comment.