Logic analyzer

edited October 2016 in Arduino

Hello, i am working on a very simple logyc analyzer.

The hardware is made of an arduino which semd through serial this kind of data:

100:20:20

Where the first number is the time in microseconds when the pulse is high, the second low, and the last is the duty cycle.

Processing read everything correct from arduino

My problem is in drawing it. I tried with lines and points but i m not able to draw it correct

Tagged:

Answers

  • Please post the code you have so far. Also, for drawing this info, you would need to clarify the relationship btw duty cycle value and the pulse itself.

    Kf

  • edited October 2016

    my arduino code as i said send this kind of data:

    Serial.print (timeUp); Serial.print (":"); Serial.print (timeDown); Serial.print (":"); Serial.println (dc);

    the duty cycle is calculated with this:

    void dutyCicle () { long timeTot = timeUp + timeDown; dc = (float) (timeUp * 100.00) / timeTot; return dc; }

    here processing:

    import processing.serial.*;

    Serial myPort; int xPos = 0; int timeUp; int timeDown; float dc; int y_high = 100; int y_low = 200;

    void setup () {

    size(800, 300); myPort = new Serial(this, Serial.list()[0], 115200); myPort.bufferUntil('\n');

    background(255); }

    void draw () { while (xPos < timeUp) {

    point(xPos, y_high);
    xPos++;
    

    }

    if (xPos == timeUp) { line(xPos, y_high, xPos, y_low); timeDown = xPos + timeDown; }

    while (xPos < timeDown) {

    point(xPos, y_low);
    xPos++;
    

    }

    if (xPos == timeDown) { line(xPos, y_low, xPos, y_high); timeUp = xPos + timeUp; } }

    void serialEvent (Serial myPort) {

    String inString = myPort.readString();

    if (inString != null) { inString = trim(inString); String list [] = split(inString, ':'); timeUp = int(list[0]); timeDown = int(list[1]); dc = float(list[2]); }

    println(timeDown); }

    the comunication isn't a problem, but i really don't have any idea why it doesn't work. and i m quite sure that i m doing it in a stupid and complicated way but it s the first time that i have hand on processing

  • GoToLoop thank you but i don t think you have read my previously post: i don t have problem with the comunication. I get exactly the data that i want but i m not able to use them in processing

    My data are two: The time in which the pulse is hight and the time in which is low. And from them i should draw just two lines proportionally to the value of the data

  • edited October 2016 Answer ✓

    @aster94 -- currently nobody can copy/paste and test your code, because it is not formatted correctly. Please format it correctly as per How to Format Code.

    To debug, first separate your drawing logic from your serial logic, so that you can test your draw logic with either a stored string or with randomly generated numbers.

    It looks like your draw loop is unbounded in ways that cause you lots of problems.

    1. Formatting: declare Y_LOW and Y_HIGH in capitals as global constants; assign xPos in setup to make it clear that it is not a constant.
    2. Currently you have a multi-pass drawing method. However, xPos increases to infinity, and you never reset it. So your draw loop draws off the screen to the right hand side -- forever. Performances slows or stops, and no new drawing appears onscreen. One solution is to wrap your draw block in an if statement, checking that xPos is < width and doing nothing otherwise. Decide what events will clear the screen and reset xPos.

    This test sketch does it with a mouse click:

    /**
     * Logic analyzer draw routine
     * 2016-10-12 Processing 3.2.1
     * https:// forum.processing.org/two/discussion/18513/logic-analyzer#latest
     **/
    
    int xPos;
    int timeUp;
    int timeDown;
    float dc;
    int Y_HIGH = 100;
    int Y_LOW = 200;
    
    void setup () {
      size(800, 300); 
      xPos = 0;
      timeUp = 50;
      timeDown = 100;
      background(255);
    }
    
    void draw () {
      if (xPos < width) {
        while (xPos < timeUp) {
          point(xPos, Y_HIGH);
          xPos++;
        }
    
        if (xPos == timeUp) { 
          line(xPos, Y_HIGH, xPos, Y_LOW); 
          timeDown = xPos + timeDown;
        }
    
        while (xPos < timeDown) {
          point(xPos, Y_LOW);
          xPos++;
        }
    
        if (xPos == timeDown) { 
          line(xPos, Y_LOW, xPos, Y_HIGH); 
          timeUp = xPos + timeUp;
        }
      }
    }
    
    void mouseClicked() {
      background(255);
      xPos = 0;
      timeUp = int(random(10, 50));
      timeDown = int(random(50, 100));
    }
    

    LogicAnalyzerDrawRoutine

  • Another line plotting example, but using key presses as input: :)
    http://studio.SketchPad.cc/sp/pad/view/ro.9cfU11egvzT6G/latest

  • I will try! But my first idea was to have it real time. The serialEvent(); fires every time processing receive a new data, correct?

  • edited October 2016 Answer ✓
  • edited October 2016

    hi, i completed it, works very well, i will do a few little adjustmet but its ok it is completely different to the first code i wrote, i use more line(); than point(); because they were to slow, then i add a few texting of data

    i didn't put the serial event since for you is better to create some random values if you want to play it

    void setup () {
    
      size(800, 300);
    
    
      background(255);
      stroke(0);
    }
    
    int timeUp;
    int timeDown;
    float dc;
    
    int xPos = 0;
    int y_high = 50;
    int y_low = 100;
    int x_high, x_low;
    int remain;
    
    
    void draw () {
    
      x_high = xPos + timeUp;
    
      if (x_high >= width) {
    
        line(xPos, y_high, width, y_high);
    
        xPos = 0;
        background(255);
        remain = x_high - width;
    
        line(xPos, y_high, remain, y_high);
        xPos = remain;
    
      } else {
    
        line(xPos, y_high, x_high, y_high);
        xPos = x_high;
    
      }
    
      line(xPos, y_high, xPos, y_low);
    
      x_low = xPos + timeDown;
    
      if (x_low >= width) {
    
        line(xPos, y_low, width, y_low);
    
        xPos = 0;
        background(255);
        remain = x_low - width;
    
        line(xPos, y_low, remain, y_low);
        xPos = remain;
    
      } else {
    
        line(xPos, y_low, x_low, y_low);
        xPos = x_low;
    
      }
    
      line(xPos, y_low, xPos, y_high);
    
      drawData();
    }
    
    void drawData () {
    
      pushMatrix();
      translate(0, 200);
    
      fill(0);
      rect(0, 0, width, 100);
    
      fill(255);
      textSize(15);
    
      text("High:", 30, 30);
      text(timeUp, 100, 30);
      text("Low:", 30, 60);
      text(timeDown, 100, 60);
      text("DC:", 30, 90);
      text(dc, 100, 90);
    
      popMatrix();
    } 
    

    thanks to everyone!

    the only stuff that i don't understand is why when i arrive near the edge it don't draw the line untill it

Sign In or Register to comment.