Singing Arduino

Would you like to surprise someone with a song?

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
Buzzer
Buzzer
× 1
5 mm LED: Red
5 mm LED: Red
× 1
5 mm LED: Green
5 mm LED: Green
× 1
Resistor 330 ohm
Resistor 330 ohm
× 1

Story

Code

#include <WiFi101.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiSSLClient.h>
#include <SPI.h>


char ssid[] = "your network SSID";      //  your network SSID (name)
char pass[] = "your network password";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
int ledpin = 6;
int value = 0;
bool val = true;

/***************************/
/* In English Nomenclature */
/*    Used in Star Wars    */
#define c 261
#define d 294
#define e 329
#define f 349
#define g 391
#define gS 415
#define a 440
#define aS 455
#define b 466
#define cH 523
#define cSH 554
#define dH 587
#define dSH 622
#define eH 659
#define fH 698
#define fSH 740
#define gH 784
#define gSH 830
#define aH 880

/***************************/
/* In Spanish Nomenclature */
const int la = 110; //La
const int d = 131;//Do
const int r = 147;//Re
const int m = 165;//Mi
const int f = 174;//Fa
const int s = 196;//Sol
const int l = 220;//La
const int lS = 233;//La#
const int si = 247;//si
const int Do = 262;//do
const int Re = 294; //re
const int Mi = 329;//Mi
const int Fa = 349;//Fa
const int Sol = 392;//Sol
const int La = 440;//La
const int LaS = 466;//La#
const int Si = 494;//si
const int Doo = 523;//do
/***************************/


const int buzzerPin = 8;
const int ledPin1 = 12;
const int ledPin2 = 13;
int counter;

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(115200);      // initialize serial communication
  Serial.print("Start Serial ");
  pinMode(ledpin, OUTPUT);      // set the LED pin mode
  // Check for the presence of the shield
  Serial.print("WiFi101 shield: ");
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("NOT PRESENT");
    return; // don't continue
  }
  Serial.println("DETECTED");
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    digitalWrite(ledpin, LOW);
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
    digitalWrite(ledpin, HIGH);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
  pinMode(buzzerPin, OUTPUT);
}
void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/S\">here</a> listen to Star Wars<br>");
            client.print("Click <a href=\"/R\">here</a> listen to 'The Rains of Castamere'<br>");
            client.print("Click <a href=\"/C\">here</a> listen to 'Cucaracha'<br>");
            client.print("Click <a href=\"/H\">here</a> listen to 'Happy Birthday'<br>");
            


            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          HappyBirthday();
        }
         if (currentLine.endsWith("GET /S")) {
          StarWars();
        }
        if (currentLine.endsWith("GET /R")) {
          The Rains of Castamere();               
        }
        if (currentLine.endsWith("GET /C")) {
          Cucaracha();               
        }
        
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");

  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

void beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
  if (counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  } else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
  noTone(buzzerPin);

  delay(50);
  counter++;
}

void HappyBirthday() {

  beep(Do, 250);
  beep(Do, 250);
  beep(Re, 500);
  beep(Do, 500);
  beep(Fa, 500);
  beep(Mi, 1000);
  beep(Do, 250);
  beep(Do, 250);
  beep(Re, 500);
  beep(Do, 500);
  beep(Sol, 500);
  beep(Fa, 1000);
  beep(Do, 250);
  beep(Do, 250);
  beep(Doo, 500);
  beep(La, 500);
  beep(Fa, 500);
  beep(Mi, 500);
  beep(Re, 1000);
  beep(LaS, 250);
  beep(LaS, 250);
  beep(La, 500);
  beep(Fa, 500);
  beep(Sol, 500);
  beep(Fa, 1000);

}

void Cucaracha() {
  beep(d, 250);
  beep(d, 250);
  beep(d, 250);
  beep(f, 250);
  delay(250);
  beep(l, 250);
  delay(250);
  beep(d, 250);
  beep(d, 250);
  beep(d, 250);
  delay(250);
  beep(f, 500);
  beep(l, 1250);
  delay(500);
  beep(f, 250);
  beep(f, 250);
  beep(m, 250);
  beep(m, 250);
  beep(r, 250);
  beep(r, 250);
  beep(d, 1000);
  delay(250);
  beep(d, 250);
  beep(d, 250);
  beep(d, 250);
  beep(m, 500);
  delay(250);
  beep(s, 250);
  delay(250);
  delay(250);
  beep(d, 250);
  beep(d, 250);
  beep(d, 250);
  beep(m, 500);
  delay(250);
  beep(s, 1250);
  delay(500);
  beep(Do, 250);
  beep(Re, 250);
  beep(Do, 250);
  beep(lS, 250);
  beep(l, 250);
  beep(s, 250);
  beep(f, 500);
  delay(2000);
  beep(d, 250);
  beep(d, 250);
  beep(f, 250);
  beep(f, 250);
  beep(l, 250);
  beep(l, 250);
  beep(Do, 500);
  delay(250);
  beep(l, 1250);
  delay(250);
  beep(Do, 500);
  beep(Re, 250);
  beep(Do, 250);
  beep(lS, 250);
  beep(l, 250);
  beep(Do, 250);
  beep(lS, 500);
  delay(250);
  beep(s, 1250);
  delay(500);
  beep(d, 250);
  beep(d, 250);
  beep(m, 250);
  beep(m, 250);
  beep(s, 250);
  beep(s, 250);
  beep(lS, 500);
  delay(250);
  beep(s, 1250);
  delay(250);
  beep(Do, 500);
  beep(Re, 250);
  beep(Do, 250);
  beep(lS, 250);
  beep(l, 250);
  beep(s, 250);
  beep(f, 1000);
  delay(2000);

}

void TheRainsofCastamere() {
  
  beep(la, 500);
  beep(f, 750);
  beep(la, 250);
  beep(m, 750);
  beep(la, 250);
  beep(f, 500);
  beep(s, 500);
  beep(m, 750);
  beep(la, 250);
  beep(s, 500);
  beep(f, 500);
  beep(m, 500);
  beep(r, 500);
  beep(m, 2000); 
  beep(l, 250);
  beep(l, 500);
  beep(lS, 250);
  beep(s, 500);
  beep(d, 250);
  beep(d, 250);
  beep(l, 500);
  beep(lS, 500);
  beep(s, 750);
  beep(l, 250);
  beep(lS, 500);
  beep(l, 500);
  beep(s, 500);
  beep(f, 500);
  beep(m, 1500);
  beep(la, 250);
  beep(la, 250); 
  beep(f, 750);
  beep(la, 125);
  beep(m, 750);
  beep(la, 250);
  beep(f, 500);
  beep(s, 500);
  beep(m, 750);
  beep(la, 250);
  beep(s, 500);
  beep(f, 500);
  beep(m, 500);
  beep(r, 500);
  beep(m, 1500);
  beep(d, 500);
  beep(l, 750);
  beep(lS, 250);
  beep(s, 750);
  beep(d, 250); 
  beep(l, 500);
  beep(lS, 500);
  beep(s, 750);
  beep(l, 250);
  beep(lS, 500);
  beep(l, 500);
  beep(s, 500);
  beep(f, 500);
  beep(m, 1500);
  beep(d, 500);
  beep(l, 750);
  beep(lS, 250);
  beep(s, 750);
  beep(d, 250);
  beep(l, 500);
  beep(lS, 500);
  beep(s, 750);
  beep(l, 250);
  beep(lS, 500);
  beep(l, 500);
  beep(s, 500);
  beep(f, 500);
  beep(r, 2000);

  delay(1500);

  beep(la, 500);
  beep(m, 250);
  beep(f, 250);
  beep(r, 1000);
  beep(f, 500);
  beep(f, 250);
  beep(m, 250);
  beep(la, 1000);
  delay(250);
  beep(la, 250);
  beep(f, 250);
  beep(f, 250);
  beep(r, 1000);
  beep(f, 500);
  beep(f, 250);
  beep(m, 1250);
  beep(la, 500);
  beep(m, 250);
  beep(f, 250);
  beep(r, 1000);
  beep(l, 500);
  beep(l, 500);
  beep(s, 500);
  beep(r, 750);
  beep(f, 250);
  beep(f, 750);
  beep(r, 250);
  beep(m, 750);
  beep(f, 250);
  beep(r, 1500);
  beep(la, 500);
  beep(m, 250);
  beep(f, 250);
  beep(l, 500);
  beep(l, 500);
  beep(s, 500);
  beep(r, 750);
  beep(f, 250);
  beep(f, 750);
  beep(r, 250);
  beep(l, 250);
  beep(s, 250);
  beep(m, 250);
  beep(f, 250);
  beep(r, 2000);
}
void StarWars() {
   
    beep( a, 500); 
    beep( a, 500);     
    beep( a, 500); 
    beep( f, 350); 
    beep( cH, 150);
    
    beep( a, 500);
    beep( f, 350);
    beep( cH, 150);
    beep( a, 1000);
    
    
    beep( eH, 500);
    beep( eH, 500);
    beep( eH, 500);    
    beep( fH, 350); 
    beep( cH, 150);
    
    beep(gS, 500);
    beep( f, 350);
    beep( cH, 150);
    beep( a, 1000);
    
    
    beep( aH, 500);
    beep( a, 350); 
    beep( a, 150);
    beep( aH, 500);
    beep( gSH, 250); 
    beep( gH, 250);
    
    beep( fSH, 125);
    beep( fH, 125);    
    beep( fSH, 250);
    delay(250);
    beep( aS, 250);    
    beep( dSH, 500);  
    beep( dH, 250);  
    beep( cSH, 250);  
    
    
    beep(cH, 125);  
    beep( b, 125);  
    beep( cH, 250);      
    delay(250);
    beep( f, 125);  
    beep( gS, 500);  
    beep( f, 375);  
    beep( a, 125); 
    
    beep( cH, 500); 
    beep( a, 375);  
    beep( cH, 125); 
    beep(eH, 1000); 
    
    beep( aH, 500);
    beep( a, 350); 
    beep( a, 150);
    beep( aH, 500);
    beep( gSH, 250); 
    beep(gH, 250);
    
    beep(fSH, 125);
    beep( fH, 125);    
    beep( fSH, 250);
    delay(250);
    beep(aS, 250);    
    beep( dSH, 500);  
    beep( dH, 250);  
    beep( cSH, 250);  
    
    
    beep( cH, 125);  
    beep( b, 125);  
    beep( cH, 250);      
    delay(250);
    beep( f, 250);  
    beep( gS, 500);  
    beep( f, 375);  
    beep( cH, 125); 
           
    beep(a, 500);            
    beep(f, 375);            
    beep( c, 125);            
    beep( a, 1000);      
}

Source : Singing Arduino


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top