Skip to content

Commit 6f0ed3a

Browse files
committed
Changed PingPongPID to use Vernier library and eliminated notes for integrating VPython. I also modified PID parameters to work with my particular physical setup.
1 parent df6456b commit 6f0ed3a

File tree

1 file changed

+34
-95
lines changed

1 file changed

+34
-95
lines changed

Diff for: VernierPingPongPID/VernierPingPongPID.ino

+34-95
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
/*
2-
VernierMotionDetectorPID (v 2015.05)
2+
VernierMotionDetectorPID (v 2017.08)
33
Takes data from a Vernier Motion Detector connected to Digital 1 connector.
44
Uses PID to control fan to elevate ping pong ball to target height. The ball is in
55
plastic tube 0.6 m tall with a fan at the bottom and a motion detector at the top.
66
This version uses a Digital Control Unit (DCU) connected to the Digital 2 port to
77
control the fan.
88
9-
Motion detector portion of this sketch is taken from VernierMotionDetector (v 2014.09)
10-
It measures the time taken for the ultrasound to return (in microseconds)
11-
and then calculates the corresponding distance (based on the speed of ultrasound
12-
in air) and displays the distance (in cm) on the Serial Monitor.
13-
14-
Here is how the Vernier Motion Detector works:
15-
- when pin 2 on BTD is pulled high, this triggers the ultrasound pulse
16-
- the program then starts timing but then delays 0.9 ms *(blanking time,
17-
0.9 seconds is the time it takes ultrasound to travel 15 cm twice (round trip))
18-
- the program then monitors pin 1 on the BTD, waiting for it to go high.
19-
This happens when an echo is detected.
9+
Motion detector is read using the Vernier Library that calculates the distance based
10+
the echo of an ultrasonic sound wave.
2011
2112
The PID control is modified from http://playground.arduino.cc/Code/PIDLibaryBasicExample
2213
and takes the calculated input from the motion detector ("distance") and controls analog PWM
@@ -26,111 +17,59 @@ http://playground.arduino.cc/Code/PIDLibrary.
2617
As written, the reading will feed to the PID control continuously and data fed to
2718
Serial Print.
2819
29-
This has been used with VPython to create a virtual display of the ping pong ball.
30-
Some notes in the sketch are there to aid ease of reading data into VPython.
20+
There are several ways you can modify this. We have previously linked the output
21+
of this program to VPython to create a virtual display of the ping pong ball.
22+
Search the internet for tips on combining Arduino and VPython. You can also vary
23+
set point so that it seeks a variety of heights - definitely more eye catching.
3124
3225
See www.vernier.com/arduino for more information.
3326
*/
27+
3428
#include <PID_v1.h>
29+
#include "VernierLib.h"
30+
VernierLib Vernier;
31+
double setPoint, distance, output; //define variables we'll be connecting to
32+
PID myPID(&distance, &output, &setPoint, 3,6.0,6.0, REVERSE);
33+
/*Specify the links and initial tuning parameters. These work for a system
34+
with a 0.6 m tube and the particular fan we are using. Depends on power supply
35+
to the fan as well as cowling around fan.*/
3536

36-
//Define Variables we'll be connecting to
37-
double Setpoint, Distance, Output;
38-
39-
//Specify the links and initial tuning parameters
40-
PID myPID(&Distance, &Output, &Setpoint, 9,1.0,2.0, REVERSE);
41-
42-
const int TriggerPin = 3; //trigger pin
43-
const int EchoPin = 2;// echo pin
44-
45-
void setup()
37+
void setup()
4638
{
47-
// initialize the Ping pin as an output:
48-
pinMode(TriggerPin, OUTPUT);
49-
pinMode(EchoPin, INPUT); //this is the pin that goes high when an echo is received
50-
51-
Setpoint = 30; // setpoint (cm from top)
52-
53-
//turn the PID on
54-
myPID.SetMode(AUTOMATIC);
55-
56-
// initialize serial communication at 9600 bits per second:
5739
Serial.begin(9600);
58-
// The following can be grayed out for a VPython serial display
40+
41+
setPoint = 40; // setpoint (cm from top)
42+
myPID.SetMode(AUTOMATIC); //turn the PID on
43+
5944
Serial.println("Vernier Format 2");
6045
Serial.println("Motion Detector Readings taken using Ardunio");
6146
Serial.println("Data Set");
62-
Serial.print("Time for Echo");//long name
63-
Serial.print("\t"); //tab character
47+
6448
Serial.print("Set Point");
6549
Serial.print("\t"); // tab character
6650
Serial.print ("Distance"); //long name
6751
Serial.print("\t");
6852
Serial.println ("Output from PID");
69-
Serial.print("delta t");//short name
70-
Serial.print("\t"); //tab character
71-
Serial.print("\t"); //tab character
72-
Serial.print("SP");
73-
Serial.print("\t"); // tab character
53+
54+
Serial.print("SP (cm)");
55+
Serial.print("\t");
7456
Serial.print("\t");
75-
Serial.print ("D"); //short name
57+
Serial.print ("d (cm)"); //short name
7658
Serial.print("\t");
7759
Serial.print("\t");
7860
Serial.println("0 - 255");
79-
Serial.print("micro seconds");//units
80-
Serial.print("\t"); // tab character
81-
Serial.print ("cm"); //units
82-
Serial.print("\t"); // tab character
83-
Serial.print("\t");
84-
Serial.print ("cm"); //units
85-
Serial.print("\t");
86-
Serial.print("\t");
87-
Serial.println("#");
88-
// This would be the end of the grayed out portion if using VPython
89-
}
61+
}
9062
void loop()
9163
{
92-
long time; // clock reading in microseconds
93-
long Duration; // time it take echo to return
94-
const float SpeedOfSound = 340; //in m/s
95-
int val = 0;
96-
digitalWrite(TriggerPin, LOW);
97-
delayMicroseconds(4000);
98-
digitalWrite(TriggerPin, HIGH); // start the ultrasound pulse
99-
time = micros(); //note time
100-
delayMicroseconds(900); //delay during the blanking time
101-
do
102-
{
103-
val =digitalRead(EchoPin);
104-
// if no echo, repeat loop and wait:
105-
}
106-
while (val == LOW) ;
107-
Duration =micros() - time;
108-
109-
/* The speed of sound is 340 m/s.
110-
The ultrasound travels out and back, so to find the distance of the
111-
object we take half of the distance traveled.*/
112-
113-
Distance= Duration *SpeedOfSound/2/10000 ;// note convert to cm
114-
64+
distance = Vernier.readMotionDetector();
11565
myPID.Compute();
116-
analogWrite(6,Output);
117-
118-
//* Grayed out for python connection
119-
Serial.print(Duration);// print the time it took until the echo
120-
Serial.print("\t"); // tab character
121-
Serial.print("\t");
122-
Serial.print(Setpoint);
123-
Serial.print("\t"); // tab character
66+
analogWrite(6,output);
67+
Serial.print(setPoint);
68+
Serial.print("\t");
12469
Serial.print("\t");
125-
// This would be the end of the grayed out portion
126-
Serial.print(Distance);
70+
Serial.print(distance);
12771
Serial.print("\t");
128-
Serial.print("\t"); //tab character
129-
Serial.println(Output);
130-
delay (100);
131-
72+
Serial.print("\t");
73+
Serial.println(output);
74+
delay (10);
13275
}
133-
134-
135-
136-

0 commit comments

Comments
 (0)