Back to Parent

// *
//  * Project breakbeam
//  * Description:
//  * Author:
//  * Date:
//  */
 /*
   IR Breakbeam sensor demo form adafruit
 */
#define SENSORPIN 3
int sensorState = 0, lastState=0;// variable for reading the pushbutton status
int breakCount = 0;//variable for counting the number of times the LED is Broken
int publishAfter = 10000; // This is the time delay before we should publish a new event.
long lastPublishedAt = 0 - publishAfter; // This value will store the last time we published an event.
bool reSet = false;
long lastBroken = 0;
int breakAfter = 1000;
 // Our button wired to D0
int buttonPin = D0;
// Setup Speaker Variables
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
int volume;
void setup() {
   Serial.begin(9600);
   Serial1.begin(9600);
  //SPEAKER Code
  execute_CMD(0x3F, 0, 0); // Send request for initialization parameters
  while (Serial1.available()<10){ // Wait until initialization parameters are received
    delay(30); // Pretty long delays between successive commands needed
    // Initialize sound to very low volume. Adapt according used speaker and wanted volume
    execute_CMD(0x06, 0, 30); // Set the volume (0x00~0x30)
  }
   //initialize the sensor pin as an input:
   pinMode(SENSORPIN, INPUT);
   digitalWrite(SENSORPIN, HIGH); // turn on the pullup
   pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
   Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-RESET", subscribeReset);
   Particle.function("Jar", publishJar);
 }
void loop()
{
  /*
   int buttonState = digitalRead( buttonPin );
    sensorState = digitalRead(SENSORPIN);
    if (sensorState && !lastState) {
      Serial.println("Unbroken");
    }
    if (!sensorState && lastState){
      breakCount ++;
      Serial.println("broken");
      if (abs(millis() - lastPublishedAt) > publishAfter){
        Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MIND", String(breakCount));
        lastPublishedAt = millis();
        execute_CMD(0x0D,0,1);
      }
    }
    lastState = sensorState;
  delay(100);
  */
 }

 void subscribeReset(const char *event, const char *data)
{
  breakCount = 0;
}
 // Wrote by Ype Brada
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
 // Calculate the checksum (2 bytes)
 int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
 // Build the command line
 byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, checksum >> 8, checksum & 0xFF, End_Byte};
 //Send the command line to the module
 for (byte k=0; k<10; k++)
 {
 Serial1.write( Command_line[k]);
 }
}

int publishJar(String command)
{
  if (abs(millis() - lastPublishedAt) > publishAfter)
  {
    Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MIND");
    lastPublishedAt = millis();
  }
  return 1;
}
Click to Expand

Content Rating

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

0