int delaytime = 500;
int buttonstatus = 8;
int val = 0;
int pin = 2;
void setup() {
// put your setup code here, to run once:
pinMode(pin,OUTPUT);
pinMode(buttonstatus,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
val=digitalRead(buttonstatus);
delay(delaytime);
if(val == HIGH){
digitalWrite(pin,HIGH);
//Serial.println(" Hello World ");
//Serial.println();
if(pin==2){Serial.println("TOMTE");delay(delaytime);}
}
}
But when i upload it. It does nothing.
But if remove the pin 8 on the board, and then put it back in. Then it works.
Goal is to just learn how to trigger code based on a button.
You have the button input configured as INPUT and you are testing for HIGH if the button is pressed. This implies the button is connect to +5/3.3V (depending on board) with a pull-down resistor on the input. Is this the way you have it wired? In general, it is much easier to have the input configured as INPUT_PULLUP with the button connected to GND. This way you don't need an external resistor.
Also, I notice you have a delay of 500ms after reading the button. What is the purpose of this? Usually it is more desirable to detect when the button BECOMES pressed rather than when the button IS pressed. The following tutorial will help you:
behedwin:
Thank you. I got that example to work. But tbh i do not understand why i need to use the 5v pin on the board.
I thought i had things figured out
Actually, if you change that example to look like below and just wire the button to GND you do NOT need an external resistor. I don't know why they wrote the example that way.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is LOW then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is HIGH then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}