gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
code to make an arduino 4.0 wifi connected to a buzzer play a melody
- User
code to make an arduino 4.0 wifi connected to a buzzer play a melody
- Assistant
Here's a code example that plays the "Super Mario Bros" theme using an Arduino 4.0 WiFi and a buzzer connected to pin 8:
#include <WiFiNINA.h> // Melody notes int melody[] = { NOTE_E7, NOTE_E7, 0, NOTE_E7, 0, NOTE_C7, NOTE_E7, 0, NOTE_G7, 0, 0, 0, NOTE_G6, 0, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0, NOTE_C7, 0, 0, NOTE_G6, 0, 0, NOTE_E6, 0, 0, NOTE_A6, 0, NOTE_B6, 0, NOTE_AS6, NOTE_A6, 0, NOTE_G6, NOTE_E7, NOTE_G7, NOTE_A7, 0, NOTE_F7, NOTE_G7, 0, NOTE_E7, 0, NOTE_C7, NOTE_D7, NOTE_B6, 0, 0 }; // Note durations int noteDurations[] = { 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 6, 3, 12, 12, 12, 12, 12, 12 }; // WiFi credentials char ssid[] = "your_wifi_ssid"; char pass[] = "your_wifi_password"; WiFiClient client; void setup() { // Connect to WiFi network WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(1000); } // Set pin 8 as an output pinMode(8, OUTPUT); } void loop() { // Play the melody for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) { int noteDuration = 1000 / noteDurations[i]; tone(8, melody[i], noteDuration); // Pause between notes int pauseDuration = noteDuration * 1.30; delay(pauseDuration); // Stop playing the tone noTone(8); } // Wait for 5 seconds before playing the melody again delay(5000); }