Back to Parent

#include "Particle.h"
#include "math.h"

int infraSensorPin = A0;
int ledPin = D2; 
int ledPin2 = D3;
int distance = 0; 

void setup() {
    Particle.variable("distance", distance); // Send variable to cloud
    pinMode(ledPin, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    
}

void loop() {
   int volts_1 = analogRead(A0);
   //int calculated = (6762/(reading-9))-4;

   int volts = (3.3*(volts_1-900))/2000;  // value from sensor * (5/1024)
  
   int distance = 320/(1+10*volts);
   //int distance = 13*pow(volts, -1); // worked out from datasheet graph
   delay(500);
   
  if(distance<15){  
      digitalWrite(ledPin, HIGH); // turn on red LED
      digitalWrite(ledPin2, LOW); // turn off white LED
  } else {
      digitalWrite(ledPin, LOW); // turn off red LED
      digitalWrite(ledPin2, HIGH); // turn on white LED 
  }
   
   
  Particle.publish("volts", String(volts));
  Particle.publish("distance", String(distance));
 
}
Click to Expand

Content Rating

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

0