|
| 1 | +#include "VernierLib.h" |
| 2 | +VernierLib Vernier; |
| 3 | +float sensorReading; |
| 4 | +/// |
| 5 | +const int buttonPin=12; //button on Vernier Interface Shield |
| 6 | +int buttonState = 0;//variable for reading the pushbutton |
| 7 | +char namestring[17];//character array for printing strings to display |
| 8 | +char tempstring[16]; // character array used to create string for displaying numbers |
| 9 | +#include <SoftwareSerial.h> //library used in printing to display |
| 10 | +SoftwareSerial mySerial(3,9); //for display, pin 9 = TX, pin 3 = RX (unused) |
| 11 | +/// |
| 12 | +void setup(void) |
| 13 | +{ |
| 14 | + Serial.begin(9600); |
| 15 | + /// |
| 16 | + mySerial.begin(9600); // for sending characters to display |
| 17 | + delay(500); // wait for display to boot up |
| 18 | + mySerial.write(124); // adjust backlight brightness of display |
| 19 | + mySerial.write(150); //max=157, 150=73%, 130=40%,128=off |
| 20 | + /// |
| 21 | + Vernier.autoID();// this is the routine to do the autoID |
| 22 | + printSensorInfo(); |
| 23 | + /// |
| 24 | + //send characters to 2-line display: (you could get rid of all of this if you do not have a 2-line display) |
| 25 | + //Display Name, Units, and Page |
| 26 | + mySerial.write(254); // cursor to beginning of first line |
| 27 | + mySerial.write(128); |
| 28 | + mySerial.print(" "); // clear display for next loop |
| 29 | + mySerial.print(" "); |
| 30 | + mySerial.write(254); // cursor to beginning of first line (position 128) |
| 31 | + mySerial.write(128); |
| 32 | + mySerial.print(Vernier.sensorName()); // display name on first line |
| 33 | + mySerial.write(254); // cursor to beginning of second line |
| 34 | + mySerial.write(192); |
| 35 | + mySerial.print(Vernier.sensorUnits()); // display units on second line |
| 36 | + mySerial.write(254);// cursor to the end of second line |
| 37 | + mySerial.write(204); |
| 38 | + mySerial.print("p=");// display page at end of second line (-1 if resistor ID) |
| 39 | + mySerial.print(Vernier.page()); |
| 40 | + delay(2000); // short delay |
| 41 | + mySerial.print(" "); // clear display for what is next |
| 42 | + mySerial.print(" "); |
| 43 | + //Special section to display equation type, slope, intercept, and c, if button is pressed: |
| 44 | + buttonState = digitalRead(buttonPin); |
| 45 | + // check if the pushbutton is pressed. |
| 46 | + // if it is, the buttonState is LOW: |
| 47 | + if (buttonState == LOW) |
| 48 | + { |
| 49 | + //Display SensorNumber, cal equation type, and a (intercept) |
| 50 | + mySerial.write(254); // cursor to beginning of first line |
| 51 | + mySerial.write(128); |
| 52 | + mySerial.print("#"); |
| 53 | + mySerial.print(Vernier.sensorNumber()); // Sensor ID number |
| 54 | + mySerial.write(254);// cursor to middle of the first line |
| 55 | + mySerial.write(133); // cursor to position 6 of first line |
| 56 | + mySerial.print(" cal eq="); // // display cal eq type |
| 57 | + mySerial.print(Vernier.calEquationType()); // |
| 58 | + // display page at end of second line |
| 59 | + mySerial.write(254); // cursor to beginning of 2nd line |
| 60 | + mySerial.write(192); |
| 61 | + mySerial.print("a(int)="); |
| 62 | + mySerial.print(Vernier.intercept()); |
| 63 | + delay (2000); |
| 64 | + //Display b(slope), and c (cfactor): |
| 65 | + mySerial.print(" "); // clear display for next loop |
| 66 | + mySerial.print(" "); |
| 67 | + mySerial.write(254); // cursor to beginning of first line |
| 68 | + mySerial.write(128); |
| 69 | + mySerial.print("b(sl)="); // one space |
| 70 | + mySerial.print(Vernier.slope()); // |
| 71 | + mySerial.write(254); // cursor to beginning of second line |
| 72 | + mySerial.write(192); |
| 73 | + mySerial.print("c="); |
| 74 | + mySerial.print(Vernier.cFactor()); // |
| 75 | + delay (2000); |
| 76 | + //Display shortName and AutoID voltage: |
| 77 | + mySerial.write(254); // cursor to beginning of first line (position 128) |
| 78 | + mySerial.write(128); |
| 79 | + mySerial.print(" "); // clear display for next loop |
| 80 | + mySerial.print(" "); |
| 81 | + mySerial.write(254); // cursor to beginning of first line (position 128) |
| 82 | + mySerial.write(128); |
| 83 | + mySerial.print(Vernier.shortName()); // display name on first line |
| 84 | + mySerial.write(254); // cursor to near the beginning of first line |
| 85 | + mySerial.write(192); |
| 86 | + mySerial.print("ID volts="); |
| 87 | + mySerial.print(Vernier.voltageID()); // print out the raw voltage from the sensor |
| 88 | + delay (2000); |
| 89 | + }// end of special display done if button is down |
| 90 | + //set up screen for displaying data: |
| 91 | + mySerial.write(254); // cursor to beginning of first line |
| 92 | + mySerial.write(128); |
| 93 | + mySerial.print(" "); // clear display for next loop |
| 94 | + mySerial.print(" "); |
| 95 | + mySerial.write(254); // cursor to beginning of first line (position 128) |
| 96 | + mySerial.write(128); |
| 97 | + mySerial.print(Vernier.sensorName()); // display name on first line |
| 98 | + mySerial.write(254); // cursor to middle of the second line |
| 99 | + mySerial.write(200); |
| 100 | + mySerial.print(Vernier.sensorUnits()); // display units on second line |
| 101 | +} |
| 102 | + |
| 103 | +void loop() |
| 104 | +{ |
| 105 | + sensorReading =Vernier.readSensor(); |
| 106 | + Serial.print(sensorReading); |
| 107 | + Serial.print(" "); |
| 108 | + Serial.println(Vernier.sensorUnits()); |
| 109 | + mySerial.write(254); // command character |
| 110 | + mySerial.write(192); // move to line 2, position 0, |
| 111 | + Serial.println(sensorReading); // display Sensor Reading on monitor |
| 112 | + mySerial.print(sensorReading); |
| 113 | + mySerial.write(254); // command character |
| 114 | + mySerial.write(200); // move to line 2, position 7 |
| 115 | + mySerial.print(Vernier.sensorUnits()); // print out sensor units |
| 116 | + delay(1000);// a second |
| 117 | + mySerial.write(254); // cursor to beginning of first line |
| 118 | + mySerial.write(192); |
| 119 | + mySerial.print(" "); // clear 2nd line of display for next loop |
| 120 | +} |
| 121 | + void printSensorInfo() |
| 122 | + { |
| 123 | + // print out information about the sensor found: |
| 124 | + Serial.println("Sensor Information"); |
| 125 | + Serial.print("Sensor ID number: "); |
| 126 | + Serial.print("\t"); |
| 127 | + Serial.println(Vernier.sensorNumber()); |
| 128 | + Serial.print("Sensor Name: "); |
| 129 | + Serial.print("\t"); |
| 130 | + Serial.println(Vernier.sensorName()); |
| 131 | + Serial.print("Short Name: "); |
| 132 | + Serial.print("\t"); |
| 133 | + Serial.println(Vernier.shortName()); |
| 134 | + Serial.print("Units: "); |
| 135 | + Serial.print("\t"); |
| 136 | + Serial.println(Vernier.sensorUnits()); |
| 137 | + Serial.print("ID voltage level: "); |
| 138 | + Serial.print("\t"); |
| 139 | + Serial.println(Vernier.voltageID()); |
| 140 | + Serial.print("Page: "); |
| 141 | + Serial.print("\t"); |
| 142 | + Serial.println(Vernier.page()); |
| 143 | + Serial.print("slope: "); |
| 144 | + Serial.print("\t"); |
| 145 | + Serial.println(Vernier.slope()); |
| 146 | + Serial.print("intercept: "); |
| 147 | + Serial.print("\t"); |
| 148 | + Serial.println(Vernier.intercept()); |
| 149 | + Serial.print("cFactor:"); |
| 150 | + Serial.print("\t"); |
| 151 | + Serial.println(Vernier.cFactor()); |
| 152 | + Serial.print("calEquationType: "); |
| 153 | + Serial.print("\t"); |
| 154 | + Serial.println(Vernier.calEquationType()); |
| 155 | + } |
| 156 | + |
0 commit comments