flora_uv_neopixel.ino

adafruit-uv-hat / flora_uv_neopixel.ino

/***************************************************
 * Sunscreen reminder hat using the Si1145 UV/IR/Visible Light Sensor
 * visit https://learn.adafruit.com/sunscreen-reminder-hat for full tutorial
 * contained Super Mario Bros theme composed for piezo by Tiago Gala: http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/
 * Originally written by Becky Stern for Adafruit Industries.
 * BSD license, all text above must be included in any redistribution
 
 ****************************************************/
 #include <Wire.h>
 #include "Adafruit_SI1145.h"
 #include "Adafruit_NeoPixel.h"

//tones for reminder tune
#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
int speaker = 1; // piezo wired to FLORA TX
int w = 1000;

Adafruit_SI1145 uv = Adafruit_SI1145();

uint32_t sinceTime;
uint32_t markerTime;
uint8_t loopNumber = 0;
boolean shouldChime = false;
float UVindex;
uint8_t UVrounded;
float UVthreshold = 2.0;

uint32_t reapplyInterval = 7200000; // 2 hours
uint32_t notificationInterval = 900000; // 15 minutes = 900000 milliseconds. One hour = 3600000 milliseconds. Two hours = 7200000 milliseconds

Adafruit_NeoPixel led = Adafruit_NeoPixel(1, 6, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit SI1145 test");
  if (! uv.begin()) {
    Serial.println("Didn't find Si1145");
    while (1);
  }
  Serial.println("OK!");
  pinMode(speaker, OUTPUT);
  led.begin();
  led.show(); // Initialize all pixels to 'off'
}

void loop() {
  Serial.println("===================");
  get_UV_number();
  Serial.print("UV: "); Serial.println(UVindex);
  loopNumber++;

  if (UVindex > UVthreshold) { //only chime when we are currently outside
    Serial.println("Play pips");
    pips(UVrounded);
    Serial.println("Show colour");
    ledColour(UVindex, UVrounded);
    Serial.print("Iteration number ");Serial.println(loopNumber);
    if (loopNumber >= 8) { //check to see if we've exceeded the time limit
      Serial.println("Playing music");
      chime();
      loopNumber = 0;
    }
  } else {
    colorWipe(led.Color(8, 40, 70), 1000, 1);
    loopNumber = 0;
    Serial.println("still alive!");
  }
  Serial.println();
  delay(notificationInterval);
}

uint32_t get_UV_number() {
  UVindex = uv.readUV();
  UVindex /= 100.0; // the index is multiplied by 100 so to get the integer index, divide by 100!
  UVrounded = ceil(UVindex);
  return UVrounded;
}

void pips(uint8_t repeat){
  for (int pip = 0; pip < repeat; pip++){
    Serial.print("This is a pip! ");
    tone(speaker,NOTE_CS5,300);
    delay(500);
  }
  Serial.println();
}

void chime(){
  // notes in the melody:
  // Beatles - Here Comes the Sun - I think this is more appropriate than the Mario tune :-D
  int melody[] = {
    NOTE_CS5, NOTE_A4, NOTE_B4, NOTE_CS5,0, NOTE_A4 ,0, NOTE_CS5, NOTE_B4, NOTE_A4,NOTE_FS4, NOTE_A4, NOTE_B4,NOTE_A4, NOTE_FS4,NOTE_GS4,NOTE_FS4,NOTE_GS4,NOTE_A4,NOTE_B4,0,
    NOTE_CS5, NOTE_A4, NOTE_B4, NOTE_CS5,0, NOTE_A4 ,0, NOTE_CS5, NOTE_B4, NOTE_A4,NOTE_FS4,0,NOTE_CS5,NOTE_B4,NOTE_A4,NOTE_GS4,                          };

  // note durations: 4 = quarter note, 8 = eighth note, etc.:
  int noteDurations[] = {
    4,4,4,4,4,2,4,4,2,2,2,2,2,2,4,4,4,4,2,2,2,4,4,4,4,4,2,4,4,2,2,2,4,4,2,2,1,                           };
    long vel = 20000;
    Serial.println();
    Serial.println("This is the chime");
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 37; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(speaker, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.0;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(speaker);
  }
}

void ledColour(uint8_t UVindex, uint8_t repeat){
  if (UVindex < 3)
  {
    Serial.println("GREEN");
    colorWipe(led.Color(40, 149, 0), w, repeat);
  }
  else if (UVindex < 6)
  {
    Serial.println("YELLOW");
    colorWipe(led.Color(247, 228, 0), w, repeat);
  }
  else if (UVindex < 8)
  {
    Serial.println("ORANGE");
    colorWipe(led.Color(248, 89, 0), w, repeat);
  }
  else if (UVindex < 11)
  {
    Serial.println("RED");
    colorWipe(led.Color(255, 0, 0), w, repeat);
  }
  else if (UVindex >= 11)
  {
    Serial.println("PURPLE");
    colorWipe(led.Color(150, 0, 200), w, repeat);
  }
  else
  {
    Serial.println("ERROR ERROR ERROR");
  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait, uint8_t repeat) {
  for(uint16_t i=0; i<repeat; i++) {
    led.setPixelColor(0, c);
    led.show();
    delay(w);
    led.setPixelColor(0, 0,0,0);
    led.show();
    delay(w);
  }
}