Back to Parent

//FINAL CODE

//pulls neopixel and math libraries
#include "neopixel.h"
#include <math.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

//configures neopixel
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int pixel_position = 0;
int blinkNum = 5; //begins number of blinks at 5 for all emotions first tweet
int blinkSpeed = 5; //sets speed of blinks
int countS = 0;   //counter for how many sad tweets have been sent in a row
int countM = 0;   //counter for how many mad tweets have been sent in a row
int countG = 0;   //counter for how many sad tweets have been sent in a row
int speaker = D1;

int melodyS[] = {1568,0,1976, 0, 2794,0,2794,0,2794,2637, 2349, 2093};
int melodyM[] = {2637,2637,2637,2093,0,2349,2349,2349,1976};
int melodyG[] = {1568,2093,2349,2637,2637,0, 2637,2349,2637, 2093, 2093,0,2093,2349,2637, 2794, 3520,0, 3520, 3136,2794,2637};

int noteDurationsS[] = {4,8,8,8,8,16,8,16,8,8,8,4 };
int noteDurationsM[] = {8,8,8,2,8,8,8,8,2};
int noteDurationsG[] = {8,8,8,4,4,8,8,8,8,4,4,8,8,8,8,4,4,8,8,8,8,4 };

void setup() {
  Serial.begin(9600);
  strip.begin();
  strip.show(); //Initialize all pixels to 'off'
  Spark.function("sad", sadtweet);      //if #sad is tweeted, run this
  Spark.function("mad", madtweet);      //if #mad is tweeted, run this
  Spark.function("glad", gladtweet);    //if #glad is tweeted, run this
  pinMode(speaker, OUTPUT);
}

int sadtweet(String cmd) {
  countS++; //increase sad count
  countG = 0; //returns other functions to 0
  countM = 0; //returns other functions to 0
  int blinkNumS = blinkNum*countS; //sets number of blinks for sad to correlate to the number of sad tweets were send in a row

  for (int thisNote = 0; thisNote < 12; thisNote++) {
    int noteDuration = 1000/noteDurationsS[thisNote];
    tone(speaker, melodyS[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(speaker);
  }

  //runs the program twice as many times as we want the light to blink (one for on, one for off)
  for(int j = 0; j < blinkNumS*2; j++){
    //lights on for even numbers
    if(j % 2 == 0) {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 0, 0, 255);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
    //lights off for odd numbers
    else {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 0, 0, 0);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
  }
}

int madtweet(String cmd) {
  countM++; //increase mad count
  countG = 0; //returns other functions to 0
  countS = 0; //returns other functions to 0
  int blinkNumM = blinkNum*countM;

  for (int thisNote = 0; thisNote < 9; thisNote++) {
    int noteDuration = 1000/noteDurationsM[thisNote];
    tone(speaker, melodyM[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(speaker);
  }


  for(int j = 0; j < blinkNumM*2; j++){
    if(j % 2 == 0) {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 255, 0, 0);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
    else {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 0, 0, 0);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
  }
}


int gladtweet(String cmd){
  countG++; //increase glad count
  countG = 0; //returns other functions to 0
  countS = 0; //returns other functions to 0
  int blinkNumG = blinkNum*countG;

  for (int thisNote = 0; thisNote < 22; thisNote++) {
    int noteDuration = 1000/noteDurationsG[thisNote];
    tone(speaker, melodyG[thisNote],noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    noTone(speaker);
  }


  for(int j = 0; j < blinkNum*2; j++){
    if(j % 2 == 0) {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 255, 215, 0);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
    else {
      for(int i = 0; i < strip.numPixels(); i++){
        strip.setPixelColor(i, 0, 0, 0);
        strip.show();
      }
      delay(1000/blinkSpeed);
    }
  }
}
Click to Expand

Content Rating

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

0