gpt-4-0314· openai
Suzhen/CodeChat
Flashing LEDs: 8 LEDs are connected to the pins according to the figure below, write…
- User
Flashing LEDs: 8 LEDs are connected to the pins according to the figure below, write first a Pseudocode and then a Arduino programming language code for a Adafruit HUZZAH32-ESP32 Feather that performs the following sequence:
- When the LEDs connected to 0, 1, 2 and 3pins are lit, the others LEDs (connected to 4, 5, 6, 7 pins) should be off and vice versa. Use timer instead of delay. The "Start / stop" button should be used to start and stop flashing.
- Assistant
Pseudocode:
- Initialize the pins and variables
- Configure the pins as OUTPUT
- Initialize the timer
- Start the timer
- Read the "Start/stop" button
- If the button is pressed, start flashing the LEDs
- Flashing sequence: a. Turn on LEDs connected to 0, 1, 2, and 3 pins b. Turn off LEDs connected to 4, 5, 6, and 7 pins c. Wait for the timer to reach the desired duration d. Reset the timer e. Turn off LEDs connected to 0, 1, 2, and 3 pins f. Turn on LEDs connected to 4, 5, 6, and 7 pins g. Wait for the timer to reach the desired duration h. Reset the timer
- Repeat steps 5 to 7
Arduino code:
#include <Ticker.h> const int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7}; const int buttonPin = 8; const int flashingInterval = 500; // in milliseconds bool flashing = false; bool state = false; Ticker timer; void setup() { for (int i = 0; i < 8; i++) { pinMode(ledPins[i], OUTPUT); } pinMode(buttonPin, INPUT_PULLUP); timer.attach_ms(flashingInterval, toggleLEDs); } void loop() { if (digitalRead(buttonPin) == LOW) { flashing = !flashing; delay(300); // To avoid button debounce } } void toggleLEDs() { if (!flashing) { return; } state = !state; for (int i = 0; i < 4; i++) { digitalWrite(ledPins[i], state); } for (int i = 4; i < 8; i++) { digitalWrite(ledPins[i], !state); } }