Created
May 22, 2014 02:32
-
-
Save xcvista/d54877453d149e30f112 to your computer and use it in GitHub Desktop.
A GPIO echo meter that sends echo time length over I2C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
uint32_t ms; | |
int mode; | |
#define WIRE_ADDR 0x8 | |
#define TRIGGER 38 | |
#define ECHO 36 | |
void setup() | |
{ | |
// Set up sensor | |
pinMode(TRIGGER, OUTPUT); | |
pinMode(ECHO, INPUT); | |
pinMode(13, OUTPUT); | |
mode = 1; | |
// Start I2C | |
Wire.begin(WIRE_ADDR); | |
Wire.onRequest(i2cRead); | |
} | |
void loop() | |
{ | |
// Make measuremrnt | |
digitalWrite(TRIGGER, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(TRIGGER, LOW); | |
ms = pulseIn(ECHO, HIGH, 1UL << 22); | |
digitalWrite(13, mode); | |
mode = !mode; | |
delay(10); | |
} | |
void i2cRead() | |
{ | |
Wire.write((const char *)&ms, sizeof(ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment