Issues with tinkercad

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
  }

What exactly does that mean ?

When I run the simulation, the code doesn't happen. Like what's supposed to happen doesn't, but it goes through.

how do you have your button wired up? It will require a pull-up or pull-down resistor. Do you have one installed? Much better to use the built-in pull-up resistors.

How do you have your Serial monitor configured? I do not see any Serial.begin(9600); in your setup() function so you did not initialize your Serial communication. Since you are reading just one character and not dealing with any terminating CR or LF, make sure the Serial Monitor is configured at 9600 baud and no line endings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.