int ledPin = D0;
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int intactValue; // This is the average value that the photoresistor reads when the beam is intact.
int brokenValue; // This is the average value that the photoresistor reads when the beam is broken.
int beamThreshold; // This is a value halfway between ledOnValue and ledOffValue, above which we will assume the led is on and below which we will assume it is off.
bool beamBroken = false; // This flag will be used to mark if we have a new status or now. We will use it in the loop.
void setup() {
pinMode( ledPin , OUTPUT ) ;
blinkLEDs();
takeInitialReadings();
}
void loop() {
if (analogRead(photoresistor)>beamThreshold) {
/* If you are above the threshold, we'll assume the beam is intact.
If the beam was intact before, though, we don't need to change anything.
We'll use the beamBroken flag to help us find this out.
This flag monitors the current status of the beam.
After the beam is broken, it is set TRUE
and when the beam reconnects it is set to FALSE.
*/
if (beamBroken==true) {
// If the beam was broken before, then this is a new status.
// We will send a publish to the cloud and turn the LED on.
// Send a publish to your devices...
Particle.publish("beamStatus","intact",60,PRIVATE);
// And flash the on-board LED on and off.
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
// Finally, set the flag to reflect the current status of the beam.
beamBroken=false;
}
else {
// Otherwise, this isn't a new status, and we don't have to do anything.
}
}
else {
// If you are below the threshold, the beam is probably broken.
if (beamBroken==false) {
// Send a publish...
Particle.publish("beamStatus","broken",60,PRIVATE);
// And flash the on-board LED on and off.
digitalWrite(ledPin,HIGH);
delay(500);
digitalWrite(ledPin,LOW);
// Finally, set the flag to reflect the current status of the beam.
beamBroken=true;
}
else {
// Otherwise, this isn't a new status, and we don't have to do anything.
}
}
}
void takeInitialReadings()
{
// Now we'll take some readings...
int on_1 = analogRead(photoresistor); // read photoresistor
delay(200); // wait 200 milliseconds
int on_2 = analogRead(photoresistor); // read photoresistor
delay(300); // wait 300 milliseconds
// ...And we will take two more readings.
int off_1 = analogRead(photoresistor); // read photoresistor
delay(200); // wait 200 milliseconds
int off_2 = analogRead(photoresistor); // read photoresistor
delay(1000); // wait 1 second
// Now we average the "on" and "off" values to get an idea of what the resistance will be when the LED is on and off
intactValue = (on_1+on_2)/2;
brokenValue = (off_1+off_2)/2;
// Let's also calculate the value between ledOn and ledOff, above which we will assume the led is on and below which we assume the led is off.
beamThreshold = (intactValue+brokenValue)/2;
}
void blinkLEDs()
{
digitalWrite( ledPin, HIGH );
delay( 500 );
digitalWrite( ledPin, LOW );
delay( 500 );
}
Click to Expand