/* Button-Controlled LED Toggle with Analog Read Toggles the LED each time the button on D10 is pressed. Reads from analog pin D0 when the LED is on. */ int val = 0; //int lastButtonState = 1; //bool ledState = 1; void setup() { pinMode(LED_BUILTIN, OUTPUT); // Set up LED pin pinMode(D10, INPUT_PULLUP); // Button with internal pull-up Serial.begin(115200); //115200 Serial.setTxTimeoutMs(0); } void loop() { // Read button state int buttonState = digitalRead(D10); if (buttonState ==0){ digitalWrite(LED_BUILTIN, 1); val = analogRead(D0); delay(200); Serial.println(val); // Print analog value } else{ val=0; digitalWrite(LED_BUILTIN, 0); delay(200); Serial.println(val); // Print analog value } // // If button is pressed, // if (buttonState == 0 && lastButtonState == 1) { // ledState = !ledState; // } // lastButtonState = buttonState; // Update last button state // // If LED is toggled on, read analog and print value // if (ledState == 0) { // digitalWrite(LED_BUILTIN, ledState); // val = analogRead(D0); // Read analog pin D0 // Serial.println(val); // Print analog value // } else { // digitalWrite(LED_BUILTIN, LOW); // Ensure LED is off if ledState is 0 // } }