Storing a Serial Value

edited April 2015 in Programming Questions

Hi Everyone,

I've been struggling with this issue for a number of days now, I'm quite new to Processing and am unsure of the correct way to go about Storing a Serial value .

All I'm trying to do is have Processing Scan the port and wait for a number to appear (example 112) once Processing sees this number it needs to prompt text on the Screen to say ON and it needs to stay that way until it either receives the same number or a specific number. If it receives anything outside those 2 numbers it just needs to keep reporting ON.

I have the basis of what I want but because the state obviously is no longer valid when it receives a different number the previous state disappears from the window.

Any help would be greatly appreciated

Answers

  • edited April 2015
    ```
    
    import processing.serial.*;
    
    Serial myPort;  
    
    String inString;
    
    int inByte;
    
    PFont f; 
    
    
    void setup() 
    {
      size(640, 480);
    
      printArray(PFont.list());
    
      f = createFont("Aharoni-Bold", 30);
    
      textFont(f);
      smooth();
    
      myPort = new Serial(this, "COM3", 57600);
    
      myPort.bufferUntil('\n');
    }
    
    void draw()
    
    {
     background(0);
    
     textAlign(RIGHT);
    
     text("Performance Mode",300, 50);
    
     text("ECU Mode", 300, 250 );
    
     text("EDC Mode", 300, 450); 
    
    inString = myPort.readString();
    
    
    if (inString != null) {
    
    inString = trim(inString);
    inByte = int(inString);
    
    println(inString);
    println(inByte);
    }
    
    switch (inByte)
     {
       case 112:
       fill(255);
       textAlign(LEFT );
       text("ON", 400, 50);
       break;
    
       case 113:
       fill(255);
        textAlign(LEFT);
        text("OFF", 400, 50);
         break;
    
        case 114:
       fill(255);
       textAlign(LEFT );
       text("ECO", 400, 250);
       break;
    
       case 115:
       fill(255);
       textAlign(LEFT );
       text("TRACK", 400, 250);
       break;
    
       case 116:
       fill(255);
       textAlign(LEFT );
       text("DRAG", 400, 250);
       break;
    
       case 117:
       fill(255);
       textAlign(LEFT );
       text("STREET", 400, 250);
       break;
     }
    }
    void serialEvent (Serial myPort)
    {
    
    
    }
    
    
    ```
    
  • Sorry, I have no idea why my code isnt showing up in a code box :(

  • I just found that, fixing now

  • edited April 2015 Answer ✓

    one way:

          case 112:
            stateA = 1;
            break;
          case 113:
            stateA = 2;
            break;
          case 114:
            stateB = 1;
            break;
            }
            //etc          
             --------------------------
            if (stateA == 1){
               fill(255);
               textAlign(LEFT );
               text("ON", 400, 50);
            }
            if (stateA == 2){
               fill(255);
                textAlign(LEFT);
                text("OFF", 400, 50);
            }
            if (stateB == 1){
               fill(255);
               textAlign(LEFT );
               text("ECO", 400, 250);
            }
    
  • Thank you so much!

  • edited April 2015

    My own super tweaked version too: 3:-O

    // forum.processing.org/two/discussion/10264/storing-a-serial-value
    
    import processing.serial.Serial;
    
    static final String[] MSGS = {
      "ON", "OFF", "ECO", "TRACK", "DRAG", "STREET"
    };
    
    static final int OFFSET = 112;
    String msg = "VOID";
    
    void setup() {
      size(640, 480, JAVA2D);
    
      smooth(4);
      noLoop();
      noStroke();
    
      textFont(createFont("Aharoni-Bold", 30, true));
    
      new Serial(this, "COM3", 57600).bufferUntil(ENTER);
    }
    
    void serialEvent(Serial s) {
      int val = int(s.readString().trim()) - OFFSET;
      print(val, TAB);
    
      if (val >= 0 & val < MSGS.length) {
        redraw = true;
        msg = MSGS[val];
        println(msg);
      } else println();
    }
    
    void draw() {
      background(0);
    
      textAlign(RIGHT);
      fill(0350);
      text("Performance Mode:", 300, 50);
      text("ECU Mode:", 300, 250);
      text("EDC Mode:", 300, 450);
    
      textAlign(LEFT);
      fill(#00FF00);
      text(msg, 400, 50);
    }
    
  • That Seems to drop all values into a single field, Would making more Arrays solve that issue?

  • edited April 2015

    I didn't get what you meant. Has it worked? Is it buggy?
    Pay attention at what's being print()ed in the console and check whether it matches what's expected!
    I can't test it b/c I don't have the hardware for it!

Sign In or Register to comment.