int switchPin = 33;
int led = 13;
int currentState;
int lastState;
String msg = "one";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//inputpullup stops flutter from 1 and 0 when button not pressed
pinMode(switchPin, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
lastState = 1;
//Getting current state
currentState = digitalRead(switchPin);
Serial.println(currentState);
if (lastState != currentState) {
//If button has been pressed this code will run
if (led == LOW) {
digitalWrite(led, HIGH);
Serial.println("LED HIGH");
}
}
lastState = currentState;
}
Thanks both for your quick response it is now working.
Here is my working code:
int switchPin = 33;
int led = 13;
int ledState;
int currentState;
int lastState;
String msg = "one";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//inputpullup stops flutter from 1 and 0 when button not pressed
pinMode(switchPin, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
lastState = 1;
//Getting current state
currentState = digitalRead(switchPin);
ledState = digitalRead(led);
Serial.println(currentState);
if (lastState != currentState) {
//If button has been pressed this code will run
if (ledState == HIGH) {
digitalWrite(led, LOW);
Serial.println("LED LOW");
}
if (ledState == LOW) {
digitalWrite(led, HIGH);
Serial.println("LED HIGH");
}
}
}