• I know this certainly a stupid question.

    i'm prototyping something for my students
    i use a V53L1X connected to my pico.
    i would like to transfer the datas to Processing.
    i connect the Pico on the usb plug.

    should i use
    console.log or print
    to transmit my data?

    should i initialize a serial port?
    Serial1.setup(115200);

    some time the console send error from the program (i2c init);
    i'm really confuse, it should be simple, but it's not.
    sometime nothing arrive to Processing.

    i've got an other problem, the string received start with an invisible char and [J

    How should i do to have a clean string with distance value?

    Could i use midi thru usb to send datas?
    i should split my value [0-4000] in 2 set of 7bits (midi value)

    thanks for your help

    éric

    let laser;
    
    function lis(){
      let somme=0;
      var dist = laser.performSingleMeasurement().distanc­e;
      //print(dist);
      console.log(dist);
      //console.log("distance: ",dist," lissée:",somme/table.length);
      analogWrite(LED1, (300-dist)/300, { soft:true, freq:200 });
    }
    
    function onInit() {
      Serial1.setup(115200);
      I2C2.setup({sda:B3,scl:B10});
      laser = require("VL53L1X").connect(I2C2);
      console.log("init");
      digitalWrite(B4,1); // set XSDN -> turn the sensor on
      setInterval(lis, 80);
    }
    

    and the processing sketch

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    int val;      // Data received from the serial port
    
    void setup() 
    {
      size(200, 200);
      // le port de l'espruino sur mon mac est en 3
      String portName = Serial.list()[3];
      myPort = new Serial(this, portName, 115200);
    }
    
    void draw(){
      while (myPort.available() > 0) {
        String inBuffer = myPort.readStringUntil('\r');
        if (inBuffer != null) {
          String[] q = splitTokens(inBuffer, "[J");
          if (q.length > 1) {
            val = int(float(q[1]));
            println(val);
          }
        }
      }
    }
    
  • In my opinion console is for entering commands and logging errors, data like this can better go directly to the right Serial object while the real console is switched away - that would give you clean output without any surprises.

    see also http://www.espruino.com/Reference#Serial­ - methods print,println,write

    USB port serial is USB object, not Serial1 and you don't need to setup speed for USB

  • USB object is an instance of Serial
    https://www.espruino.com/Reference#l__gl­obal_USB

    There is no exemple! to initiate the USB

    have you one?

    should i use this?

    E.on('init', function() {
      USB.setConsole();
    });
    

    Or USB.setConsole(true); ?

    regards

    é.

  • there is no need to initialize USB , just attach device to computer and it should be automatically enabled. And the idea I mentioned was to actually switch console away from USB so better something like Serial1.setConsole() or LoopbackA.setConsole()

    Of course after running this you cannot connect over USB via Espruino IDE, this is for final state when the project runs as designed. However you can make this conditional on something (button or some other pin high/low). Also I think when you hold button while inserting pico to USB it may avoid starting your code at all so that can be the way to connect IDE and change stuff

  • I had a switchto switch on/off setConsole on USB or on an other Serial
    I use print non console.log

    i've tried this

    let laser;
    
    function lis(){
      let somme=0;
      var dist = laser.performSingleMeasurement().distanc­e;
      print(dist);
      //console.log(dist);
      //console.log("distance: ",dist," lissée:",somme/table.length);
      analogWrite(LED1, (1000-dist)/1000, { soft:true, freq:200 });
    }
    
    function onInit() {
      //Serial1.setup(115200);
      I2C2.setup({sda:B3,scl:B10});
      laser = require("VL53L1X").connect(I2C2);
      print("init");
      digitalWrite(B4,1); // set XSDN -> turn the sensor on
      setInterval(lis, 150);
      pinMode(A6, 'input_pulldown');
      setWatch( function (e) {
        console.log(e.state);
        if (e.state) {
          Serial1.setConsole();
        } else {
          USB.setConsole(true);
        }
      }, A6, { repeat:true, edge:"both", debounce: 100 });  
    }
    

    if i move the console to another Serial...
    nothing come in Processing
    otherwise my output is always crippy

    
    >
    [J1587
    
    

    1587 is my number

    :v

    Any idea

  • print is basically same as console.log both write to console
    https://www.espruino.com/Reference#l__gl­obal_print
    try stuff mentioned in post #2

  • should i use console.log or print

    These write to the console, but they also have to add extra chars to remove the > prompt, add a newline, print what you want and then put > back after.

    If you want to write direct, use USB.println(...) and you'll avoid all the extra stuff getting written

  • @Gordon Thanks
    i'll try tomorrow
    I'm able to clean the output, but i was looking for a way to have a clean output (easer to explain the code to beginners)

    regards

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

connecting a pico to a computer and Processing.org with serial

Posted by Avatar for Mrbbp @Mrbbp

Actions