I have a very simple code for a controller I'm working on for a robot.
void setup() {
// put your setup code here, to run once:
pinMode(3,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(digitalRead(3));
}
The only problem is that I can't get a constant reading on switches because when I flick the switch I will get a constant stream of 1s, but when I flick the switch off I get a sporadic stream of ones and zeros. I have noticed though that if I put my finger on the USB port leading from my Arduino Nano I get a constant stream of 1s when on and zeros when off.
Does anyone have a recommendation to get around this problem or simulate touch on the USB port?
pinMode(pin, INPUT_PULLUP)
, you are activating the Arduinos internal pullup resistor.