Advertisement
Guest User

martin_cork

a guest
Jun 9th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 KB | None | 0 0
  1. /*
  2.  
  3. ROTARY ENCODER:
  4. On the side of the rotary encoder with 3 legs, theres
  5. a green and a black mark;
  6. !!! 2 AND 3 are special Pins !!!
  7. Green Mark <---> Arduino Digital Pin 2
  8. Black Mark <---> Arduino Digital Pin 3
  9. middle <---> Arduino Ground
  10.  
  11. RFID CARD:
  12. Needs to be powered off the Arduino
  13. RFID ENABLE <---> Arduino Digital Pin 10
  14. RFID SOUT <---> Arduino Digital Pin 11
  15.  
  16. SERVO
  17. Need to be powered externally
  18. Run a ground between the Arduino and the ground for the servo
  19.  
  20. PRINTER
  21. GREEN WIRE <----> Arduino Digital Pin 5
  22. YELLOW WIRE <----> Arduino Digital Pin 6
  23.  
  24. */
  25.  
  26. #include <SoftwareSerial.h>
  27. #include <Servo.h>
  28.  
  29.  
  30.  
  31. //PRINTER
  32. #include "SoftwareSerial.h"
  33. #include "Adafruit_Thermal.h"
  34. //add the irish times QR Code
  35. #include "story1sml.h"
  36. #include "story2.h"
  37. #include "story4.h"
  38. #include "story5.h"
  39. #include "story6.h"
  40.  
  41. #include <avr/pgmspace.h>
  42. int printer_RX_Pin = 5; // This is the green wire
  43. int printer_TX_Pin = 6; // This is the yellow wire
  44. Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);
  45.  
  46. //Parallax RFID Reader
  47. //can be set to either 2 or 4
  48. #define RFIDEnablePin 10 //Pin that enables reading. Set as OUTPUT and LOW to read an RFID tag
  49. #define RFIDSerialRate 2400 //Parallax RFID Reader Serial Port Speed
  50.  
  51. //Using SoftwareSerial Library to locate the serial pins off the default set
  52. //This allows the Arduino to be updated via USB with no conflict
  53. #define RxPin 11 //Pin to read data from Reader
  54. #define TxPin 10 //Pin to write data to the Reader NOTE: The reader doesn't get written to, don't connect this line.
  55. SoftwareSerial RFIDReader(RxPin,TxPin);
  56.  
  57. String RFIDTAG=""; //Holds the RFID Code read from a tag
  58. String DisplayTAG = ""; //Holds the last displayed RFID Tag
  59.  
  60. int firstSensor = 0;
  61. int secondSensor = 0;
  62. int thirdSensor = 0;
  63. int trackNum;
  64. int inByte = 0;
  65.  
  66. char val;
  67.  
  68. int encoderPin1 = 2;
  69. int encoderPin2 = 3;
  70.  
  71. volatile int lastEncoded = 0;
  72. volatile long encoderValue = 0;
  73.  
  74. long lastencoderValue = 0;
  75.  
  76. int lastMSB = 0;
  77. int lastLSB = 0;
  78.  
  79. //servo motors
  80. //attached to 8 & 9
  81. //slotServo is for the slot, gateServo is to hold the card
  82. Servo gateServo, slotServo, handleServo;
  83. int slotServoPos = 0;
  84.  
  85. //SLOT LIGHT
  86. int slotLight = 18;
  87.  
  88. //Printer text
  89. //strings should be short
  90. String story1 = "Feel free to visit neighbours, but don't be surprised if they don't trust you!";
  91. String story2 = "Watch out for evil neighbours hoping to steal your butter.";
  92. String story3 = "Story 3";
  93. String story4 = "Maybe superstitions didn't really exist at all?";
  94. String story5 = "Stealing butter was evil, but who would do such a thing?";
  95. String story6 = "But why did people believe in superstitions?";
  96. String currentStory;
  97.  
  98. /*
  99. String story1_pt2 = "keep churning";
  100. String story2_pt2 = "keep churning";
  101. String story4_pt2 = "keep churning";
  102. String story5_pt2 = "keep churning";
  103. String story6_pt2 = "keep churning";
  104. String currentStory_pt2;
  105. */
  106.  
  107. String story1_link = "http://goo.gl/q0tqE9";
  108. String story2_link = "http://goo.gl/ChbeOq";
  109. String story4_link = "http://goo.gl/5eT1Wz";
  110. String story5_link = "http://goo.gl/PPNj57";
  111. String story6_link = "http://goo.gl/pdGA7u";
  112. String pdf_link;
  113. //Printer QR Code
  114. String currentQR;
  115.  
  116. int LED_13 = 13;
  117. void setup()
  118. {
  119. //
  120. // RFID CARD READER CODE
  121. //
  122. // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  123. RFIDReader.begin(RFIDSerialRate);
  124.  
  125. // Set Enable pin as OUTPUT to connect it to the RFID /ENABLE pin
  126. pinMode(RFIDEnablePin,OUTPUT);
  127.  
  128. // Activate the RFID reader
  129. // Setting the RFIDEnablePin HIGH will deactivate the reader
  130. // which could be usefull if you wanted to save battery life for
  131. // example.
  132. digitalWrite(RFIDEnablePin, LOW);
  133.  
  134. Serial.begin(9600); // set up Serial library at 9600 bps
  135.  
  136. Serial.println("Hello world --!"); // prints hello with ending line break
  137.  
  138. ///-------------------------------\\\\\
  139.  
  140. //
  141. // ROTARY ENCODER CODE
  142. //
  143. pinMode(encoderPin1, INPUT);
  144. pinMode(encoderPin2, INPUT);
  145.  
  146. digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  147. digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
  148.  
  149. //call updateEncoder() when any high/low changed seen
  150. //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  151. attachInterrupt(0, updateEncoder, CHANGE);
  152. attachInterrupt(1, updateEncoder, CHANGE);
  153.  
  154. //SERVO CODE
  155. handleServo.attach(7);
  156. gateServo.attach(8);
  157. slotServo.attach(9);
  158. gateServo.write(0);//bottom servo, 0 == closed, 90 == open
  159. slotServo.write(60);//top servo, 0 == closed, 60 == open
  160. handleServo.write(90);
  161. pinMode(12, OUTPUT);
  162.  
  163. pinMode(LED_13, OUTPUT);
  164. //SLOT LIGHT
  165. pinMode(slotLight, OUTPUT);
  166.  
  167. //establish contact with Processing
  168. establishContact();
  169. }
  170.  
  171. void loop()
  172. {
  173. if(RFIDReader.available() > 0) // If data available from reader
  174. {
  175. gateServo.detach();
  176. slotServo.detach();
  177. handleServo.detach();
  178. ReadSerial(RFIDTAG); //Read the tag number from the reader. Should return a 10 digit serial number
  179. digitalWrite(RFIDEnablePin, HIGH);
  180. //attatch servo and ease it close slot
  181. slotServo.attach(9);
  182. for(slotServoPos = 60; slotServoPos >= 0; slotServoPos -= 1){
  183. //slotServo.write(0);
  184. slotServo.write(slotServoPos);
  185. delay(20);
  186. }
  187. thirdSensor = 1;
  188. delay(2000);
  189. slotServo.detach();
  190. //digitalWrite(RFIDEnablePin, LOW);
  191. }
  192.  
  193. //This only displays a tag once, unless another tag is scanned
  194. if(DisplayTAG!=RFIDTAG)
  195. {
  196. gateServo.detach();
  197. slotServo.detach();
  198. DisplayTAG=RFIDTAG;
  199.  
  200. //Serial.println(RFIDTAG);
  201. }
  202.  
  203. if(RFIDTAG == "0415ED29AC"){
  204. trackNum = 1;
  205. currentStory = story1;
  206. //currentStory_pt2 = story1_pt2;
  207. pdf_link = story1_link;
  208. }
  209. if(RFIDTAG == "0415ED33DF"){
  210. trackNum = 2;
  211. currentStory = story2;
  212. //currentStory_pt2 = story2_pt2;
  213. pdf_link = story2_link;
  214. }
  215. if(RFIDTAG == "0415ED41D2"){
  216. trackNum = 3;
  217. currentStory = story3;
  218. }
  219. if(RFIDTAG == "0415ED4C69"){
  220. trackNum = 4;
  221. currentStory = story4;
  222. //currentStory_pt2 = story4_pt2;
  223. pdf_link = story4_link;
  224. }
  225. if(RFIDTAG == "0415ED50A4"){
  226. trackNum = 5;
  227. currentStory = story5;
  228. //currentStory_pt2 = story5_pt2;
  229. pdf_link = story5_link;
  230. }
  231. if(RFIDTAG == "0415ED4C54"){
  232. trackNum = 6;
  233. currentStory = story6;
  234. //currentStory_pt2 = story6_pt2;
  235. pdf_link = story6_link;
  236. }
  237. firstSensor = trackNum;
  238. Serial.write(firstSensor);
  239. Serial.write(encoderValue);
  240. Serial.write(thirdSensor);
  241. //secondSensor ++;
  242.  
  243. if(Serial.available()){
  244. val = Serial.read();
  245.  
  246. if(val == '0'){ //--if someone churned their way to the end of the track, eject and print--\\
  247. //someone has listened to the full track
  248. //so eject the token
  249. digitalWrite(12, HIGH);
  250.  
  251. //TURN THE RFID READER OFF
  252. digitalWrite(RFIDEnablePin, HIGH);
  253. gateServo.attach(8);
  254. slotServo.attach(9);
  255. gateServo.write(90); //open servo
  256. //slotServo.write(60);
  257. handleServo.attach(7);
  258. handleServo.write(90);
  259.  
  260. //slot is open, so don't play any track
  261. thirdSensor = 0;
  262. delay(3000); // wait 3 seconds
  263. gateServo.write(0); //close servo
  264. slotServo.write(60);
  265. delay(2000);
  266. //set murmur track to play
  267. //trackNum = 0;
  268. gateServo.detach();
  269. slotServo.detach();
  270.  
  271. digitalWrite(12, LOW);
  272. //TURN THE RFID READER ON AGAIN
  273. digitalWrite(RFIDEnablePin, LOW);
  274. }
  275. if(val == '1'){ //--if someone stopped churning mid way, eject the card--\\
  276. digitalWrite(12, LOW);
  277. //someone has listened to the full track
  278. //so eject the token and print a receipt
  279. digitalWrite(12, HIGH);
  280.  
  281. //TURN THE RFID READER OFF
  282. digitalWrite(RFIDEnablePin, HIGH);
  283. gateServo.attach(8);
  284. slotServo.attach(9);
  285. gateServo.write(90); //open servo
  286. //slotServo.write(60);
  287. handleServo.attach(7);
  288. handleServo.write(90);
  289. //slot is open, so don't play any track
  290. thirdSensor = 0;
  291. delay(3000); // wait 3 seconds
  292. gateServo.write(0); //close servo
  293. slotServo.write(60);
  294. delay(2000);
  295. //set murmur track to play
  296. //trackNum = 0;
  297. gateServo.detach();
  298. slotServo.detach();
  299.  
  300. digitalWrite(12, LOW);
  301. //TURN THE RFID READER ON AGAIN
  302. digitalWrite(RFIDEnablePin, LOW);
  303. }
  304. if(val == '2'){
  305. digitalWrite(LED_13, LOW);
  306. handleServo.attach(7);
  307. handleServo.write(90);
  308. }
  309. //IF SOMEONE IS CHURNING - ACTUATE HANDLEMOTOR
  310. if(val == '3'){
  311. digitalWrite(slotLight, LOW);
  312. handleServo.attach(7);
  313. handleServo.write(70);
  314. }
  315. //if someone isn't churning
  316. //code taken from http://labs.ideo.com/2014/03/20/vodafonepursecharger/
  317. if(val == '4'){
  318. static bool isLightOn = false; //-- initialize as static (i.e. persistent in this function)
  319. static unsigned long lastTimeRecorded = millis(); //-- history of time where we switched the lights
  320. unsigned long currentTime = millis(); //-- read the current time once
  321.  
  322. if ( currentTime - lastTimeRecorded > 1000 ) { //-- if it's been more than 1000ms
  323. if( isLightOn ) { //-- and if the light is on
  324. digitalWrite(slotLight, LOW); //-- turn it off
  325. isLightOn = false; //-- set our flag to false
  326. } else { //-- otherwise, we know the light is off
  327. digitalWrite(slotLight, HIGH); //-- so turn it on
  328. isLightOn = true; //-- set our flag to true
  329. }
  330. lastTimeRecorded = currentTime; //-- update the last time we switched
  331. }
  332. }
  333. if(val == '5'){
  334. digitalWrite(LED_13, HIGH);
  335.  
  336. printer.begin();
  337. //PRINT STORY
  338. printer.setSize('S');
  339. printer.println("Keep churning to the end!!");
  340. delay(3000);
  341. digitalWrite(LED_13, LOW);
  342.  
  343. digitalWrite(LED_13, HIGH);
  344. //printer.feed(1);
  345. Serial.write(firstSensor);
  346. Serial.write(encoderValue);
  347. Serial.write(thirdSensor);
  348.  
  349. printer.println(currentStory);
  350. delay(3000);
  351. Serial.write(firstSensor);
  352. Serial.write(encoderValue);
  353. Serial.write(thirdSensor);
  354. digitalWrite(LED_13, LOW);
  355. //PRINT QR
  356. if(trackNum == 1){
  357. //printer.printBitmap(story1sml_width, story1sml_height, story1sml_data);
  358. }
  359.  
  360. Serial.write(firstSensor);
  361. Serial.write(encoderValue);
  362. Serial.write(thirdSensor);
  363. digitalWrite(LED_13, HIGH);
  364.  
  365. //printer.feed(2);
  366. printer.sleep();
  367.  
  368. digitalWrite(LED_13, LOW);
  369. RFIDReader.begin(RFIDSerialRate);
  370. }
  371. if(val == '6'){
  372. //printer.wake();
  373. printer.begin();
  374. digitalWrite(LED_13, HIGH);
  375. //PRINT STORY
  376. printer.setSize('M');
  377. printer.println("Keep churning!");
  378. delay(3000);
  379. Serial.write(firstSensor);
  380. Serial.write(encoderValue);
  381. Serial.write(thirdSensor);
  382.  
  383. //printer.feed(3);
  384. digitalWrite(LED_13, LOW);
  385. printer.sleep();
  386.  
  387. RFIDReader.begin(RFIDSerialRate);
  388. }
  389. if(val == '7'){
  390. digitalWrite(LED_13, HIGH);
  391. printer.wake();
  392. printer.begin();
  393. Serial.write(firstSensor);
  394. Serial.write(encoderValue);
  395. Serial.write(thirdSensor);
  396. printer.println("Lucky you - the butter is nearly made!");
  397. delay(2000);
  398. digitalWrite(LED_13, LOW);
  399. //printer.feed(1);
  400. Serial.write(firstSensor);
  401. Serial.write(encoderValue);
  402. Serial.write(thirdSensor);
  403.  
  404. digitalWrite(LED_13, HIGH);
  405. printer.println("check out the link to find out more!");
  406. delay(2000);
  407. Serial.write(firstSensor);
  408. Serial.write(encoderValue);
  409. Serial.write(thirdSensor);
  410. printer.println(pdf_link);
  411. delay(2000);
  412. //printer.feed(1);
  413. Serial.write(firstSensor);
  414. Serial.write(encoderValue);
  415. Serial.write(thirdSensor);
  416. printer.println("tear here");
  417. delay(2000);
  418. Serial.write(firstSensor);
  419. Serial.write(encoderValue);
  420. Serial.write(thirdSensor);
  421.  
  422. digitalWrite(LED_13, LOW);
  423. //printer.feed(3);
  424.  
  425. printer.sleep();
  426.  
  427. RFIDReader.begin(RFIDSerialRate);
  428. }
  429. }
  430.  
  431. else{
  432. Serial.write(firstSensor);
  433. Serial.write(encoderValue);
  434. Serial.write(thirdSensor);
  435. }
  436. }
  437.  
  438. void establishContact() {
  439. while (Serial.available() <= 0) {
  440. Serial.print('A'); // send a capital A
  441. delay(300);
  442. }
  443. }
  444.  
  445. void ReadSerial(String &ReadTagString)
  446. {
  447. int bytesread = 0;
  448. int val = 0;
  449. char code[10];
  450. String TagCode="";
  451.  
  452. if(RFIDReader.available() > 0) { // If data available from reader
  453. if((val = RFIDReader.read()) == 10) { // Check for header
  454. bytesread = 0;
  455. while(bytesread<10) { // Read 10 digit code
  456. if( RFIDReader.available() > 0) {
  457. val = RFIDReader.read();
  458. if((val == 10)||(val == 13)) { // If header or stop bytes before the 10 digit reading
  459. break; // Stop reading
  460. }
  461. code[bytesread] = val; // Add the digit
  462. bytesread++; // Ready to read next digit
  463. }
  464. }
  465. if(bytesread == 10) { // If 10 digit read is complete
  466.  
  467. for(int x=0;x<10;x++) //Copy the Chars to a String
  468. {
  469. TagCode += code[x];
  470. }
  471. ReadTagString = TagCode; //Update the caller
  472. while(RFIDReader.available() > 0) //Burn off any characters still in the buffer
  473. {
  474. RFIDReader.read();
  475. }
  476.  
  477. }
  478. bytesread = 0;
  479. TagCode="";
  480. }
  481. }
  482. }
  483.  
  484. void updateEncoder(){
  485. int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  486. int LSB = digitalRead(encoderPin2); //LSB = least significant bit
  487.  
  488. int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  489. int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  490.  
  491. if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
  492. if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
  493.  
  494. lastEncoded = encoded; //store this value for next time
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement