I purchased an Arduino Uno yesterday. I am doing the button blink tutorial (led turns off when I push a button) the issue I am having is that whenever I push the button, the Arduino disconnects from my PC, causing it to lose power. I have a 20 second timer after the button press that I would like to run, but it isn’t working. Here is my code:
// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int irsensor = 4; // The number of the pin for the IR sensor
// 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);
// initialize the ir sensor pin
pinMode(irsensor, OUTPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED off:
digitalWrite(ledPin, LOW);
digitalWrite(irsensor, LOW);
delay(20000);
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
digitalWrite(irsensor, HIGH);
}
}