Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Forked from tomasero/ultrasound.ino
Last active August 29, 2015 14:22
Show Gist options
  • Save harrisonhjones/bb2d851bf8cc6e7b98ef to your computer and use it in GitHub Desktop.
Save harrisonhjones/bb2d851bf8cc6e7b98ef to your computer and use it in GitHub Desktop.
#define APP_VERSION 1.1
double distance = 0;
double echo = 0;
int ultraSoundSignal = D0; // Ultrasound signal pin
// Elapsed Millis Stuff
unsigned int publishInterval = 2000; // Every 2000 ms (2 seconds)
elapsedMillis timeElapsed; //declare global if you don't want it reset every time loop runs
void setup() {
pinMode(ultraSoundSignal,OUTPUT);
Spark.publish("status", "{ status: \"started up! "+String(APP_VERSION)+"\"}", 60, PRIVATE );
Spark.variable("distance", &distance, DOUBLE);
timeElapsed = 0;
}
void loop() {
if (timeElapsed > publishInterval)
{
updateDistance();
timeElapsed = 0;
}
}
void updateDistance() {
Spark.publish("status", "{ status: updateDistance}", 60, PRIVATE );
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
// please note that pulseIn has a 1sec timeout, which may
// not be desirable. Depending on your sensor specs, you
// can likely bound the time like this -- marcmerlin
// echo = pulseIn(ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
distance = (echo / 58.138);// * .39; //convert to CM then to inches
// distance = round(distance);
Spark.publish("distance", "Outside: " + String(distance));
if (distance <= 100) {
Spark.publish("distance", String(distance)); //it just posts one time? why?
}
}
double pulseIn(uint16_t pin, uint8_t state) {
GPIO_TypeDef* portMask = (PIN_MAP[pin].gpio_peripheral); // Cache the target's peripheral mask to speed up the loops.
uint16_t pinMask = (PIN_MAP[pin].gpio_pin); // Cache the target's GPIO pin mask to speed up the loops.
unsigned long pulseCount = 0; // Initialize the pulseCount variable now to save time.
unsigned long loopCount = 0; // Initialize the loopCount variable now to save time.
unsigned long loopMax = 20000000; // Roughly just under 10 seconds timeout to maintain the Spark Cloud connection.
// Wait for the pin to enter target state while keeping track of the timeout.
while (GPIO_ReadInputDataBit(portMask, pinMask) != state) {
if (loopCount++ == loopMax) {
return 0;
}
}
// Iterate the pulseCount variable each time through the loop to measure the pulse length; we also still keep track of the timeout.
while (GPIO_ReadInputDataBit(portMask, pinMask) == state) {
if (loopCount++ == loopMax) {
return 0;
}
pulseCount++;
}
// Return the pulse time in microseconds by multiplying the pulseCount variable with the time it takes to run once through the loop.
return pulseCount * 0.405; // Calculated the pulseCount++ loop to be about 0.405uS in length.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment