Advertisement
igendel

ChatGPT Arduino Accelerating Blinky, Attempt #1

Jan 7th, 2023
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Me: write me Arduino code for a device which blinks an LED and makes the blink rate faster each time a button is pressed */
  2.  
  3. /* ChatGPT: Here is an example of Arduino code that will blink an LED and increase the blink rate each time a button is pressed: */
  4.  
  5. const int buttonPin = 2;    // pin for the button
  6. const int ledPin = 13;      // pin for the LED
  7. int blinkInterval = 1000;   // interval at which to blink (milliseconds)
  8.  
  9. // variables for tracking the button state
  10. int buttonState = 0;    // current state of the button
  11. int lastButtonState = 0; // previous state of the button
  12.  
  13. void setup() {
  14.   // set the LED pin as output
  15.   pinMode(ledPin, OUTPUT);
  16.   // set the button pin as input
  17.   pinMode(buttonPin, INPUT);
  18. }
  19.  
  20. void loop() {
  21.   // read the button state
  22.   buttonState = digitalRead(buttonPin);
  23.   // check if the button state has changed
  24.   if (buttonState != lastButtonState) {
  25.     // check if the button is pressed
  26.     if (buttonState == HIGH) {
  27.       // decrease the blink interval
  28.       blinkInterval = max(blinkInterval - 100, 100);
  29.     }
  30.     // save the current button state as the last button state
  31.     lastButtonState = buttonState;
  32.   }
  33.   // turn the LED on
  34.   digitalWrite(ledPin, HIGH);
  35.   // delay for the specified interval
  36.   delay(blinkInterval);
  37.   // turn the LED off
  38.   digitalWrite(ledPin, LOW);
  39.   // delay for the specified interval
  40.   delay(blinkInterval);
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement