Try just printing the value of pinState. You declared it as an integer, but you are looking at it to be "HIGH" like a boolean. Integers are usually numbers.
I'm missing something here.
The Arduino docs say digitalRead returns HIGH or LOW. It is an integer value but is supposed to be restricted to those 2 values.
digitalRead, HIGH and LOW are defined in the Arduino header as
int digitalRead(uint8_t pin); #define HIGH 0x1 #define LOW 0x0
The change is to not check for HIGH or LOW and that seems to work.
Say what?
digitalRead is not guaranteed to return HIGH or LOW?
The one who has the most fun, wins!
It does just that. My original code was in excess of what I needed so this suggestion was to simplify it to what is really required. I was trying to debug code that was working. Me being a hardware guy took reference to a pin to be physical pin. The compiler just disagreed.
Later I'll define the digitalRead variables as boolean.
It does just that. My original code was in excess of what I needed so this suggestion was to simplify it to what is really required. I was trying to debug code that was working. Me being a hardware guy took reference to a pin to be physical pin. The compiler just disagreed.
I'm not understanding this at all.
By "it" do you mean digitalRead or your code?
Let me check my premise: digitalRead returns an integer that is restricted to either HIGH or LOW.
The code from github for digitalRead (return values highlighted):
int digitalRead(uint8_t pin)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
if (port == NOT_A_PIN) return LOW;
// If the pin that support PWM output, we need to turn it off
// before getting a digital reading.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
if (*portInputRegister(port) & bit) return HIGH;
return LOW;
}
So digitalRead does indeed return only 2 integer values. There's an implicit conversion of the constants for HIGH and LOW to integer.
The original, non-working code was
const int inPin = 10;
int pinState = 0;
void setup() {
//...
pinMode(inPin, INPUT_PULLUP); //use internal pullup
}
void loop() {
pinState = digitalRead(inPin);
if (pinState == HIGH)
{
Serial.println("1");
}
else {
Serial.println("0");
}
delay(1000);
}
The changed, now working code is
//...
void loop() {
pinState = digitalRead(inPin); // reading D10 not pin 10
Serial.println(pinState);
delay(2000);
}
So, by not checking the return value of digitalRead, the circuit worked?
Could you post the output from this program?
The one who has the most fun, wins!
Both ways if coding work. The code is fine. Never was broke. I was toggling the wrong input pin low and high on the Nano. I'd post a video but 10mb limit...
Both ways if coding work. The code is fine. Never was broke. I was toggling the wrong input pin low and high on the Nano. I'd post a video but 10mb limit...
Ah! A circuit error. OK, that adds up. I was worried I was losing my mind.
The one who has the most fun, wins!
So it's been 30 years since I wrote any code and even then not much before my career path took me away. I ended up heading up our electronics hardware design and engineering teams. Then I nixed all of that for a career in medicine. So I'm back retired and at where it all began. Tons of Arduino videos later I'm designing a universal transceiver controller to adapt to any crystal controlled radio for frequency agility. No CB radios, but commercial two way stuff that is sitting who knows where. I have a few in my stash so overfilling the effort to save a few. Perhaps others may be inspired when I'm done.