Summary

Our two robots are akin to soulmates in love: they finish each other’s song-tences (get it, instead of sentences)! Each bot is equipped with a speaker, a SPDT switch, and a servo with an attached arm. When the first bot’s arm moves down, it presses the switch of the second robot, which begins to play one verse of the popular English lullaby, “Twinkle Twinkle Little Star”. After it sings its verse, the second robot then places its arm down, pressing on the switch of the first robot. The first robot then plays the second verse of the song before moving its arm down again to allow its partner to start playing the beginning of the song again.

Video

Solidworks Files

Demo 3 – Solidworks Files

Arduino Code


/*
Jen Kwang and Sora Shin "Demo 3: The Conversation" for 16-223
Lifts servo arm to upright position, then plays a tune, then moves arm 90 deg
down to push the switch on its partner.  Note that switch is wired to power
the whole circuit when pressed and ground otherwise.

Code for twinkle twinkle little star taken from baojie at GitHub:
https://gist.github.com/baojie/4522173
*/


/* Melody
 * (cleft) 2005 D. Cuartielles for K3
 *
 * This example uses a piezo speaker to play melodies.  It sends
 * a square wave of the appropriate frequency to the piezo, generating
 * the corresponding tone.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = period / 2 = 1 / (2 * toneFrequency)
 *
 * where the different tones are described as in the table:
 *
 * note   frequency   period  timeHigh
 * c          261 Hz          3830  1915  
 * d          294 Hz          3400  1700  
 * e          329 Hz          3038  1519  
 * f          349 Hz          2864  1432  
 * g          392 Hz          2550  1275  
 * a          440 Hz          2272  1136  
 * b          493 Hz          2028  1014  
 * C          523 Hz          1912  956
 *
 * http://www.arduino.cc/en/Tutorial/Melody
 */

#include <Servo.h> 
int speakerPin = 5;
const int servoPin = 10;
Servo svo;

int length = 15; // the number of notes

//twinkle twinkle little star
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc "; // a space represents a rest

//////////////
// !! Depending on which of the partners you have, change the first or second half of the array to all zeroes.
// Sora =  { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2 };
// Jen =  { 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 };
//////////////

int beats[] = { 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2 };
int tempo = 300;

void playTone(int tone, int duratio n) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  svo.attach(servoPin);
  pinMode(speakerPin, OUTPUT);

  
}

void loop() {

  svo.write(30);
  delay(500);
   
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2); 
  }

   svo.write(120);
   delay(5000);

  
}