How can I digitalRead a pin that is in pinMode OUTPUT?

后端 未结 11 1044
不知归路
不知归路 2020-12-09 02:30

I have a very simple test sketch in which I\'m trying to set a pin to HIGH and then read its state with digitalRead. Here is my sketch.

<         


        
11条回答
  •  攒了一身酷
    2020-12-09 03:17

    I wrote a routine for flashing four different LEDs for different purposes but I also wanted to retain their initial state before I flash them, as their steady state also tells me something is occurring in my code, so here is my Flash code, you call it by sending the pin number, the number of times to flash it and how long to flash it for.

    inline void Flash (byte pinNum, byte times, byte flashSpeed)
      {
        bool state;                     // Local var for State of pin
        state = digitalRead(pinNum);    // Read the current state as set elsewhere
        int x;                          // Local var for repeat flash
        for (x = 0; x < times; x++)
          {
            digitalWrite(pinNum, HIGH); // Turn on LED
            delay(flashSpeed);
            digitalWrite(pinNum, LOW);  // Turn off LED
            delay(flashSpeed * 2);      //  leave off twice as long as on
          }                             //  due to persistence of Vision   
        digitalWrite(pinNum, state);    // Restore the original state of the LED
      }
    

提交回复
热议问题