I'm fairly sure my code works, but it doesn't run well on tinkercad for some reason. is it the code or the site itself?
int buttonPin = 2; // set variables for component pins
int redLED = 4;
int buttonPress; // declare variable for the button state
char answer;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(redLED, OUTPUT);
}
void loop() {
buttonPress = digitalRead(buttonPin); // set variable to button pin reading
if (buttonPress == HIGH) { // if button is pressed, turn LED on
answer = ShouldItlight();
if (answer == 'Y') {
digitalWrite(redLED, HIGH);
} else if (answer == 'N') {
digitalWrite(redLED, LOW); // otherwise, LED is off
}
}
}
char ShouldItlight() {
char yesorno;
Serial.println(" press y to light the button.");
while (!Serial.available()) {}
yesorno = Serial.read();
return yesorno; //returns a character
}