Is there a way to detect the output state of a digital pin?
i.e. if part of the program sets the output pin HIGH, how can I detect this in a different part of the program? Is there a way to get the current state of an output or should I simply set a variable at the same time and check the variable? Same question if useing the pin as PWM.
Also, is HIGH = 1 and LOW = 0?
is this;
digitalWrite(ledPin, HIGH);
the same as this;
digitalWrite(ledPin, 1);
As far as I know, there is no getPinMode() but one could be written. It would require knowledge of how pinMode() is written, to get the correct bits of the correct port register.
As for HIGH or LOW, why not try Serial.print(HIGH, DEC) back to your PC to see? The definition of HIGH is in hardware/cores/arduino/wiring.h if you want to see for yourself, but many such questions can be answered with a quick five line sketch. Experimentation is learning.
As for HIGH or LOW, why not try Serial.print(HIGH, DEC) back to your PC to see? The definition of HIGH is in hardware/cores/arduino/wiring.h if you want to see for yourself, but many such questions can be answered with a quick five line sketch. Experimentation is learning.
Thanks, I should have mentions that I am unable to test anything atm, otherwise - yes, I would have.
As for the pinMode() it sounds like simply setting a variable, then use that var to set the pin will be just as easy. I can test the variable then to see what state the pin is in, or should be in. Maybe digitalRead() should be able to read the state of an output pin.
What are your thought of reading the value of a digital pin used as a PWM output? Can I try it with analogRead()?
Aside from direct shorts, could I damage the Arduino by testing stuff in the software? I got a bunch of other questions too, but I can test it myself later - just don't know if I should stay away from anything. SO far everything I have read on the Arduino site hasn't mentioned possible damage so unless I short a pin or something external, I think I am ok.
What are your thought of reading the value of a digital pin used as a PWM output? Can I try it with analogRead()?
The "value" of a digital pin used as a PWM output is either HIGH or LOW at any given time. If you do an analogWrite(), you're not writing a value to the pin, you're writing a number to the comparator that will write HIGH and LOW to the pin at different times.
The analogRead() (ADC) is not related to analogWrite() (PWM) at all. Analog pins A0 to A5 have ten-bit Analog/Digital Converters (ADCs). The digital PWM pins D3,D5,D6,D9,D10,D11 are attached to PWM numerical comparators. Counter to the confusing name analogWrite(), PWM pins do not actually output analog values (voltage levels between HIGH and LOW).
unless I short a pin or something external, I think I am ok
In general, yes.
Just be sure you're not connecting a pin that you've configured as an OUTPUT to +5V or GND or another OUTPUT without something to limit current (like a resistor). One of the first things the chip does on reset is to set all i/o pins as INPUTs in part to reduce this risk.