/*
Project created by Neoxelox for Hackster.io Arduino Contest 31/12/2016
Cayenne MKR1000 Example
This sketch connects to the Cayenne server using an Arduino/Genuino MKR1000 and runs the main communication loop.
The Cayenne Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. Install the Arduino SAMD Boards from the Arduino Boards Manager if you have not done so already.
2. Install the WiFi101 library (https://github.com/arduino-libraries/WiFi101) from the Arduino Library Manager if you have not done so already.
3. Select the Arduino/Genuino MKR1000 board and the correct port in the Arduino IDE.
4. Set the token variable to match the Arduino token from the Dashboard.
5. Set the network name and password.
6. Compile and upload this sketch.
For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Pins, data
should be sent to those pins using virtualWrites. Examples for sending and receiving
Virtual Pin data are under the Basics folder.
*/
//#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMKR1000.h>
#define LED_VIRTUAL_PIN V0
#define SONG_VIRTUAL_PIN V1
#define SONG2_VIRTUAL_PIN V2
#define SONG3_VIRTUAL_PIN V3
#define SONG4_VIRTUAL_PIN V4
#define SONG5_VIRTUAL_PIN V5
#define SONG6_VIRTUAL_PIN V6
#define SONG7_VIRTUAL_PIN V7
#define SONG8_VIRTUAL_PIN V8
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "*********";
// Your network name and password.
char ssid[] = "*********";
char password[] = "**********";
int speakerPin = 6;
int leds = 5;
int yesno = 0;
//Buzzer-----------------------------------
int tempo = 150;
//Buzzer-----------------------------------
void setup()
{
Serial.begin(9600);
Cayenne.begin(token, ssid, password);
pinMode(speakerPin, OUTPUT);
pinMode(leds, OUTPUT);
}
void loop()
{
Cayenne.run();
}
//BUZZER-----------------------------------------------------------------------------
void playTone(int tone, int duration) {
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', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' };
int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
//BUZZER------------------------------------------------------------------------------
//CAYENNE VIRTUAL DASHBOARD-----------------------------------------------------------
CAYENNE_IN(LED_VIRTUAL_PIN)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1023
analogWrite(leds, currentValue / 4); // must be from 0 to 255
}
//Ding Dong Merrily
CAYENNE_IN(SONG_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 73;
char notes[] = "ggagsed deggsgg ggagsed deggsgg DCbCDbCbabCabagabgagsgasgsesgeseddeggsgg "; // a space represents a rest
int beats[] = { 2,2,1,1,1,1,4,2,2,2,2,2,2,4,2,2,2,2,1,1,1,1,4,2,2,2,2,2,2,4,2,2,3,1,1,1,1,1,3,1,1,1,1,1,3,1,1,1,1,1,3,1,1,1,1,1,3,1,1,1,1,1,3,1,2,2,2,2,2,2,4,2,2 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//God Rest Ye Merry Gentlemen
CAYENNE_IN(SONG2_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 69;
char notes[] = "ddaagfedcdefga ddaagfedcdefga avgavCDagfdefgfgavaagfedfedgfgavCDagfed";
int beats[] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,4,2,2,2,2,2,2,4,1,1,2,4,2,2,2,2,2,2,2,2,2,2,8 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//O Little Town of Bethlehem
CAYENNE_IN(SONG3_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 71;
char notes[] = "cfffgagavCavafggfcfffgagavCavafggffaCDCvagfgavCcfagfccfffgagavCavafggf ";
int beats[] = { 2,2,2,2,2,1,1,1,1,2,2,2,1,1,2,2,6,2,2,2,2,2,1,1,1,1,2,2,2,1,1,2,2,6,1,1,3,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,4,4,2,2,2,2,1,1,1,1,2,2,2,1,1,2,2,6,2 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//While Shephards Watched
CAYENNE_IN(SONG4_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 29;
char notes[] = "faagfvvagaCCbCaDCvagfeagffef ";
int beats[] = { 2,3,1,2,2,2,2,2,2,2,2,2,2,6,2,3,1,2,2,2,2,2,2,2,2,2,2,6,2 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//In The Bleak Midwinter
CAYENNE_IN(SONG5_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 51;
char notes[] = "aavCagfgagdgavCaggfgagff vavCDDaaCagfecavCagfgagff ";
int beats[] = { 2,3,1,2,2,4,4,3,1,2,2,8,3,1,2,2,3,1,4,2,2,3,1,6,2,3,1,2,2,2,2,2,2,2,2,2,2,6,2,2,2,2,2,4,4,2,2,3,1,8,8};
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//Hark the Herald
CAYENNE_IN(SONG6_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 77;
char notes[] = "cffefaagCCCvagacffefaagCffeedcCCCfvaagCCCfvaagDDDCvavgavCffgaDDDCvavgavCffgf ";
int beats[] = {2,2,3,1,2,2,2,2,2,2,3,1,2,2,4,2,2,3,1,2,2,2,2,2,2,3,1,2,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,1,1,3,1,2,2,4,3,1,2,2,2,2,4,2,1,1,3,1,2,2,4,8};
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//O come all Ye Faithful
CAYENNE_IN(SONG7_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 64;
char notes[] = "ggdgadbabCbaggsesgabsedd DCbCbabgasedggsgagdbbabCbabCbagsgCbagg ";
int beats[] = { 2,4,2,2,4,4,2,2,2,2,4,2,2,4,2,2,2,2,2,2,4,3,1,6,2,4,2,2,4,4,2,2,2,2,3,1,2,2,2,2,2,2,4,2,2,2,2,2,2,4,2,2,2,2,2,2,4,2,2,4,3,1,6,8 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//O Come O Come Emmanuel
CAYENNE_IN(SONG8_VIRTUAL_PIN)
{
yesno = getValue.asInt();
if (yesno == 1){
int length = 63;
char notes[] = "egbbbaCbagabgegasedeaaeesgsedgabbbaCbag DD bb baCdagabgegasede ";
int beats[] = { 2,2,2,2,2,2,2,2,2,6,2,2,2,2,2,2,2,2,2,6,2,2,2,2,2,4,2,2,6,2,2,2,2,2,2,2,2,2,4,2,2,4,2,2,4,2,2,2,2,2,2,6,2,2,2,2,2,2,2,2,2,8,8 };
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);
yesno = getValue.asInt();
if (yesno == 0){
break;
}
}
}
}
//CAYENNE VIRTUAL DASHBOARD-----------------------------------------------------------
Comments