Hello,
I am using a Nano manufactured by LAFVIN (suckered into buying 3 for $13). Unfortunately, when I read the digital pins these pins are always in HIGH state even if I have an inline button to disable the connectivity. I suspect that my interpretation of the pinout diagrams is incorrect.
I used the standard Arduino button example with the following modifications:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = LED_BUILTIN; // the number of the LED pin
const int laserPin = 5;
// variables will change:
// int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
pinMode(laserPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(5000);
digitalWrite(ledPin, LOW);
delay(5000);
}
void loop() {
// read the state of the pushbutton value:
int buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
digitalWrite(laserPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
digitalWrite(laserPin, LOW);
}
}
Both the BUILTIN_LED and external one on 5 (as shown in the above code) respond correctly to statements in the code but the digitalRead for buttonPin is always HIGH irrespective of assignment to other digital pins. I have tried all three boards that came in the package and their performance is identical. The same code runs correctly on my UNO by which my conclusion is that my understanding of the pin mapping in Nano is naive.
I want the LEDs to be lit only when pinButton is pressed (i.e. HIGH) but it seems that digitalRead is always returning HIGH even if the button is not pressed. Doesn't make sense to me except that I am using incorrect pin assingments for the button. I didn't do a digitalWrite in the first few iterations because I have a 100K resistor in the circuit from the button pin (on the board's digital pin side) to GND but the digitalWrite (for internal pullup) seems to have no effect.
I would like to read the value of the button's status. Please let me know what changes in the code are needed. Thanks.
Kind regards.