Advertisement
Guest User

StateMachine2

a guest
Apr 26th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include <Keypad.h>
  2. #include <SoftwareSerial.h>
  3. #include <Adafruit_RGBLCDShield.h>
  4. #include <Wire.h>
  5. #include <utility/Adafruit_MCP23017.h>
  6.  
  7. Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
  8.  
  9. //Parallax RFID Reader
  10. #define RFIDEnablePin 2 //Pin that enables reading. Set as OUTPUT and LOW to read an RFID tag
  11. #define RFIDSerialRate 2400 //Parallax RFID Reader Serial Port Speed
  12. #define SLEEP 200 //Default sleep duration for RF reader
  13.  
  14. //Using SoftwareSerial Library to locate the serial pins off the default set
  15. //This allows the Arduino to be updated via USB with no conflict
  16. #define RxPin 5 //Pin to read data from Reader
  17. #define TxPin 4 //Pin to write data to the Reader NOTE: The reader doesn't get written to, don't connect this line.
  18. SoftwareSerial RFIDReader(RxPin, TxPin);
  19.  
  20. #define Num_Chips 7 //Numer of RFID chips in array
  21. #define ROWS 4 // Four rows
  22. #define COLS 4 // Four columns
  23. #define RFID_CODE 10 //size of RFID tag
  24.  
  25. #define PWM 3 //Pin for the motor
  26.  
  27.  
  28. //If the RFID reader is currently reading
  29.  
  30. //The state of the machine, either WAIT waiting for a button press or 1 while looking for an RFID tag
  31. int state;
  32.  
  33. //The RFID tag currently being searched for
  34. String target = "";
  35. String lastTag = "";
  36.  
  37. // Define the Keymap
  38. char keys[ROWS][COLS] =
  39. {
  40. {'1', '2', '3', 'A'},
  41. {'4', '5', '6', NO_KEY},
  42. {'7', NO_KEY, NO_KEY, NO_KEY},
  43. {NO_KEY, NO_KEY, NO_KEY, 'x'}
  44. };
  45.  
  46. // Define Array of Strings for RFID tags
  47. String chips[Num_Chips] = {"0000958C92", "0000934267", "0000955FD0", "0000928EB5", "0000934425", "000093242A", "000093815C"};
  48.  
  49.  
  50. //Variables used in RFID scanning
  51. int bytesRead, val;
  52.  
  53. //Storage for RFID codes during scanning
  54. char code[RFID_CODE];
  55.  
  56. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  57. byte rowPins[ROWS] = { 9, 8, 7, 6 };
  58.  
  59. // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  60. byte colPins[COLS] = {13, 12, 11, 10 };
  61.  
  62. // Create the Keypad
  63. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  64.  
  65.  
  66. //Starts the motor forward and ramps the speed up to full
  67. void startMotor()
  68. {
  69. digitalWrite (PWM, HIGH);
  70. Serial.println("Starting motor RPM ramp-up");
  71. //Slowly ramp the motor up from WAIT to full speed
  72. for (int motorValue = 0 ; motorValue <= 255; motorValue += 5)
  73. {
  74. analogWrite(PWM, motorValue);
  75. delay(30);
  76. }
  77. }
  78.  
  79. //Slows the motor and eventually disables it
  80. void stopMotor()
  81. {
  82. //Slowly ramp the motor down from full speed to 0
  83. Serial.println("Starting motor ramp-down");
  84. for (int motorValue = 255 ; motorValue >= 0; motorValue -= 5)
  85. {
  86. analogWrite(PWM, motorValue);
  87. delay(30);
  88. }
  89. Serial.println("Motor disabled");
  90. digitalWrite (PWM, LOW);
  91. }
  92.  
  93. void resetLCD()
  94. {
  95. lcd.print("Select Box #");
  96. }
  97.  
  98. //Reads data from the RFID reader and if it is a valid tag assigns it to a string
  99. void ReadSerial(String &ReadTagString)
  100. {
  101. int bytesread = 0;
  102. int val = 0;
  103. char code[10];
  104. String TagCode = "";
  105.  
  106. if (RFIDReader.available() > 0) { // If data available from reader
  107. if ((val = RFIDReader.read()) == 10) { // Check for header
  108. bytesread = 0;
  109. while (bytesread < 10) { // Read 10 digit code
  110. if ( RFIDReader.available() > 0) {
  111. val = RFIDReader.read();
  112. if ((val == 10) || (val == 13)) { // If header or stop bytes before the 10 digit reading
  113. break; // Stop reading
  114. }
  115. code[bytesread] = val; // Add the digit
  116. bytesread++; // Ready to read next digit
  117. }
  118. }
  119. if (bytesread == 10) { // If 10 digit read is complete
  120.  
  121. for (int x = 0; x < 10; x++) //Copy the Chars to a String
  122. {
  123. TagCode += code[x];
  124. }
  125. ReadTagString = TagCode; //Update the caller
  126. while (RFIDReader.available() > 0) //Burn off any characters still in the buffer
  127. {
  128. RFIDReader.read();
  129. }
  130.  
  131. }
  132. bytesread = 0;
  133. TagCode = "";
  134. }
  135. }
  136. }
  137.  
  138. //This method checks to see if the RFID reader has found the desired tag, if not it continues to run until it does
  139. void scanRFID()
  140. {
  141. String tag;
  142. if (RFIDReader.available() > 0)
  143. {
  144. ReadSerial(tag);
  145.  
  146. if (tag != lastTag)
  147. {
  148. lastTag = tag;
  149. Serial.print("Reading tag " + tag);
  150. }
  151. if (tag == target)
  152. {
  153. Serial.println("Found the correct tag, disabling motor");
  154. resetLCD();
  155. stopMotor();
  156. state = WAIT;
  157. }
  158. }
  159. }
  160.  
  161.  
  162. void setup()
  163. {
  164. // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  165. RFIDReader.begin(RFIDSerialRate);
  166.  
  167. // Set Enable pin as OUTPUT to connect it to the RFID /ENABLE pin
  168. pinMode(RFIDEnablePin, OUTPUT);
  169.  
  170. // Activate the RFID reader
  171. // Setting the RFIDEnablePin HIGH will deactivate the reader
  172. // which could be usefull if you wanted to save battery life for
  173. // example.
  174. digitalWrite(RFIDEnablePin, LOW);
  175. Serial.begin(9600); // set up Serial library at 9600 bps
  176.  
  177. digitalWrite(PWM, LOW); //Set the motor to off
  178.  
  179. lcd.begin(16, 2);
  180. resetLCD();
  181.  
  182. state = WAIT;
  183. }
  184.  
  185. void loop()
  186. {
  187. char key = kpd.getKey();
  188. Serial.print("State is currently : " + state);
  189. if (key != NO_KEY) //If the key is a valid key
  190. {
  191. if (key == 'x' && state == 1) //If killswitch key is pressed disbale motor and set state to WAIT
  192. {
  193. Serial.println("Killswitch pressed, deactivating motor");
  194. resetLCD();
  195. stopMotor();
  196. state = WAIT;
  197. }
  198. else//If numerical key is pressed
  199. {
  200. if (key != 'x')
  201. {
  202. String message = "Box ";
  203. message += key;
  204. message += " Selected";
  205.  
  206. lcd.print(message);
  207. Serial.println("Setting target to " + chips[(key - '0') - 1]);
  208. target = chips[(key - '0') - 1]; //Get the ID string for the toolbox associated with the key pressed
  209.  
  210. if (state != 1) //Set state to 1ing and enable motor if not already on
  211. {
  212. Serial.println("Starting search for " + target);
  213. state = 1;
  214. startMotor();
  215. }
  216. }
  217. }
  218. }
  219.  
  220. if (state == 1)
  221. {
  222. scanRFID(); //Reads from the RFID reader and checks if the correct tag has been found
  223. }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement