Created
January 2, 2014 09:31
-
-
Save Justasic/8216849 to your computer and use it in GitHub Desktop.
Arduino ultrasonic LED range graph thing (note this appends data, not renews it. It's a bug I might fix later)
This file contains 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
#!/usr/bin/env python | |
import serial, time | |
import pyqtgraph as pg | |
import pyqtgraph.multiprocess as mp | |
class Queue: | |
"""A sample implementation of a First-In-Never-Out | |
data structure.""" | |
def __init__(self, length = 0): | |
self.in_stack = [] | |
self.maxlen = length | |
def __str__(self): | |
return str(self.in_stack) | |
def __repr__(self): | |
return "<%s with %s>" % (self.__class__, self.in_stack) | |
def push(self, obj): | |
self.in_stack.append(obj) | |
if self.maxlen != 0: | |
if len(self.in_stack) > self.maxlen: | |
del self.in_stack[0] | |
def get(self): | |
return self.in_stack | |
if __name__ == "__main__": | |
# Initialize the plot window | |
pg.mkQApp() | |
proc = mp.QtProcess() | |
rpg = proc._import('pyqtgraph') | |
plotwin = rpg.plot() | |
curve = plotwin.plot(pen='y') | |
plotwin.setDownsampling(auto=True) | |
plotwin.setLabel('left', "distance", 'cm') | |
plotwin.setLabel('bottom', "time") | |
plotwin.enableAutoRange(False) | |
data = proc.transfer([]) | |
q = Queue(15) | |
s = serial.Serial('/dev/ttyACM0', 9600, timeout=1) | |
while True: | |
distance = s.readline().strip() | |
if distance == '-1': | |
distance = 0 | |
q.push(int(distance)) | |
data.extend(q.get(), _callSync='off') | |
curve.setData(y=data, _callSync='off') |
This file contains 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
/* | |
HC-SR04 Ping distance sensor: | |
VCC to arduino 5v | |
GND to arduino GND | |
Echo to Arduino pin 7 | |
Trig to Arduino pin 8 | |
This sketch originates from Virtualmix: http://goo.gl/kJ8Gl | |
Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html | |
And modified further by ScottC here: http://arduinobasics.blogspot.com/ | |
on 10 Nov 2012. | |
*/ | |
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); | |
#define echoPin 7 // Echo Pin | |
#define trigPin 8 // Trigger Pin | |
#define LEDPin 13 // Onboard LED | |
int maximumRange = 200; // Maximum range needed | |
int minimumRange = 0; // Minimum range needed | |
long duration, distance; // Duration used to calculate distance | |
void setup() { | |
Serial.begin (9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required) | |
lcd.begin(16, 2); | |
lcd.print("Distance (cm):"); | |
} | |
void redraw() | |
{ | |
lcd.clear(); | |
lcd.print("Distance (cm):"); | |
// set the cursor to column 0, line 1 | |
// (note: line 1 is the second row, since counting begins with 0): | |
lcd.setCursor(0, 1); | |
} | |
void loop() { | |
// print the number of seconds since reset: | |
//lcd.print(millis()/1000); | |
/* The following trigPin/echoPin cycle is used to determine the | |
distance of the nearest object by bouncing soundwaves off of it. */ | |
digitalWrite(trigPin, LOW); | |
delayMicroseconds(2); | |
digitalWrite(trigPin, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
//Calculate the distance (in cm) based on the speed of sound. | |
distance = duration/58.2; | |
redraw(); | |
if (distance >= maximumRange || distance <= minimumRange){ | |
/* Send a negative number to computer and Turn LED ON | |
to indicate "out of range" */ | |
Serial.println("-1"); | |
lcd.print("(unknown)"); | |
digitalWrite(LEDPin, HIGH); | |
} | |
else { | |
/* Send the distance to the computer using Serial protocol, and | |
turn LED OFF to indicate successful reading. */ | |
Serial.println(distance); | |
lcd.print(distance); | |
digitalWrite(LEDPin, LOW); | |
} | |
//Delay 50ms before next reading. | |
delay(50); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment