.. ,

. . . ,,

week two

coding (almost) anything

. . , .





learning about the seeed XIAO

This week we were focused on coding our respective devices given to us. I recived the Seeed XIAO board and looked into how it works via the documentation. It seems to be very similar to most of the arduino boards I have used before with the exception of RGB LED light which seems like a very helpful addition for state tracking and such.


Image 1

. . , .



coding the seeed XIAO

The first step was to code the XIAO to do something with its LEDs. Initally I ran the blink funtion on the three color LED but then I wanted program the RGB LED. I wasn't sure what to do at first, but i figured it would be most interesting to have the lights blink to the beat of a song. I initally wanted to analyze a mp3 of a song within arduino and then send the signals to the XIAO, but i realized that was a bit too much of a challange. So instead, I decided to ask chatGPT to generate the beat sequence for the song that was stuck in my head that day - "Hopelessly Devoted to You" from the Grease musical.


                
                    #include 

                        int Power = 11;
                        int PIN = 12;
                        #define NUMPIXELS 1
                        
                        Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
                        
                        // eat timings in milliseconds for the song "Hopelessly Devoted to You" generated by chatGPT
                        int beats[] = {
                          500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                          500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                          500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                          500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                          500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500
                        };
                        int numBeats = sizeof(beats) / sizeof(beats[0]);
                        int beatIndex = 0;
                        
                        void setup()
                        {
                          pixels.begin();
                          pinMode(Power, OUTPUT);
                          digitalWrite(Power, HIGH);
                        }
                        
                        void loop() {
                          if (beatIndex < numBeats) {
                            pixels.clear();
                            pixels.setPixelColor(0, pixels.Color(15, 25, 205));
                            delay(beats[beatIndex] / 2);
                            pixels.show();
                            pixels.clear();
                            pixels.setPixelColor(0, pixels.Color(103, 25, 205));
                            delay(beats[beatIndex] / 2);
                            pixels.show();
                            beatIndex++;
                          } else {
                            delay(5000);
                            beatIndex = 0;
                          }
                        }
                
            



. . , .

Next, I tried to make there be an output to an exterior element. I decided the simplest output would be a light and I tested the "fade" preset from the library. I had a lot of trouble to get the light connected as I did not realize I needed to add a D in front of the pin name for a digital pin - credit to some of my classmate's websites who I looked at and found the solution!


                    
                        #include 
    
                            int Power = 11;
                            int PIN = 12;
                            #define NUMPIXELS 1
                            
                            Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
                            
                            // eat timings in milliseconds for the song "Hopelessly Devoted to You" generated by chatGPT
                            int beats[] = {
                              500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500
                            };
                            int numBeats = sizeof(beats) / sizeof(beats[0]);
                            int beatIndex = 0;
                            
                            void setup()
                            {
                              pixels.begin();
                              pinMode(Power, OUTPUT);
                              digitalWrite(Power, HIGH);
                            }
                            
                            void loop() {
                              if (beatIndex < numBeats) {
                                pixels.clear();
                                pixels.setPixelColor(0, pixels.Color(15, 25, 205));
                                delay(beats[beatIndex] / 2);
                                pixels.show();
                                pixels.clear();
                                pixels.setPixelColor(0, pixels.Color(103, 25, 205));
                                delay(beats[beatIndex] / 2);
                                pixels.show();
                                beatIndex++;
                              } else {
                                delay(5000);
                                beatIndex = 0;
                              }
                            }
                    
                



. . , .

Finally I wanted to transition to something that would make the song more evident. As I am a ballroom dancer, it would be fun to have music play along with a light that flashes to the beat to make it easier to follow. I did not have a speaker module available but I got my hands on a buzzer. I again consulted chatGPT for the notes and the melody.

For some reason. the buzzer plays music and then just taps out a beat for a period and then returns to making noise, which I could not figure out how to fix.
                
                    #include 

                        #include 

                            // Note frequencies in Hertz
                            #define NOTE_B3 247
                            #define NOTE_C4 262
                            #define NOTE_D4 294
                            #define NOTE_E4 330
                            #define NOTE_F4 349
                            #define NOTE_G4 392
                            #define NOTE_A4 440
                            #define NOTE_B4 494
                            #define NOTE_C5 523
                            #define NOTE_D5 587
                            #define NOTE_E5 659
                            #define NOTE_F5 698
                            #define NOTE_G5 784
                            #define NOTE_A5 880
                            #define NOTE_B5 988
                            #define NOTE_C6 1046
                            
                            int Power = 11;
                            int PIN = 12;
                            #define NUMPIXELS 1
                            
                            Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
                            
                            // Beat timings in milliseconds for the song "Hopelessly Devoted to You" from chatGPT
                            int beats[] = {
                              500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500,
                              500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500, 500, 1000, 500
                            };
                            int numBeats = sizeof(beats) / sizeof(beats[0]);
                            int beatIndex = 0;
                            
                            int buzzerPin = D10; // Pin 0 is used for the buzzer
                            
                            // Melody for "Hopelessly Devoted to You" using the note frequencies  from chatGPT
                            int melody[] = {
                              NOTE_E5, NOTE_D5, NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_D5,
                              NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_G4
                            };
                            
                            int noteDurations[] = {
                              4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4
                            };
                            
                            void setup() {
                              pixels.begin();
                              pinMode(Power, OUTPUT);
                              digitalWrite(Power, HIGH);
                            
                              pinMode(buzzerPin, OUTPUT);
                            }
                            
                            void loop() {
                              if (beatIndex < numBeats) {
                                pixels.clear();
                                pixels.setPixelColor(0, pixels.Color(15, 25, 205));
                                tone(buzzerPin, melody[beatIndex], 1000 / noteDurations[beatIndex]);
                                delay(beats[beatIndex] / 2);
                                noTone(buzzerPin);
                                pixels.show();
                                pixels.clear();
                                pixels.setPixelColor(0, pixels.Color(103, 25, 205));
                                delay(beats[beatIndex] / 2);
                                pixels.show();
                                beatIndex++;
                              } else {
                                delay(5000);
                                beatIndex = 0;
                              }
                            }
                
            




. . , .


!! 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ??