update hcsr04.cpp by changing 20000 to 2000

Dependencies:   Stepper mbed SDFileSystem Ultrasonic PinDetect millis

Committer:
rschimpf78
Date:
Sun Mar 18 02:40:25 2018 +0000
Revision:
0:88834eed3de0
Child:
2:e8f8095464cf
done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rschimpf78 0:88834eed3de0 1 /*#include "mbed.h"
rschimpf78 0:88834eed3de0 2 #include <vector>
rschimpf78 0:88834eed3de0 3 //--------Brushed Motor -------------------
rschimpf78 0:88834eed3de0 4 DigitalOut in3(PTC17);
rschimpf78 0:88834eed3de0 5 DigitalOut in4(PTC16);
rschimpf78 0:88834eed3de0 6 //---------Stepper Motor ------------------
rschimpf78 0:88834eed3de0 7 Stepper mot(PTA1,D2);//d2 and d3
rschimpf78 0:88834eed3de0 8 DigitalOut en(D4);
rschimpf78 0:88834eed3de0 9 Serial pc(USBTX, USBRX);
rschimpf78 0:88834eed3de0 10
rschimpf78 0:88834eed3de0 11 //--Internal pullup resistors for Bumper Switches---
rschimpf78 0:88834eed3de0 12 DigitalIn bswitch(D1);
rschimpf78 0:88834eed3de0 13 DigitalIn tswitch(D0);
rschimpf78 0:88834eed3de0 14 int main() {
rschimpf78 0:88834eed3de0 15 vector<int> time;
rschimpf78 0:88834eed3de0 16 vector<int> plates;
rschimpf78 0:88834eed3de0 17 int n,t,h,m,s = 0;
rschimpf78 0:88834eed3de0 18 int tests = 0;
rschimpf78 0:88834eed3de0 19 PULp.period(0.0006f);
rschimpf78 0:88834eed3de0 20 ENp = 1;
rschimpf78 0:88834eed3de0 21 DIRp = 0;
rschimpf78 0:88834eed3de0 22 PULp.write(0.5f);
rschimpf78 0:88834eed3de0 23 in3=0;
rschimpf78 0:88834eed3de0 24 in4=0;
rschimpf78 0:88834eed3de0 25 pc.printf("\nCommands ");
rschimpf78 0:88834eed3de0 26 pc.scanf("%d",&tests);
rschimpf78 0:88834eed3de0 27
rschimpf78 0:88834eed3de0 28 for (int i = 1; i<=tests;i++){
rschimpf78 0:88834eed3de0 29 pc.printf("\nEnter the Number of Plates for test %d: ",i);
rschimpf78 0:88834eed3de0 30 pc.scanf("%d",&n);
rschimpf78 0:88834eed3de0 31 pc.printf("\nEnter hours and minutes to test these plates\n\nHours: ");
rschimpf78 0:88834eed3de0 32 pc.scanf("%d",&h);
rschimpf78 0:88834eed3de0 33 pc.printf("Minutes: ");
rschimpf78 0:88834eed3de0 34 pc.scanf("%d",&m);
rschimpf78 0:88834eed3de0 35 pc.printf("Seconds: ");
rschimpf78 0:88834eed3de0 36 pc.scanf("%d",&s);
rschimpf78 0:88834eed3de0 37 t=(h*3600)+(m*60)+s;
rschimpf78 0:88834eed3de0 38 time.push_back(t);
rschimpf78 0:88834eed3de0 39 plates.push_back(n);
rschimpf78 0:88834eed3de0 40 }
rschimpf78 0:88834eed3de0 41 pc.printf("\nTesting in Progress...");
rschimpf78 0:88834eed3de0 42 for (unsigned y=0; y<time.size(); y++){
rschimpf78 0:88834eed3de0 43
rschimpf78 0:88834eed3de0 44 //--------Turn Stepper on for a certain amount of time based on the number of plates----
rschimpf78 0:88834eed3de0 45 in3=0; // stop actuator
rschimpf78 0:88834eed3de0 46 in4=0; // stop actuator
rschimpf78 0:88834eed3de0 47 ENp = 0;
rschimpf78 0:88834eed3de0 48 DIRp = 0;
rschimpf78 0:88834eed3de0 49 wait(1.9*(plates.at(y)-1));
rschimpf78 0:88834eed3de0 50 ENp = 1;
rschimpf78 0:88834eed3de0 51 wait(0.5);
rschimpf78 0:88834eed3de0 52 //-------Move Plates Under X ray Beam---------------------------------------------------
rschimpf78 0:88834eed3de0 53 in3=0;
rschimpf78 0:88834eed3de0 54 in4=1;
rschimpf78 0:88834eed3de0 55 wait(14); //reaches end after 10 seconds
rschimpf78 0:88834eed3de0 56 in3=0; // stop actuator
rschimpf78 0:88834eed3de0 57 in4=0; // stop actuator
rschimpf78 0:88834eed3de0 58 //-----Test the Plate for specified time ----------------------------------------------
rschimpf78 0:88834eed3de0 59 wait(time.at(y)); //Leave plates in beam path for the user inputted amount of time
rschimpf78 0:88834eed3de0 60 //-----Retract plates to original position---------------------------------------------
rschimpf78 0:88834eed3de0 61 in3=1;
rschimpf78 0:88834eed3de0 62 in4=0;
rschimpf78 0:88834eed3de0 63 wait(14);
rschimpf78 0:88834eed3de0 64 in3=0;
rschimpf78 0:88834eed3de0 65 in4=0;
rschimpf78 0:88834eed3de0 66 wait(0.5);
rschimpf78 0:88834eed3de0 67 //--------Return Stepper to base height with one plate selected------------------------
rschimpf78 0:88834eed3de0 68 ENp = 0;
rschimpf78 0:88834eed3de0 69 DIRp = 1;
rschimpf78 0:88834eed3de0 70 wait(2*(plates.at(y)-1));
rschimpf78 0:88834eed3de0 71 ENp = 1;
rschimpf78 0:88834eed3de0 72 //-------------------------------------------------------------------------------------
rschimpf78 0:88834eed3de0 73 pc.printf("\n\nTest number %d complete", y+1);
rschimpf78 0:88834eed3de0 74 pc.printf("\nNumber of Plates: %d", plates.at(y));
rschimpf78 0:88834eed3de0 75 pc.printf("\nLength of Test: %d seconds", time.at(y));
rschimpf78 0:88834eed3de0 76 }
rschimpf78 0:88834eed3de0 77 pc.printf("\n\nAll testing complete");
rschimpf78 0:88834eed3de0 78 }
rschimpf78 0:88834eed3de0 79 */
rschimpf78 0:88834eed3de0 80
rschimpf78 0:88834eed3de0 81 /*#include "mbed.h"
rschimpf78 0:88834eed3de0 82
rschimpf78 0:88834eed3de0 83 Serial pc(USBTX, USBRX);
rschimpf78 0:88834eed3de0 84
rschimpf78 0:88834eed3de0 85 int main() {
rschimpf78 0:88834eed3de0 86 char c;
rschimpf78 0:88834eed3de0 87 char buffer[128];
rschimpf78 0:88834eed3de0 88 pc.gets(buffer, 4);
rschimpf78 0:88834eed3de0 89 pc.printf("I got '%s'\n", buffer);
rschimpf78 0:88834eed3de0 90 if(strcmp(buffer,"on")==0) {
rschimpf78 0:88834eed3de0 91 printf("success");
rschimpf78 0:88834eed3de0 92 }
rschimpf78 0:88834eed3de0 93 else{
rschimpf78 0:88834eed3de0 94 printf("fail");
rschimpf78 0:88834eed3de0 95 }
rschimpf78 0:88834eed3de0 96 }
rschimpf78 0:88834eed3de0 97 */
rschimpf78 0:88834eed3de0 98 #include "mbed.h"
rschimpf78 0:88834eed3de0 99 #include "Stepper.h"
rschimpf78 0:88834eed3de0 100 DigitalIn homeSwitch(D8);
rschimpf78 0:88834eed3de0 101 DigitalIn endSwitch(D1);
rschimpf78 0:88834eed3de0 102 DigitalIn bottomSwitch(D0);
rschimpf78 0:88834eed3de0 103 DigitalIn topSwitch(D9);
rschimpf78 0:88834eed3de0 104 Stepper mot(PTA1,D2);
rschimpf78 0:88834eed3de0 105 DigitalOut en(D4);
rschimpf78 0:88834eed3de0 106 DigitalOut in1(D11);
rschimpf78 0:88834eed3de0 107 DigitalOut in2(D10);
rschimpf78 0:88834eed3de0 108 Serial pc(USBTX, USBRX);
rschimpf78 0:88834eed3de0 109
rschimpf78 0:88834eed3de0 110 void positionStatus()
rschimpf78 0:88834eed3de0 111 {
rschimpf78 0:88834eed3de0 112 int plateposition; //plate position -> 1 is home, 2 is traveling, 3 is under beam path
rschimpf78 0:88834eed3de0 113 if(homeSwitch==0 && endSwitch==1)
rschimpf78 0:88834eed3de0 114 {
rschimpf78 0:88834eed3de0 115 plateposition = 1;
rschimpf78 0:88834eed3de0 116 pc.printf("\nPlate position is home %d",plateposition);
rschimpf78 0:88834eed3de0 117 }
rschimpf78 0:88834eed3de0 118 else if (homeSwitch==1 && endSwitch==1)
rschimpf78 0:88834eed3de0 119 {
rschimpf78 0:88834eed3de0 120 plateposition = 2;
rschimpf78 0:88834eed3de0 121 pc.printf("\nPlates are currently moving %d",plateposition);
rschimpf78 0:88834eed3de0 122 }
rschimpf78 0:88834eed3de0 123 else if(homeSwitch==1 && endSwitch==0)
rschimpf78 0:88834eed3de0 124 {
rschimpf78 0:88834eed3de0 125 plateposition = 3;
rschimpf78 0:88834eed3de0 126 pc.printf("\nPlates are under beam path %d",plateposition);
rschimpf78 0:88834eed3de0 127 }
rschimpf78 0:88834eed3de0 128 else
rschimpf78 0:88834eed3de0 129 {
rschimpf78 0:88834eed3de0 130 pc.printf("\nError, cannot locate plate position");
rschimpf78 0:88834eed3de0 131 }
rschimpf78 0:88834eed3de0 132 }
rschimpf78 0:88834eed3de0 133
rschimpf78 0:88834eed3de0 134 void retractPMMA()
rschimpf78 0:88834eed3de0 135 {
rschimpf78 0:88834eed3de0 136 in1=1;
rschimpf78 0:88834eed3de0 137 in2=0;
rschimpf78 0:88834eed3de0 138 wait(14);
rschimpf78 0:88834eed3de0 139 in1=0;
rschimpf78 0:88834eed3de0 140 in2=0;
rschimpf78 0:88834eed3de0 141 positionStatus();
rschimpf78 0:88834eed3de0 142 }
rschimpf78 0:88834eed3de0 143
rschimpf78 0:88834eed3de0 144 void sendPMMA() //Command: Send plates
rschimpf78 0:88834eed3de0 145 { //Returns: plates arrived(),plates didn't arrive()
rschimpf78 0:88834eed3de0 146 wait(2);
rschimpf78 0:88834eed3de0 147 in1=1;
rschimpf78 0:88834eed3de0 148 in2=0;
rschimpf78 0:88834eed3de0 149 wait(14);
rschimpf78 0:88834eed3de0 150 in1=0;
rschimpf78 0:88834eed3de0 151 in2=0;
rschimpf78 0:88834eed3de0 152 positionStatus();
rschimpf78 0:88834eed3de0 153 }
rschimpf78 0:88834eed3de0 154
rschimpf78 0:88834eed3de0 155 void autoCalibrate() //if machine is plugged in when the plate selector isn't at the origin...
rschimpf78 0:88834eed3de0 156 {
rschimpf78 0:88834eed3de0 157 en = 0; //...it automatically lowers it to the origin position
rschimpf78 0:88834eed3de0 158 mot.setSpeed(800);
rschimpf78 0:88834eed3de0 159 mot.rotate(0);
rschimpf78 0:88834eed3de0 160 while(bottomSwitch); //While bottom switch is unpressed
rschimpf78 0:88834eed3de0 161 mot.stop(); //Stop rotation when switch is pressed (pressed = logical level low)
rschimpf78 0:88834eed3de0 162 en=1; //Disable driver
rschimpf78 0:88834eed3de0 163 mot.setPositionZero(); //Set absolute origin for plate selector at switch hit
rschimpf78 0:88834eed3de0 164 }
rschimpf78 0:88834eed3de0 165
rschimpf78 0:88834eed3de0 166 void internalpullups()
rschimpf78 0:88834eed3de0 167 {
rschimpf78 0:88834eed3de0 168 bottomSwitch.mode (PullUp);
rschimpf78 0:88834eed3de0 169 topSwitch.mode (PullUp);
rschimpf78 0:88834eed3de0 170 homeSwitch.mode (PullUp);
rschimpf78 0:88834eed3de0 171 endSwitch.mode (PullUp);
rschimpf78 0:88834eed3de0 172 wait (.01);
rschimpf78 0:88834eed3de0 173 }
rschimpf78 0:88834eed3de0 174
rschimpf78 0:88834eed3de0 175 void numberofPlates() // Command: Number of PMMA to send (plates)
rschimpf78 0:88834eed3de0 176 // Returns: Actual number of plates selected (selectedplates), selectionerror (1 for command error, 0 for correct command)
rschimpf78 0:88834eed3de0 177 {
rschimpf78 0:88834eed3de0 178 int stepheight,plates,selectionerror;
rschimpf78 0:88834eed3de0 179 double selectedplates;
rschimpf78 0:88834eed3de0 180 pc.printf("\nEnter the Number of Plates to send ");
rschimpf78 0:88834eed3de0 181 pc.scanf("%d",&plates);
rschimpf78 0:88834eed3de0 182 if (plates <= 0)
rschimpf78 0:88834eed3de0 183 {
rschimpf78 0:88834eed3de0 184 pc.printf("\nNot a valid number, must be 1 to 16");
rschimpf78 0:88834eed3de0 185 selectionerror = 1;
rschimpf78 0:88834eed3de0 186 }
rschimpf78 0:88834eed3de0 187 else if (plates >= 17)
rschimpf78 0:88834eed3de0 188 {
rschimpf78 0:88834eed3de0 189 pc.printf("\nNot a valid number, must be 1 to 16");
rschimpf78 0:88834eed3de0 190 selectionerror = 1;
rschimpf78 0:88834eed3de0 191 }
rschimpf78 0:88834eed3de0 192 else
rschimpf78 0:88834eed3de0 193 {
rschimpf78 0:88834eed3de0 194 selectionerror = 0;
rschimpf78 0:88834eed3de0 195 pc.printf("\nCommand Valid\n");
rschimpf78 0:88834eed3de0 196 en = 0;
rschimpf78 0:88834eed3de0 197 mot.goesTo(plates*1620);
rschimpf78 0:88834eed3de0 198 while(!mot.stopped());
rschimpf78 0:88834eed3de0 199 stepheight = mot.getPosition();
rschimpf78 0:88834eed3de0 200 selectedplates = stepheight/1620;
rschimpf78 0:88834eed3de0 201 pc.printf("\n Steps taken: %d ",selectedplates);
rschimpf78 0:88834eed3de0 202 mot.stop();
rschimpf78 0:88834eed3de0 203 en = 1;
rschimpf78 0:88834eed3de0 204 }
rschimpf78 0:88834eed3de0 205 }
rschimpf78 0:88834eed3de0 206 void selectionStatus()
rschimpf78 0:88834eed3de0 207 {
rschimpf78 0:88834eed3de0 208 int stepheight;
rschimpf78 0:88834eed3de0 209 double selectedplates;
rschimpf78 0:88834eed3de0 210 stepheight = mot.getPosition();
rschimpf78 0:88834eed3de0 211 selectedplates = stepheight/1620;
rschimpf78 0:88834eed3de0 212 pc.printf("\n Number of plates currently selected: %d ",selectedplates);
rschimpf78 0:88834eed3de0 213
rschimpf78 0:88834eed3de0 214 }
rschimpf78 0:88834eed3de0 215
rschimpf78 0:88834eed3de0 216
rschimpf78 0:88834eed3de0 217 int main()
rschimpf78 0:88834eed3de0 218 {
rschimpf78 0:88834eed3de0 219 char command[256] = { 0 };
rschimpf78 0:88834eed3de0 220 internalpullups();
rschimpf78 0:88834eed3de0 221 wait (.01);
rschimpf78 0:88834eed3de0 222 autoCalibrate(); //...it automatically lowers it to the origin position for initial power up
rschimpf78 0:88834eed3de0 223
rschimpf78 0:88834eed3de0 224 while(1)
rschimpf78 0:88834eed3de0 225 {
rschimpf78 0:88834eed3de0 226 pc.printf("\nEnter Command: \n send \n retract \n position (get status) \n selection (get status)" );
rschimpf78 0:88834eed3de0 227 pc.scanf("\n%s", &command);
rschimpf78 0:88834eed3de0 228 if (strcmp (command, "send") == 0)
rschimpf78 0:88834eed3de0 229 {
rschimpf78 0:88834eed3de0 230 pc.printf ("\nCommand Recieved: Send"); //confirmation of recieved command
rschimpf78 0:88834eed3de0 231 numberofPlates(); //select number of plates to send
rschimpf78 0:88834eed3de0 232 sendPMMA();
rschimpf78 0:88834eed3de0 233 }
rschimpf78 0:88834eed3de0 234 if (strcmp (command, "retract") == 0)
rschimpf78 0:88834eed3de0 235 {
rschimpf78 0:88834eed3de0 236 pc.printf ("\nCommand Recieved: retract");
rschimpf78 0:88834eed3de0 237 retractPMMA();
rschimpf78 0:88834eed3de0 238 }
rschimpf78 0:88834eed3de0 239 if (strcmp (command, "position") == 0)
rschimpf78 0:88834eed3de0 240 {
rschimpf78 0:88834eed3de0 241 pc.printf ("\nCommand Recieved: position");
rschimpf78 0:88834eed3de0 242 positionStatus();
rschimpf78 0:88834eed3de0 243 }
rschimpf78 0:88834eed3de0 244 if (strcmp (command, "selection") == 0)
rschimpf78 0:88834eed3de0 245 {
rschimpf78 0:88834eed3de0 246 pc.printf ("\nCommand Recieved: selection");
rschimpf78 0:88834eed3de0 247 selectionStatus();
rschimpf78 0:88834eed3de0 248 }
rschimpf78 0:88834eed3de0 249 }
rschimpf78 0:88834eed3de0 250 }
rschimpf78 0:88834eed3de0 251
rschimpf78 0:88834eed3de0 252
rschimpf78 0:88834eed3de0 253 /* int main ()
rschimpf78 0:88834eed3de0 254 {
rschimpf78 0:88834eed3de0 255 int steps, plates;
rschimpf78 0:88834eed3de0 256 bswitch.mode (PullUp);
rschimpf78 0:88834eed3de0 257 wait (.01);
rschimpf78 0:88834eed3de0 258 mot.setSpeed (600);
rschimpf78 0:88834eed3de0 259 mot.rotate (0); //if machine is plugged in when the plate selector isn't at the origin...
rschimpf78 0:88834eed3de0 260
rschimpf78 0:88834eed3de0 261 while (bswitch); //...it automatically lowers it to the origin positio
rschimpf78 0:88834eed3de0 262 mot.stop (); //Stop rotation when switch is pressed (pressed = logical level low)
rschimpf78 0:88834eed3de0 263 en = 1; //Disable driver
rschimpf78 0:88834eed3de0 264 mot.setPositionZero (); //Set absolute origin for plate selector at switch hit
rschimpf78 0:88834eed3de0 265 char c[256] = { 0 };
rschimpf78 0:88834eed3de0 266
rschimpf78 0:88834eed3de0 267 while (1)
rschimpf78 0:88834eed3de0 268 {
rschimpf78 0:88834eed3de0 269 pc.printf("Choose a command:\nPMMA 1");
rschimpf78 0:88834eed3de0 270 pc.scanf ("%s", &c);
rschimpf78 0:88834eed3de0 271
rschimpf78 0:88834eed3de0 272 if (strcmp (c, "PMMA1") == 0)
rschimpf78 0:88834eed3de0 273 {
rschimpf78 0:88834eed3de0 274 pc.printf ("\n match ");
rschimpf78 0:88834eed3de0 275 }
rschimpf78 0:88834eed3de0 276
rschimpf78 0:88834eed3de0 277 if (strcmp (c, "no") == 0)
rschimpf78 0:88834eed3de0 278 {
rschimpf78 0:88834eed3de0 279 pc.printf ("\n not match ");
rschimpf78 0:88834eed3de0 280 }
rschimpf78 0:88834eed3de0 281 }
rschimpf78 0:88834eed3de0 282 }
rschimpf78 0:88834eed3de0 283 */