You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Espruino has a nice feature on pins: it has a configuration - called auto - where it configures itself as input or output depending on the read or write operation...

    This nice feature plays now dirty tricks on you.... ;)

    When you define the pin as output, you will be able to read from the pin and you get the output register, because the driver of pin stays on (not three-state) and drives the input driver. ...and you get what you are looking for: status of the pin / LED... to be more precise: the output register... (if you short cut your pin, I bet you get the either rail... close to 0 or 3.3, I expect... I did not tell you to try... that's why I just guess. (Tried to find details about the inner circuitry to confirm, but could not find it).

    Give it a try...

    EDIT: just validated (on Espruino Wifi, expect Puck not to be different, but I may be mistaken... :( ...) ...in the console (on Espruino-Wifi, LED / LED1 - red LED - is on B2, therefore, B2 and LED and LED1 are interchangeable...):

    >getPinMode(LED1); // asking pin mode on LED / LED1 / B2 pin
    ="analog"
    >pinMode(B2,"output"); // setting pin mode on LED / LED1 /B2 pin to plain ouput
    =undefined
    >getPinMode(LED1); //  asking again, and it shows set mode
    ="output"
    >digitalRead(LED1); // reading, 0 expected, because of wiring and LED is off
    =0
    >LED1.set(); // turning LED / LED1 / B2 pin on
    =undefined
    >digitalRead(LED1);  // reading, 1 expected, because of wiring and LED is on
    =1
    >
    

    As noticed, pin mode (of B2, LED, LED1 on Espruino) is by default "analog" mode (Espuio-Wifi after reset / power-on / connect with 'factory' setup - no saved code of mine running on start up).

    Analog mode may have another twist: since it can be PWM and not really analog, reading may be fickle... If you for straight, plain "output", it just works...

    Without setting the pin mode, but turning the LED on, what happens when you read? ...I expect the LED turns of... (so is behavior of Espruino-Wifi):

    >pinMode(B2,"auto"); // set outo
    =undefined
    >getPinMode(B2); // returns what it was from before... 
    ="output"
    >B2.set(); // turns LED on
    =undefined
    >digitalRead(B2); // switches to input, but on 1st read I get 1 because it reads so fast and 'sensitively' that the voltage has not yet collapsed... very interesting - BUT LED EVENTUALLY TURNS OFF
    =1
    >digitalRead(B2); // 2nd time I get 0... as expected... because all the electrons have left the scene / left the building...
    =0
    >
    

    (...my little Christmas present to you - Merry Christmas!)

About

Avatar for allObjects @allObjects started