Back to Parent

/*The code is meant to turn on the fan in the #MuslimBan ambient device.  The
 ambient device turns on a fan that turns taunt a caged bird.  The bird flaps
 its wings and there is a sound caused by a dc motor that promotes anxiousness.
 The different devices are triggered by an event.
 */

int controlpin = D2;    //Controls the fan
int monitorlight = D7;  //Light for testing
const int pwr_off = 0;  //Sets analog signal to 0
int on_time = 3000;     //This was originally use to learn how to control and wire the fan
int off_time = 1000;    //This was originally use to learn how to control and wire the fan
int fan_on = 0;         //This variable triggers the code action to take place when the event occurs
int pwr_cmd = 100;      //how fast the fan spins
int fan_time = 9000;    //how long the fan spins in ms

void setup()
{
  pinMode(controlpin, OUTPUT);    //Sets up the the fan control pin
  pinMode(monitorlight, OUTPUT);  //The light on the photon is set up for testing
  Particle.subscribe("melikey",event_driven);   //This is the name of the event that triggers all the devices


}

void loop()
{

  if (fan_on == 1)    //fan_on variable is changed when the event is triggered
  {

//This segment of code is used to make test and make the original circuit, it was replaced by the fan_control fuction
  /*digitalWrite(monitorlight, HIGH);
  digitalWrite(controlpin, HIGH);
  delay(on_time);
  digitalWrite(monitorlight, LOW);
  digitalWrite(controlpin, LOW);
  delay(off_time);*/

  fan_control(pwr_cmd,fan_time);  //fan function take power level and time on
  fan_on = 0;       //change the intializing variable so that the fan only turns on once.
  }
  else{}

}

void fan_control(int pwr_lvl, int duration)
{

  int pwr_cmd = map(pwr_lvl, 0, 100, 0, 255); //maps the 0-100% to the analog values required
  analogWrite(controlpin, pwr_cmd);           // Writes the power of the fan
  delay(duration);                            // How long will the fan will be turned on
  analogWrite(controlpin, pwr_off);           // Turns off the fan


}
void event_driven(const char *event, const char *data)
{

  fan_on = 1;

}
Click to Expand

Content Rating

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

0