1
1
/*
2
- VernierMotionDetectorPID (v 2015.05 )
2
+ VernierMotionDetectorPID (v 2017.08 )
3
3
Takes data from a Vernier Motion Detector connected to Digital 1 connector.
4
4
Uses PID to control fan to elevate ping pong ball to target height. The ball is in
5
5
plastic tube 0.6 m tall with a fan at the bottom and a motion detector at the top.
6
6
This version uses a Digital Control Unit (DCU) connected to the Digital 2 port to
7
7
control the fan.
8
8
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.
20
11
21
12
The PID control is modified from http://playground.arduino.cc/Code/PIDLibaryBasicExample
22
13
and takes the calculated input from the motion detector ("distance") and controls analog PWM
@@ -26,111 +17,59 @@ http://playground.arduino.cc/Code/PIDLibrary.
26
17
As written, the reading will feed to the PID control continuously and data fed to
27
18
Serial Print.
28
19
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.
31
24
32
25
See www.vernier.com/arduino for more information.
33
26
*/
27
+
34
28
#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.*/
35
36
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 ()
46
38
{
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:
57
39
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
+
59
44
Serial.println (" Vernier Format 2" );
60
45
Serial.println (" Motion Detector Readings taken using Ardunio" );
61
46
Serial.println (" Data Set" );
62
- Serial.print (" Time for Echo" );// long name
63
- Serial.print (" \t " ); // tab character
47
+
64
48
Serial.print (" Set Point" );
65
49
Serial.print (" \t " ); // tab character
66
50
Serial.print (" Distance" ); // long name
67
51
Serial.print (" \t " );
68
52
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 " );
74
56
Serial.print (" \t " );
75
- Serial.print (" D " ); // short name
57
+ Serial.print (" d (cm) " ); // short name
76
58
Serial.print (" \t " );
77
59
Serial.print (" \t " );
78
60
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
+ }
90
62
void loop ()
91
63
{
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 ();
115
65
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 " );
124
69
Serial.print (" \t " );
125
- // This would be the end of the grayed out portion
126
- Serial.print (Distance);
70
+ Serial.print (distance);
127
71
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 );
132
75
}
133
-
134
-
135
-
136
-
0 commit comments