Skip to content

Commit fb875ab

Browse files
dvernierdavidlim
authored andcommitted
Changed examples to use VernierLib.
1 parent e900084 commit fb875ab

File tree

4 files changed

+76
-116
lines changed

4 files changed

+76
-116
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ Using Arduino and Vernier Sensors
44
Overview
55
--------
66

7-
Vernier has always supported hands-on, do-it-yourself projects for students (or teachers). The availability of very inexpensive, easy-to-program microcomputers, like the Arduino, makes projects easy and affordable.
7+
Vernier has always supported hands-on, do-it-yourself projects for students (or teachers). The availability of very inexpensive, easy-to-program microcomputers,
8+
like the Arduino, makes projects easy and affordable.
89

9-
We posted a guide to [Using Vernier sensors with Arduino][1]. It is a free guide to connecting, calibrating, writing programs, and doing fun projects with our sensors. The guide references sketches included in this repository.
10+
We posted a guide to [Using Vernier sensors with Arduino][1]. It is a free guide to connecting, calibrating, writing programs, and doing fun projects with our sensors.
11+
The guide references sketches included in this repository.
1012

1113
All sketches are developed by Vernier.
1214

13-
If you want an Arduino library to automatically detect Vernier sensors, please see [VernierLib][2]
15+
Include the [VernierLib library][2] in your version of the Arduino IDE to make using Vernier sensors easy.
1416

1517

1618
License
+25-33
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,62 @@
11
/*
2-
VernierAnalogSensorThreshold (v. 2014.09)
3-
Reads a Vernier analog (BTA) sensor connnected to pin A0 of the Arduino. This
4-
sketch displays the sensor reading on the Serial Monitor. As written, the
5-
reading will be displayed every half second. Change the variable
6-
TimeBetweenReadings to change the rate.
2+
VernierAnalogSensorThreshold (v. 2018.05)
3+
Reads a Vernier analog (BTA) sensor connnected to pin A0 of the Arduino, Analog 1 on
4+
the Vernier Interface Shield. This sketch displays the sensor reading on the Serial Monitor.
5+
As written, the reading will be displayed every half second. Change the variable
6+
timeBetweenReadings to change the rate.
77
88
This has code added to turn on line D13 if the sensor reading exceeds a
99
threshold.
1010
1111
See www.vernier.com/arduino for more information.
1212
1313
*/
14-
float Threshold =100 ;// this is the limit you are setting (here we are using 100)
14+
#include "VernierLib.h"
15+
VernierLib Vernier;
16+
float sensorReading;
17+
int readingNumber;
18+
float threshold =100 ;// this is the limit you are setting (here we are using 100)
1519
////////////////////////////////////////
16-
// This is the information on the sensor being used.
17-
//See the www.vernier.com/products/sensors.
18-
char Sensor[]="Hand Dynamometer";
19-
float Intercept = -19.295;
20-
float Slope = 175.416;
21-
int TimeBetweenReadings = 500;
20+
int timeBetweenReadings = 500;
2221
/////////////////////////////////////////
2322
void setup()
2423
{
2524
Serial.begin(9600); //initialize serial communication at 9600 bits per second
2625
pinMode(13, OUTPUT); // This specifies that digital line D13 will be used for output (not input, which is the default)
27-
Serial.println("Vernier Format 2");
28-
Serial.print(Sensor);
26+
Vernier.autoID();// this is the routine to do the autoID Serial.println("Vernier Format 2");
27+
Serial.println(Vernier.sensorName());
2928
Serial.print(" ");
3029
Serial.println("Readings taken using Ardunio");
3130
Serial.println("Data Set");
3231
Serial.print("Time");//long name
3332
Serial.print("\t"); //tab character
34-
Serial.println ("Force"); //long name, change to match sensor
33+
Serial.println(Vernier.sensorName());
3534
Serial.print ("t"); //short name
3635
Serial.print("\t"); //tab character
37-
Serial.println("F");//short name, change to match sensor
36+
Serial.println(Vernier.shortName());
3837
Serial.print("seconds");//units
3938
Serial.print("\t"); // tab character
40-
Serial.println ("newtons"); //change to match sensor
39+
Serial.println(Vernier.sensorUnits());
4140
}
4241
void loop()
4342
{
44-
float Count;
45-
float Voltage;
46-
float SensorReading;
47-
int ReadingNumber=0;
48-
float Time;
49-
//the print below does the division first to avoid overflows
50-
Serial.print(ReadingNumber/1000.0*TimeBetweenReadings);
51-
Count = analogRead(A0);
52-
Voltage = Count / 1023 * 5.0;// convert from count to raw voltage
53-
SensorReading= Intercept + Voltage * Slope;
54-
Serial.print(" ");
55-
Serial.println(SensorReading);
56-
delay(TimeBetweenReadings);// delay in between reads for stability
57-
if (SensorReading>Threshold){
43+
sensorReading =Vernier.readSensor();
44+
Serial.print(readingNumber/1000.0*timeBetweenReadings);
45+
Serial.print ("\t");
46+
Serial.println(sensorReading);
47+
48+
delay(timeBetweenReadings);// delay in between reads for stability
49+
if (sensorReading>threshold){
5850
digitalWrite(13, HIGH);
5951
}// This turns on the LED
6052
else
6153
{
6254
digitalWrite(13,LOW);
6355
}
64-
ReadingNumber++;
56+
readingNumber++;
6557
}
6658

6759

6860

6961

70-
62+

VernierLaserTracker/VernierLaserTracker.ino

+22-31
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
/*VernierLaserTracker (v 2014.4)
1+
/*VernierLaserTracker (v 2018.5)
22
Monitors the position of an object using a Vernier Motion Detector
33
and then aims a laser pointer mounted on a servo motor at the object.
44
5-
Because of the use of a trig function (arctangent) in the calculations,
6-
this sketch requires the math.h library. It also requires the servo library.
5+
This sketch uses three Arduino libraries. To read the Motion Detector, it uses
6+
the VernierLib library. Because of the use of a trig function (arctangent) in the
7+
calculations, this sketch requires the math.h library. It also requires the servo
8+
library to control the servo motor.
79
810
See www.vernier.com/arduino for more information.
911
*/
1012
#include <math.h>
1113
#include <Servo.h>
1214
Servo myservo; // create servo object to control a servo
13-
long time;// clock reading in microseconds
14-
long Duration; // time it take echo to return
15-
const int SpeedOfSound = 344; //in m/s
16-
double Distance;// in centimeters
17-
int val = 0;
18-
const int TriggerPin = 3; //trigger pin
19-
const int EchoPin = 2;// echo pin
15+
#include "VernierLib.h"
16+
VernierLib Vernier;
17+
float distance = 0;// distqnce in cm
2018
int Range=100; //distance in cm from Laser Pointer/Servo motor to Motion Detector
19+
const int Laser = 6;//laser pin, connect laser to pin 6 of the Arduino, which is the first line on Digital 2
20+
2121
void setup()
2222
{
2323
myservo.attach(9); // attaches the servo on pin 9 to the servo object
24-
// initialize the Ping pin as an output:
25-
pinMode(TriggerPin, OUTPUT);
26-
pinMode(EchoPin, INPUT); //this is the pin that goes high when an echo is received
2724
// initialize serial communication at 9600 bits per second:
2825
Serial.begin(9600);
2926
Serial.println(" ");
@@ -44,25 +41,19 @@ void loop()
4441
double Angle =0;
4542
float Degrees;
4643
int ServoSetting;
47-
digitalWrite(TriggerPin, LOW);
48-
delayMicroseconds(4000);
49-
digitalWrite(TriggerPin, HIGH); // start the ultrasound pulse
50-
time = micros(); //note time
51-
delayMicroseconds(900); //delay during the blanking time
52-
do
53-
{
54-
val =digitalRead(EchoPin);
55-
// if no echo, repeat loop and wait:
56-
}
57-
while (val == LOW) ;
58-
Duration =micros() - time;
59-
/* The speed of sound is 344 m/s.
60-
The ultrasound travels out and back, so to find the distance of the
61-
object we take half of the distance traveled.*/
62-
Distance= Duration *SpeedOfSound/2/10000 ;// note convert to cm
63-
Serial.print(Distance);
44+
distance = Vernier.readMotionDetector();
45+
Serial.print(distance);
46+
Serial.println(" cm");
47+
if (distance < 120)
48+
{
49+
digitalWrite(Laser, HIGH);// if closest item is within 120 cm it turns laser on
50+
}
51+
else digitalWrite(Laser, LOW);
52+
delay(100);//delay a tenth of a second
53+
54+
Serial.print(distance);
6455
Serial.print("\t"); // tab character
65-
ArcTan = atan(Distance/Range);
56+
ArcTan = atan(distance/Range);
6657
Serial.print(ArcTan);
6758
Serial.print("\t"); // tab character
6859
Degrees= ArcTan*57.29578; //convert radians to degrees

VernierLaserTrackerAutoOn/VernierLaserTrackerAutoOn.ino

100644100755
+24-49
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,26 @@
1-
#include <Servo.h>
2-
3-
/*VernierLaserTrackerAutoOn (v 2014.12)
1+
/*VernierLaserTracker (v 2018.5)
42
Monitors the position of an object using a Vernier Motion Detector
5-
and then aims a laser pointer mounted on a servo motor at the object.
6-
This version also turns on the laser pointer when an object is
7-
detected within 120 cm of Motion Detector. This requires a hard
8-
wired laser pointer!
3+
and then aims a laser pointer mounted on a servo motor at the object.
94
10-
Because of the use of a trig function (arctangent) in the calculations,
11-
this sketch requires the math.h library. It also requires the servo library.
5+
This sketch uses three Arduino libraries. To read the Motion Detector, it uses
6+
the VernierLib library. Because of the use of a trig function (arctangent) in the
7+
calculations, this sketch requires the math.h library. It also requires the servo
8+
library to control the servo motor.
129
1310
See www.vernier.com/arduino for more information.
1411
*/
1512
#include <math.h>
1613
#include <Servo.h>
1714
Servo myservo; // create servo object to control a servo
18-
long time;// clock reading in microseconds
19-
long Duration; // time it take echo to return
20-
const int SpeedOfSound = 344; //in m/s
21-
double Distance;// in centimeters
22-
int val = 0;
23-
int laserval = 0;
24-
const int TriggerPin = 3; //trigger pin
25-
const int EchoPin = 2;// echo pin
26-
const int Laser = 6;//laser pin
15+
#include "VernierLib.h"
16+
VernierLib Vernier;
17+
float distance = 0;// distqnce in cm
2718
int Range=100; //distance in cm from Laser Pointer/Servo motor to Motion Detector
19+
const int laser = 6;//laser pin, connect laser to pin 6 of the Arduino, which is the first line on Digital 2
20+
2821
void setup()
2922
{
3023
myservo.attach(9); // attaches the servo on pin 9 to the servo object
31-
// initialize the Ping pin as an output:
32-
pinMode(TriggerPin, OUTPUT);
33-
pinMode(EchoPin, INPUT); //this is the pin that goes high when an echo is received
34-
pinMode(Laser, OUTPUT); //this pin goes high to turn laser on
3524
// initialize serial communication at 9600 bits per second:
3625
Serial.begin(9600);
3726
Serial.println(" ");
@@ -52,33 +41,19 @@ void loop()
5241
double Angle =0;
5342
float Degrees;
5443
int ServoSetting;
55-
digitalWrite(TriggerPin, LOW);
56-
delayMicroseconds(4000);
57-
digitalWrite(TriggerPin, HIGH); // start the ultrasound pulse
58-
time = micros(); //note time
59-
delayMicroseconds(900); //delay during the blanking time
60-
do
61-
{
62-
val =digitalRead(EchoPin);
63-
// if no echo, repeat loop and wait:
64-
}
65-
while (val == LOW) ;
66-
Duration =micros() - time;
67-
/* The speed of sound is 344 m/s.
68-
The ultrasound travels out and back, so to find the distance of the
69-
object we take half of the distance traveled.*/
70-
Distance= Duration *SpeedOfSound/2/10000 ;// note convert to cm
71-
if (Distance < 120)
72-
{
73-
digitalWrite(Laser, HIGH);// if closest item is within 120 cm it turns laser on
74-
}
75-
else
76-
{
77-
digitalWrite(Laser, LOW);
78-
}
79-
Serial.print(Distance);
44+
distance = Vernier.readMotionDetector();
45+
Serial.print(distance);
46+
Serial.println(" cm");
47+
if (distance < 120)
48+
{
49+
digitalWrite(laser, HIGH);// if closest item is within 120 cm it turns laser on
50+
}
51+
else digitalWrite(laser, LOW);
52+
delay(100);//delay a tenth of a second
53+
54+
Serial.print(distance);
8055
Serial.print("\t"); // tab character
81-
ArcTan = atan(Distance/Range);
56+
ArcTan = atan(distance/Range);
8257
Serial.print(ArcTan);
8358
Serial.print("\t"); // tab character
8459
Degrees= ArcTan*57.29578; //convert radians to degrees
@@ -89,4 +64,4 @@ digitalWrite(Laser, LOW);
8964
}
9065

9166

92-
67+

0 commit comments

Comments
 (0)