Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Me: write me Arduino code for a device which blinks an LED and makes the blink rate faster each time a button is pressed */
- /* ChatGPT: Here is an example of Arduino code that will blink an LED and increase the blink rate each time a button is pressed: */
- const int buttonPin = 2; // pin for the button
- const int ledPin = 13; // pin for the LED
- int blinkInterval = 1000; // interval at which to blink (milliseconds)
- // variables for tracking the button state
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- void setup() {
- // set the LED pin as output
- pinMode(ledPin, OUTPUT);
- // set the button pin as input
- pinMode(buttonPin, INPUT);
- }
- void loop() {
- // read the button state
- buttonState = digitalRead(buttonPin);
- // check if the button state has changed
- if (buttonState != lastButtonState) {
- // check if the button is pressed
- if (buttonState == HIGH) {
- // decrease the blink interval
- blinkInterval = max(blinkInterval - 100, 100);
- }
- // save the current button state as the last button state
- lastButtonState = buttonState;
- }
- // turn the LED on
- digitalWrite(ledPin, HIGH);
- // delay for the specified interval
- delay(blinkInterval);
- // turn the LED off
- digitalWrite(ledPin, LOW);
- // delay for the specified interval
- delay(blinkInterval);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement