// the pin we're reading from
int distance_sensor = A0;
// store the distance value / reading from the sensor
int distance_measure = 0;
const int sampleWindow = 10;
int numPins = 6;
int ledPins[ 6 ] = { D0, D1, D2, D3, D4, D5};
void setup(){
// start serial connection
Serial.begin(9600);
for( int i = 0 ; i < numPins; i++ ){
pinMode(ledPins[ i ], OUTPUT);
}
// Share the value as a distance level
// reading through the cloud
Particle.variable( "distance", &distance_measure, INT );
}
void loop()
{
distance_measure = analogRead( distance_sensor );
//distance_measure = sampleDistance( );
Serial.println( distance_measure );
displayDistanceOnLEDs();
delay( 100 );
}
void displayDistanceOnLEDs(){
int level = 10;
for( int i = 0 ; i < numPins; i++ ){
int led = ledPins[ i ];
int desiredDistance = level * (i+1);
if( distance_measure > desiredDistance ){
digitalWrite( led, HIGH);
}else{
digitalWrite( led, LOW);
}
}
int sampleDistance( )
{
unsigned long startMillis = millis(); // Start of sample window
int highest_sample = 0;
int lowest_sample = 80;
// collect data for 100 mS
while (millis() - startMillis < sampleWindow)
{
int sample = analogRead( distance_sensor );
// invert the range, and convert it to a percent
sample = map( sample, 0, 4095, 1000, 0, );
// now see if the sample is the lowest;
if ( sample > highest_sample ){
highest_sample = sample ;
}
if ( sample < lowest_sample ){
lowest_sample = sample;
}
}
int peakToPeak = highest_sample - lowest_sample;
return peakToPeak;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .