Back to Parent

#include <Wire.h>

//code for lights to respond to heart rate
//#define wwLED_PIN 6
//#define pLED_PIN 9
#define bgLED_PIN 11

byte heartRateByte=0;
int x = 0;

void setup() {
  Wire.begin(5);    // Start I2C Bus as a Slave (Device 5)
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600); //start serial for output
  
//  pinMode(wwLED_PIN, OUTPUT);
//  pinMode(pLED_PIN, OUTPUT);
  pinMode(bgLED_PIN, OUTPUT);
  
//  digitalWrite(wwLED_PIN, LOW);
//  digitalWrite(pLED_PIN, LOW);
  digitalWrite(bgLED_PIN, HIGH);
}

/*void dim(){
  //set BG LED to half brightness
  for (int brightness=0;brightness<130;brightness++){
    analogWrite(bgLED_PIN,brightness);
    delay(10);
  }
  pinMode(pLED_PIN, LOW);
  pinMode(wwLED_PIN, LOW);
}*/

/*void pulse(){
  pinMode(bgLED_PIN, LOW);
  //set ww pulse, p pulse
  for (int brightness=0;brightness<256;brightness++){
    analogWrite(pLED_PIN,brightness);
    analogWrite(wwLED_PIN,brightness);
    delay(10);
  }
  delay(250);
  for (int brightness=255;brightness>=0;brightness--){
    analogWrite(pLED_PIN,brightness);
    analogWrite(wwLED_PIN,brightness);
    delay(10);
  }
}*/

/*void Blink(){
  //blink bg
  digitalWrite(bgLED_PIN, HIGH);
  //blink p
  digitalWrite(pLED_PIN, HIGH);
  //blink ww
  digitalWrite(wwLED_PIN, HIGH);
  delay(500);
    //blink bg
  digitalWrite(bgLED_PIN, LOW);
  //blink p
  digitalWrite(pLED_PIN, LOW);
  //blink ww
  digitalWrite(wwLED_PIN, LOW);
  delay(500);
}
*/
/*void off(){
  pinMode(bgLED_PIN, LOW);
  pinMode(pLED_PIN, LOW);
  pinMode(wwLED_PIN, LOW);
}*/

void receiveEvent(int howMany)
{
 while(Wire.available())
  {
    byte r = Wire.read();
    x=r;
    if(r > 0)
    {
    int heartRate = (int) r;
    Serial.println(heartRate);
    }
    else if(r == 0)
    {
      Serial.print("error");
    }
    else
    {
      Serial.print("error2");
    }
  }
}

void loop()
{
  delay(100);
  if (x>0 && x<=80)
  {
    digitalWrite(bgLED_PIN, HIGH);
  }
  else if (x>80 && x<=100)
  {
    digitalWrite(bgLED_PIN, LOW);
  //  pinMode(pLED_PIN, HIGH);
  }
/*  else if (x>100 && x<=184)
  {
    Blink();
  }
  else if (x>184)
  {
    off();
  }
  else if (x == 0)
  {
    off();
  }*/
}
Click to Expand

Content Rating

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

0