• Complete electronics/embedded newbie here, so apologies if I'm just missing something really obvious...

    I'm trying to set up a system where when I connect two Picos, one realises the connection has occurred and sends some data to the other over Serial1.

    The simple way to achieve this I've found is to have both Picos connected via a Serial1 connection (9600/tx:B6/rx:B7), and then connect the 3.3v pin of Pico A to pin B3 of Pico B, and just use setWatch() on Pico B to detect pin B3 being pulled high when they connect.

    This seems to work fine - every time I connect B's B3 to A's 3.3v the watcher triggers exactly once.

    The problem occurs when I try to print text over Serial1 as a result - suddenly Pico B's setWatch() is continually being triggered, as fast as the debounce setting will permit it.

    If I put a counter in and (for example) only output to Serial1 if(counter < 5), I get the expected number of outputs and no more, so obviously outputting to Serial1 isn't simply triggering the setWatch() event handler endlessly - it's doing it exactly once per write to Serial1... and then the event handler writes to Serial1, re-triggering the setWatch() event handler... etc, etc, etc.

    There's no obvious connection I can see between B3 and Serial1 on B6/B7 - can anyone explain what on earth I'm missing?

    Example code follows - B6/B7 each lead to the other pin on another Pico, and I trigger the behaviour by connecting that other Pico's 3.3v pin to B3 on this Pico:

    // Convenience function so I can tell when it's transmitting/listening
    function setTransmitting(transmitting) {
      if(transmitting) {
        digitalWrite(LED1, 1);  // Red = transmitting
        digitalWrite(LED2, 0);  // Green = listening
      }
      else {
        digitalWrite(LED1, 0);
        digitalWrite(LED2, 1);
      }
    }
    
    // Set everything up (so it can be easily called from onInit or the REPL as necessary)
    function setUp() {
    
      USB.setConsole();
    
      /**** Set up inputs ****/
    
      pinMode(B3, 'input_pulldown');  // Listen for connection (other Pico's 3.3v pin pulls B3 high on connection)
    
      /**** Set up outputs ****/
    
      Serial1.setup(9600, { tx:B6, rx:B7 });  // Serial data connection
      setTransmitting(false);                 // Display indicator
    
      /**** Behaviours ****/
    
      setWatch(function(e) {  // On connection, output data
        setTransmitting(true);
    
        Serial1.print('Hello!');
    
        setTransmitting(false);
      }, B3, { repeat: true, edge: 'rising', debounce:500 });  
    }
    
    E.on('init', function() {
      setUp();
    });
    

    TL;DR: Basically it seems as if sending data over Serial1 resets the setWatch() on pin B3, leading to it triggering the event handler even if B3 was already high before the Serial write occurred. Why would writing to Serial1 re-trigger a watch on pin B3?

    P.S. Apologies for the bad title - I only realised after posting that Serial1 was triggering the watch on B3 exactly once per write (and the event handler was then re-sending to Serial1 leading to an infinite loop), and not continually.

About

Avatar for jtq @jtq started