Back to Parent

/*************button***************/
//button1 is for bell
int speakerPin = D0;
int buttonPin = A3;
int button;

//button2 is for LED
int buttonPin2 = A5;
int button2;

//button3 is for change the melody
int buttonPin3 = A6;
int button3;

/***************melody****************/
int noteDurations[] = { 8,8,4,4,4,2,8,8,4,4,4,2,8,8,4,4,4,4,3,8,8,4,4,4,2 };

int melody[] =
{
  196, 196, 220, 196, 262, 247, 196,
  196, 220,196, 294, 262,
  196, 196, 392,330,262, 247, 220,
  349, 349, 330, 262, 294, 262
};

int melodyDataBase[] = {1661, 1568,196,220,262,247,294,330,392,349};
boolean isPlaying = false;

/***************melody2****************/
int melody2[] = {196 , 131, 156, 175, 196 ,131, 156, 175, 147, 175 , 123, 147, 156, 175, 123, 156, 147,131};
int noteDurations2[] = {6, 9, 3, 3, 6, 6, 3, 3, 27, 9, 9, 3, 3, 6, 6, 3, 3, 27};

/***************LED****************/
int RedPin = A0;
int BluePin = A1;
int GreenPin = D1;

int r = 255;
int g = 255;
int b = 255;

/********save data to google**********/
char saveData[8];
int savePreData[8];
int n = 0;
int data1;
int data2;

/********force sensor**********/
//force sensor is for replacing one of the button
int forcePin = A7;
int forceCellReading = 0;


void setup()
{
  pinMode( buttonPin , INPUT_PULLDOWN);
  pinMode( buttonPin2 , INPUT_PULLDOWN);
  pinMode( buttonPin3 , INPUT_PULLDOWN);

  /***************data on cloud***************/
  //now the event blabla would be for changing melody on another device
  Spark.subscribe( "Mylalallalallalallasound", handleDoorbellPush);
  Spark.subscribe( "MysocutelovelyLED", handleLEDPush );//,MY_DEVICES);

  /***************MY RGB***************/
  pinMode( RedPin , OUTPUT);
  pinMode( BluePin , OUTPUT);
  pinMode( GreenPin , OUTPUT);

  /**********turn the sound and light off******************/
  digitalWriteColor( 255, 255, 255 );
  noTone(speakerPin);

  /***************variable for detect and debug***************/
  Spark.variable("red", &r, INT);
  Spark.variable("green", &g, INT);
  Spark.variable("blue", &b, INT);
  Spark.variable("saveMelody", &saveData, STRING);
  Spark.variable("force", &forceCellReading, INT);
}

void loop()
{
  if (Spark.connected() == false)
  {
    Spark.connect();
  }

  button = digitalRead( buttonPin );
  button2 = digitalRead(buttonPin2);
  button3 = digitalRead(buttonPin3);

  forceCellReading = analogRead(forcePin);

  /**********button 1 for sound, button 2 for RGB******************/
  if( button == HIGH )
  {
      doDingDong();
    //  delay( 500 );
  }

  if( button2 == HIGH )
  {
      announceLED();
  }

  if( button3 == HIGH )
  {
      playThisSong();
  }

  if(forceCellReading > 1000)
  {
    announceDoorbell();
    //changeMelody();
  }

  /********write data on saveData array, send to google spread sheet*******/
  for(int i = 0; i < 3; i++)
  {
    data1 = savePreData[i];
    data2 = savePreData[7-i];
    sprintf(saveData, "{\"data1\":%d,\"data2\":%d}", data1, data2);
  }
}

/**************************Sound **************************/
void announceDoorbell()
{
  Spark.publish( "Mylalallalallalallasound","HIGH");
}

void handleDoorbellPush(const char *event, const char *data)
{
    changeMelody();
}

void doDingDong()
{
  for (int thisNote = 0; thisNote < arraySize(melody); thisNote++)
  {

    int noteDuration = 1000/noteDurations[thisNote];
    tone(speakerPin, melody[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(speakerPin);
  }
  isPlaying = true;
}

void playThisSong()
{
  int pace = 1450;
  for (int Note = 0; Note <54; Note++)
  {
    int duration = pace/noteDurations2[Note];
    tone(speakerPin, melody2[Note],duration);

    delay(duration*1.2);
    noTone(speakerPin);
  }
}

/**************************LED **************************/
void announceLED()
{
  Spark.publish( "MysocutelovelyLED","HIGH");
}

void handleLEDPush(const char *event, const char *data)
{
//  if(data == "HIGH")
  ledAllColorRandom();
}

void ledAllColorRandom()
{
  // set all the colors to a random
  digitalWriteColor( random( 0, 128), random( 0, 128), random( 0, 128) );

  delay(800);

  // Set all of the colors to off

  digitalWriteColor( 255, 255, 255 );
  delay( 100 );
}

void digitalWriteColor( int r1, int g1, int b1 )
{
  r = r1;
  g = g1;
  b = b1;

  analogWrite(RedPin,r );
  analogWrite(GreenPin,g );
  analogWrite(BluePin,b );
}

/**************************melody**************************/
void changeMelody()
{
  for(int i = 0; i < arraySize(melody); i++ )
  {
    int j = random(1,100)%10;
    melody[i] = melodyDataBase[j];

    /************save data to predata array**************/
    savePreData[i] = melody[i];
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0