Back to Parent

#include "neopixel.h"
#include <math.h>
#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int valp1 = 0;
int valp2 = 0;
int ledPin = D7;
int othercm;
int bubblesPin = D3;
const int trigPin = D5;
const int echoPin = D6;


void setup() {
  Serial.begin(9600);
  //listening for other device's events
  Particle.subscribe("amountlove-Raha" , loveHandler);//change event name for other device
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(bubblesPin, OUTPUT);
}


void loop() {
  long duration1;
   int inches, cm;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.

  duration1 = pulseIn(echoPin, HIGH);
  //Serial.print(duration1);
  //Serial.print("ms,  ");
  // convert the time into a distance
  inches = microsecondsToInches(duration1);
  cm = microsecondsToCentimeters(duration1);
  /*Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.println();*/


  String myVal = String(cm);
  Serial.print(myVal);
  Serial.print("cm  ");
  //Serial.print(myVal);
  // read the value from the button pin
  Particle.publish( "amountlove-Jamie", myVal );//change event name for other device

  delay(1000);

//letstalk turns on lights
//vibin turns on pump
  int difference = (cm-othercm);
  int absDifference = abs(difference);
  //Serial.print(absDifference);
  //Serial.print("diff  ");
  if( absDifference <= 2) {
    vibin();
     //Serial.print("vibin");
       if (cm < 4) {
         if (othercm < 4){
           letstalk();
           //Serial.print("talking");
         }
         else {
         }
      }else {
        uint32_t i;
        for(i=0; i< strip.numPixels(); i++){
        strip.setPixelColor(i, 0, 0, 0 );}//lights off
        strip.show();

       }
  }else {
      systemoff();
      //Serial.print("systemoff ");
     }
   }


void loveHandler(const char *event,const char *data)
{ //this is the handler for receiving the subscribed event
  String otherValString = data;
  othercm = otherValString.toInt();
  Serial.print("Raha");
  Serial.print(othercm);
  Serial.print("  ");

}


long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

void letstalk() {
  //float val = (exp(sin(millis()/2000.0*M_PI)) - 0.36787944)*108.0;
  uint16_t i;
    //uint32_t c = strip.Color(0,  255,  255);
    //c is deciding color
    //i is position of led
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, 0,  255,  255);
      //sets every pixel to the same color
      }
      strip.show();
    }



void vibin() {
  digitalWrite(bubblesPin, HIGH);

}

void systemoff(){
  uint32_t i;
  for(i=0; i< strip.numPixels(); i++){
  strip.setPixelColor(i, 0, 0, 0 );}//lights off
  strip.show();
  digitalWrite(bubblesPin, LOW);

}
Click to Expand

Content Rating

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

0