Skip to content

Instantly share code, notes, and snippets.

@keyz182
Created August 17, 2012 17:35
Show Gist options
  • Save keyz182/3380883 to your computer and use it in GitHub Desktop.
Save keyz182/3380883 to your computer and use it in GitHub Desktop.
Ultrasonic Reader
#include <SoftwareSerial.h>
#include <Wire.h>
#include <NewPing.h>
#define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
SoftwareSerial mySerial(2, 3); // RX, TX, Serial is needed to debug
unsigned int pingSpeed = 50; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer; // Holds the next ping time.
int led = 7; //LED To indicate that the board is ON
int time = 0;
int request = 0; //What function was requested via i2c, only 0x02 at the moment
void setup(){
// Open serial communications and wait for port to open:
Serial.begin(115200);
pinMode(led, OUTPUT);
digitalWrite(led, 1);
Wire.begin(3); // join i2c bus with address #3
Wire.onRequest(requestEvent); // reg request event, called when something is requested over i2c
Wire.onReceive(receiveEvent); // reg receive event, called when we receive something over i2c
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Connected!!");
pingTimer = millis();
}
void loop() // run over and over
{
if (millis() >= pingTimer) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer += pingSpeed; // Set the next ping time.
sonar.ping_timer(echoCheck); // Send out the ping, calls "echoCheck" function every 24uS where you can check the ping status.
}
// Do other stuff here, really. Think of it as multi-tasking.
}
void receiveEvent(int howMany)
{
Serial.println("receive");
char in[howMany];
int counter = 0;
while(Wire.available()>0)
{
in[counter] = Wire.read(); // receive byte as a character
counter++;
}
if (in[0] == 0x02){
Serial.println("0x02 received");
request = 2; //set request so that receive knows what to send
}
Serial.println(in[0]);
}
void requestEvent()
{
if(request == 2){
const uint8_t *c=(uint8_t *)(&time); //point to the int with the round trip time as an array of 4 uint8_t
Wire.write(c,4); // respond with message of 4 bytes
Serial.println(time);
}
}
void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
if (sonar.check_timer()) { // This is how you check to see if the ping was received.
// Here's where you can add code.
time = sonar.ping_result;
//distance = sonar.ping_result / US_ROUNDTRIP_CM;
}
}
mport quick2wire.i2c as i2c
import time
import struct
address = 0x03 #Set the address to query
while True: #Just keep going!
with i2c.I2CMaster() as bus:
notdone=True
while(notdone):
time.sleep(0.1) #sleep just in case, there's a bug hiding somewhere that causes this to crash otherwise
bus.transaction(i2c.writing_bytes(address,0x02)) #tell the 'duino we want to retreive a reading
a,b,c,d = bus.transaction(i2c.reading(address,4))[0] #receive the reading
ar = bytearray([a,b,c,d]) #put vals in a byte array, needed for the unpack later
if not a==chr(0) or not b==chr(0) or not c==chr(0) or not d==chr(0): #make sure something was returned apart from zeroes
rtt = struct.unpack('<I',ar)[0] - 131072 #BUG! why is every value 131072 higher than it should be?
#We use pythons struct.unpack to convert the byte array to an unsigned int
notdone=False #if we're here, we got a reading
print("Distance is %f CM" % (rtt/57)) #57 uS to travel 1CM, RTT
else:
print("Not ready") #all zeroes returned
time.sleep(0.25) #lets sleep a little
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment