Skip to content

Commit 7f83145

Browse files
committed
first
0 parents  commit 7f83145

File tree

2 files changed

+366
-0
lines changed

2 files changed

+366
-0
lines changed

arduino_echo_locator.ino

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// ----- arduino pinouts
2+
#define Trig1 4 //sensor A "Trig" pin
3+
#define Echo1 5 //sensor A "Echo" pin
4+
5+
#define Trig2 6 //sensor B "Trig" pin
6+
#define Echo2 7 //sensor B "Echo" pin
7+
8+
// ----- results
9+
float Baseline = 50; //distance between the transducers (cm)
10+
float Distance1; //from active sender (cm)
11+
float Distance2; //from passive receiver (cm)
12+
13+
// ----- task scheduler
14+
int TaskTimer1 = 0; //task 1 (see ISR(TIMER2_COMPA_vect)
15+
bool TaskFlag1 = false; //flag 1
16+
17+
18+
// ===============================
19+
// setup
20+
// ===============================
21+
void setup() {
22+
23+
// ----- configure serial port
24+
Serial.begin(115200);
25+
26+
// ----- configure arduino pinouts
27+
pinMode(Echo1, INPUT); //make echo pins inputs
28+
pinMode(Echo2, INPUT);
29+
pinMode(Trig1, OUTPUT); //make trig pins OUTPUT
30+
pinMode(Trig2, OUTPUT);
31+
digitalWrite(Trig1, LOW); //set trig pins LOW
32+
digitalWrite(Trig2, LOW);
33+
34+
// ----- configure Timer 2 to generate a compare-match interrupt every 1mS
35+
noInterrupts(); //disable interrupts
36+
TCCR2A = 0; //clear control registers
37+
TCCR2B = 0;
38+
TCCR2B |= (1 << CS22) | //16MHz/128=8uS
39+
(1 << CS20) ;
40+
TCNT2 = 0; //clear counter
41+
OCR2A = 125 - 1; //8uS*125=1mS (allow for clock propagation)
42+
TIMSK2 |= (1 << OCIE2A); //enable output compare interrupt
43+
interrupts(); //enable interrupts
44+
}
45+
46+
// ===============================
47+
// loop()
48+
// ===============================
49+
void loop()
50+
{
51+
// ----- measure object distances
52+
if (TaskFlag1)
53+
{
54+
TaskFlag1 = false;
55+
measure();
56+
57+
// -----Distance1 and Distance2 readings to the display
58+
Serial.print(Distance1); Serial.print(","); Serial.println(Distance2);
59+
}
60+
}
61+
62+
// ===============================
63+
// task scheduler (1mS interrupt)
64+
// ===============================
65+
ISR(TIMER2_COMPA_vect)
66+
{
67+
// ----- timers
68+
TaskTimer1++; //task 1 timer
69+
70+
// ----- task1
71+
if (TaskTimer1 > 499) //interval between pings (50mS=423cm)
72+
{
73+
TaskTimer1 = 0; //reset timer
74+
TaskFlag1 = true; //signal loop() to perform task
75+
}
76+
}
77+
78+
// ===============================
79+
// measure distances
80+
// ===============================
81+
void measure()
82+
{
83+
// ----- locals
84+
unsigned long start_time; //microseconds
85+
unsigned long finish_time1; //microseconds
86+
unsigned long finish_time2; //microseconds
87+
unsigned long time_taken; //microseconds
88+
boolean echo_flag1; //flags reflect state of echo line
89+
boolean echo_flag2;
90+
91+
// ----- send 10uS trigger pulse
92+
digitalWrite(Trig1, HIGH);
93+
digitalWrite(Trig2, HIGH);
94+
delayMicroseconds(10);
95+
digitalWrite(Trig1, LOW);
96+
digitalWrite(Trig2, LOW);
97+
98+
// ----- wait for both echo lines to go high
99+
while (!digitalRead(Echo1));
100+
while (!digitalRead(Echo2));
101+
102+
// ----- record start time
103+
start_time = micros();
104+
105+
// ----- reset the flags
106+
echo_flag1 = false;
107+
echo_flag2 = false;
108+
109+
// ----- measure echo times
110+
while ((!echo_flag1) || (!echo_flag2))
111+
{
112+
// ----- Echo1
113+
if ((!echo_flag1) && (!digitalRead(Echo1))) //Echo1 received
114+
{
115+
echo_flag1 = true;
116+
finish_time1 = micros();
117+
time_taken = finish_time1 - start_time;
118+
Distance1 = ((float)time_taken) / 59; //use 59 as there is a return path
119+
}
120+
121+
// ----- Echo2
122+
if ((!echo_flag2) && (!digitalRead(Echo2))) //Echo2 received
123+
{
124+
echo_flag2 = true;
125+
finish_time2 = micros();
126+
time_taken = finish_time2 - start_time;
127+
Distance2 = ((float)time_taken) / 29.5; //use 29.5 as there is no return path
128+
}
129+
}
130+
}

arduino_echo_locator.pde

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
// ----- serial port
2+
import processing.serial.*; //import the serial library
3+
Serial myPort; //the Serial port object
4+
final int Baud_rate = 115200; //communication speed
5+
String Input_string; //used for incoming data
6+
7+
// -----custom shape
8+
PShape Object; //give the shape a name
9+
int Frame_count = 0; //frame counter
10+
boolean Frame_visible = true; //true=visible; false=invisible
11+
12+
// ----- display graphics
13+
PGraphics Canvas; //name of drawing area to be created
14+
PFont myFont; //name of font to be created
15+
float Baseline = 50; //triangle baseline (cm)
16+
float X; //X coordinate in (cm)
17+
float Y; //Y coordinate in (cm)
18+
19+
// ----- sensor distance BELOW baseline (cm)
20+
float Offset = 50; //assumes square display
21+
22+
// =========================
23+
// setup
24+
// ==========================
25+
void setup() {
26+
27+
// ----- configure screen
28+
size(800, 800, P3D); //define window size, 3D
29+
background(0); //black
30+
frameRate(60); //60 frames per second
31+
32+
// ----- create a drawing area for fading the beam
33+
Canvas = createGraphics(width, height);
34+
35+
// ------ create the screen font
36+
myFont = createFont("Arial Black", 20);
37+
38+
// ----- configure the Object
39+
Object = createShape(ELLIPSE, 0, 0, 30, 30); //create the Object
40+
Object.setFill(color(255, 0, 0, 255)); //red, opaque
41+
Object.setStroke(color(255, 0, 0, 255)); //red, opaque
42+
43+
// ----- initialize the serial port
44+
/*
45+
IMPORTANT:
46+
If your display can't see the arduino try changing the [number] associated
47+
with your COM port.
48+
49+
The code line " printArray(Serial.list());" generates a list
50+
of [numbers] within brackets. e.g: [0] "COM5"
51+
52+
The [number] inside the square bracket MUST match the [number] in the
53+
code line "myPort = new Serial(this, Serial.list()[0], Baud_rate);"
54+
*/
55+
printArray(Serial.list()); //lists your COM ports on screen
56+
myPort = new Serial(this, Serial.list()[0], Baud_rate);
57+
myPort.bufferUntil('\n');
58+
}
59+
60+
// ==========================
61+
// draw
62+
// ==========================
63+
void draw()
64+
{
65+
// ----- refresh the screen
66+
background(0); //black background
67+
textFont(myFont, 20); //specify font to be used
68+
draw_grid(); //draw grid
69+
draw_object();
70+
}
71+
72+
// =======================
73+
// serial event (called with each Arduino data string)
74+
// =======================
75+
void serialEvent(Serial myPort)
76+
{
77+
// ----- wait for a line-feed
78+
Input_string = myPort.readStringUntil('\n');
79+
println(Input_string); //visual feedback
80+
81+
// ----- validate
82+
if (Input_string != null)
83+
{
84+
// ----- trim whitespace
85+
Input_string = trim(Input_string);
86+
String[] values = split(Input_string, ',');
87+
88+
// ----- gather Heron variables
89+
float a = float(values[1]) - float(values[0]); //d2 (vertex -> sensor B)
90+
float b = float(values[0]); //d1 (vertex -> sensor A)
91+
float c = Baseline; //baseline
92+
//float d = c*1.414; //display diagonal (square)
93+
float d = sqrt(150*150 + 100*100); //diagonal (display + offset)
94+
float s = (a + b + c)/2; //semi-perimeter
95+
96+
// ----- validate distances
97+
/* eliminate bogus errors */
98+
boolean distances_valid = true;
99+
if
100+
(
101+
(a < 0) || //d1 must be less than d2
102+
(b > d) || //d1 out-of-range
103+
(a > d) || //d2 out-of-range
104+
((s - a) < 0) || //these values must be positive
105+
((s - b) < 0) ||
106+
((s - c) < 0)
107+
)
108+
{
109+
distances_valid=false;
110+
X=1000; //move flashing dot off-screen
111+
Y=1000; //move flashing dot off-screen
112+
}
113+
114+
// ----- apply Heron's formula
115+
if (distances_valid)
116+
{
117+
float area = sqrt(s * (s - a) * (s - b) * (s - c));
118+
Y = area * 2 / c;
119+
X = sqrt(b * b - Y * Y);
120+
121+
// ----- display data for valid echos
122+
print(" d1: ");
123+
println(b);
124+
print(" d2: ");
125+
println(a);
126+
print(" base: ");
127+
println(c);
128+
print("offset: ");
129+
println(Offset);
130+
print(" s: ");
131+
println(s);
132+
print(" area: ");
133+
println(area);
134+
print(" X: ");
135+
println(X);
136+
print(" Y: ");
137+
println(Y-Offset);
138+
println("");
139+
}
140+
myPort.clear(); //clear the receive buffer
141+
}
142+
}
143+
144+
// ==========================
145+
// draw_grid
146+
// ==========================
147+
void draw_grid()
148+
{
149+
pushMatrix();
150+
151+
scale(0.8);
152+
translate(width*0.1, height*0.10);
153+
fill(0);
154+
stroke(255);
155+
156+
// ----- border
157+
strokeWeight(4);
158+
rect(0, 0, width, height, 20, 20, 20, 20);
159+
160+
// ----- horizontal lines
161+
strokeWeight(1);
162+
line(0, height*0.1, width, height*0.1);
163+
line(0, height*0.2, width, height*0.2);
164+
line(0, height*0.3, width, height*0.3);
165+
line(0, height*0.4, width, height*0.4);
166+
line(0, height*0.5, width, height*0.5);
167+
line(0, height*0.6, width, height*0.6);
168+
line(0, height*0.7, width, height*0.7);
169+
line(0, height*0.8, width, height*0.8);
170+
line(0, height*0.9, width, height*0.9);
171+
172+
// ----- vertical lines
173+
line(width*0.1, 0, width*0.1, height);
174+
line(width*0.2, 0, width*0.2, height);
175+
line(width*0.3, 0, width*0.3, height);
176+
line(width*0.4, 0, width*0.4, height);
177+
line(width*0.5, 0, width*0.5, height);
178+
line(width*0.6, 0, width*0.6, height);
179+
line(width*0.7, 0, width*0.7, height);
180+
line(width*0.8, 0, width*0.8, height);
181+
line(width*0.9, 0, width*0.9, height);
182+
183+
// ----- label the X-axis
184+
fill(255); //white text
185+
textAlign(LEFT, TOP);
186+
text("0", -20, height+10); //0cm
187+
text("50", width*0.5-20, height+10); //50cm
188+
text("100cm", width-20, height+10); //100cm
189+
190+
// ----- label the y-axis
191+
textAlign(RIGHT, BOTTOM);
192+
text("100cm", 10, -10); //100cm
193+
textAlign(RIGHT, CENTER);
194+
text("50", -10, height/2); //100cm
195+
196+
popMatrix();
197+
}
198+
199+
// ==========================
200+
// draw_object
201+
// ==========================
202+
void draw_object()
203+
{
204+
pushMatrix();
205+
scale(0.8);
206+
stroke(0, 255, 0);
207+
strokeWeight(1);
208+
translate(width*0.1, height*1.1); //(0,0) now lower-left corner
209+
210+
// ----- make the object flash
211+
if ((frameCount-Frame_count)>4)
212+
{
213+
Frame_visible = !Frame_visible;
214+
Frame_count = frameCount;
215+
}
216+
217+
// ----- object color scheme
218+
if (Frame_visible)
219+
{
220+
// ----- make object visible
221+
Object.setFill(color(255, 0, 0, 255)); //opaque
222+
Object.setStroke(color(255, 0, 0, 255)); //opaque
223+
} else
224+
{
225+
// ----- hide the object
226+
Object.setFill(color(255, 0, 0, 0)); //clear
227+
Object.setStroke(color(255, 0, 0, 0)); //clear
228+
}
229+
230+
// ----- draw the object
231+
pushMatrix();
232+
translate(X/100*width, -(Y-Offset)/100*height);
233+
shape(Object);
234+
popMatrix();
235+
popMatrix();
236+
}

0 commit comments

Comments
 (0)