Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ROTARY ENCODER:
- On the side of the rotary encoder with 3 legs, theres
- a green and a black mark;
- !!! 2 AND 3 are special Pins !!!
- Green Mark <---> Arduino Digital Pin 2
- Black Mark <---> Arduino Digital Pin 3
- middle <---> Arduino Ground
- RFID CARD:
- Needs to be powered off the Arduino
- RFID ENABLE <---> Arduino Digital Pin 10
- RFID SOUT <---> Arduino Digital Pin 11
- SERVO
- Need to be powered externally
- Run a ground between the Arduino and the ground for the servo
- PRINTER
- GREEN WIRE <----> Arduino Digital Pin 5
- YELLOW WIRE <----> Arduino Digital Pin 6
- */
- #include <SoftwareSerial.h>
- #include <Servo.h>
- //PRINTER
- #include "SoftwareSerial.h"
- #include "Adafruit_Thermal.h"
- //add the irish times QR Code
- #include "story1sml.h"
- #include "story2.h"
- #include "story4.h"
- #include "story5.h"
- #include "story6.h"
- #include <avr/pgmspace.h>
- int printer_RX_Pin = 5; // This is the green wire
- int printer_TX_Pin = 6; // This is the yellow wire
- Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);
- //Parallax RFID Reader
- //can be set to either 2 or 4
- #define RFIDEnablePin 10 //Pin that enables reading. Set as OUTPUT and LOW to read an RFID tag
- #define RFIDSerialRate 2400 //Parallax RFID Reader Serial Port Speed
- //Using SoftwareSerial Library to locate the serial pins off the default set
- //This allows the Arduino to be updated via USB with no conflict
- #define RxPin 11 //Pin to read data from Reader
- #define TxPin 10 //Pin to write data to the Reader NOTE: The reader doesn't get written to, don't connect this line.
- SoftwareSerial RFIDReader(RxPin,TxPin);
- String RFIDTAG=""; //Holds the RFID Code read from a tag
- String DisplayTAG = ""; //Holds the last displayed RFID Tag
- int firstSensor = 0;
- int secondSensor = 0;
- int thirdSensor = 0;
- int trackNum;
- int inByte = 0;
- char val;
- int encoderPin1 = 2;
- int encoderPin2 = 3;
- volatile int lastEncoded = 0;
- volatile long encoderValue = 0;
- long lastencoderValue = 0;
- int lastMSB = 0;
- int lastLSB = 0;
- //servo motors
- //attached to 8 & 9
- //slotServo is for the slot, gateServo is to hold the card
- Servo gateServo, slotServo, handleServo;
- int slotServoPos = 0;
- //SLOT LIGHT
- int slotLight = 18;
- //Printer text
- //strings should be short
- String story1 = "Feel free to visit neighbours, but don't be surprised if they don't trust you!";
- String story2 = "Watch out for evil neighbours hoping to steal your butter.";
- String story3 = "Story 3";
- String story4 = "Maybe superstitions didn't really exist at all?";
- String story5 = "Stealing butter was evil, but who would do such a thing?";
- String story6 = "But why did people believe in superstitions?";
- String currentStory;
- /*
- String story1_pt2 = "keep churning";
- String story2_pt2 = "keep churning";
- String story4_pt2 = "keep churning";
- String story5_pt2 = "keep churning";
- String story6_pt2 = "keep churning";
- String currentStory_pt2;
- */
- String story1_link = "http://goo.gl/q0tqE9";
- String story2_link = "http://goo.gl/ChbeOq";
- String story4_link = "http://goo.gl/5eT1Wz";
- String story5_link = "http://goo.gl/PPNj57";
- String story6_link = "http://goo.gl/pdGA7u";
- String pdf_link;
- //Printer QR Code
- String currentQR;
- int LED_13 = 13;
- void setup()
- {
- //
- // RFID CARD READER CODE
- //
- // RFID reader SOUT pin connected to Serial RX pin at 2400bps
- RFIDReader.begin(RFIDSerialRate);
- // Set Enable pin as OUTPUT to connect it to the RFID /ENABLE pin
- pinMode(RFIDEnablePin,OUTPUT);
- // Activate the RFID reader
- // Setting the RFIDEnablePin HIGH will deactivate the reader
- // which could be usefull if you wanted to save battery life for
- // example.
- digitalWrite(RFIDEnablePin, LOW);
- Serial.begin(9600); // set up Serial library at 9600 bps
- Serial.println("Hello world --!"); // prints hello with ending line break
- ///-------------------------------\\\\\
- //
- // ROTARY ENCODER CODE
- //
- pinMode(encoderPin1, INPUT);
- pinMode(encoderPin2, INPUT);
- digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
- digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
- //call updateEncoder() when any high/low changed seen
- //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
- attachInterrupt(0, updateEncoder, CHANGE);
- attachInterrupt(1, updateEncoder, CHANGE);
- //SERVO CODE
- handleServo.attach(7);
- gateServo.attach(8);
- slotServo.attach(9);
- gateServo.write(0);//bottom servo, 0 == closed, 90 == open
- slotServo.write(60);//top servo, 0 == closed, 60 == open
- handleServo.write(90);
- pinMode(12, OUTPUT);
- pinMode(LED_13, OUTPUT);
- //SLOT LIGHT
- pinMode(slotLight, OUTPUT);
- //establish contact with Processing
- establishContact();
- }
- void loop()
- {
- if(RFIDReader.available() > 0) // If data available from reader
- {
- gateServo.detach();
- slotServo.detach();
- handleServo.detach();
- ReadSerial(RFIDTAG); //Read the tag number from the reader. Should return a 10 digit serial number
- digitalWrite(RFIDEnablePin, HIGH);
- //attatch servo and ease it close slot
- slotServo.attach(9);
- for(slotServoPos = 60; slotServoPos >= 0; slotServoPos -= 1){
- //slotServo.write(0);
- slotServo.write(slotServoPos);
- delay(20);
- }
- thirdSensor = 1;
- delay(2000);
- slotServo.detach();
- //digitalWrite(RFIDEnablePin, LOW);
- }
- //This only displays a tag once, unless another tag is scanned
- if(DisplayTAG!=RFIDTAG)
- {
- gateServo.detach();
- slotServo.detach();
- DisplayTAG=RFIDTAG;
- //Serial.println(RFIDTAG);
- }
- if(RFIDTAG == "0415ED29AC"){
- trackNum = 1;
- currentStory = story1;
- //currentStory_pt2 = story1_pt2;
- pdf_link = story1_link;
- }
- if(RFIDTAG == "0415ED33DF"){
- trackNum = 2;
- currentStory = story2;
- //currentStory_pt2 = story2_pt2;
- pdf_link = story2_link;
- }
- if(RFIDTAG == "0415ED41D2"){
- trackNum = 3;
- currentStory = story3;
- }
- if(RFIDTAG == "0415ED4C69"){
- trackNum = 4;
- currentStory = story4;
- //currentStory_pt2 = story4_pt2;
- pdf_link = story4_link;
- }
- if(RFIDTAG == "0415ED50A4"){
- trackNum = 5;
- currentStory = story5;
- //currentStory_pt2 = story5_pt2;
- pdf_link = story5_link;
- }
- if(RFIDTAG == "0415ED4C54"){
- trackNum = 6;
- currentStory = story6;
- //currentStory_pt2 = story6_pt2;
- pdf_link = story6_link;
- }
- firstSensor = trackNum;
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- //secondSensor ++;
- if(Serial.available()){
- val = Serial.read();
- if(val == '0'){ //--if someone churned their way to the end of the track, eject and print--\\
- //someone has listened to the full track
- //so eject the token
- digitalWrite(12, HIGH);
- //TURN THE RFID READER OFF
- digitalWrite(RFIDEnablePin, HIGH);
- gateServo.attach(8);
- slotServo.attach(9);
- gateServo.write(90); //open servo
- //slotServo.write(60);
- handleServo.attach(7);
- handleServo.write(90);
- //slot is open, so don't play any track
- thirdSensor = 0;
- delay(3000); // wait 3 seconds
- gateServo.write(0); //close servo
- slotServo.write(60);
- delay(2000);
- //set murmur track to play
- //trackNum = 0;
- gateServo.detach();
- slotServo.detach();
- digitalWrite(12, LOW);
- //TURN THE RFID READER ON AGAIN
- digitalWrite(RFIDEnablePin, LOW);
- }
- if(val == '1'){ //--if someone stopped churning mid way, eject the card--\\
- digitalWrite(12, LOW);
- //someone has listened to the full track
- //so eject the token and print a receipt
- digitalWrite(12, HIGH);
- //TURN THE RFID READER OFF
- digitalWrite(RFIDEnablePin, HIGH);
- gateServo.attach(8);
- slotServo.attach(9);
- gateServo.write(90); //open servo
- //slotServo.write(60);
- handleServo.attach(7);
- handleServo.write(90);
- //slot is open, so don't play any track
- thirdSensor = 0;
- delay(3000); // wait 3 seconds
- gateServo.write(0); //close servo
- slotServo.write(60);
- delay(2000);
- //set murmur track to play
- //trackNum = 0;
- gateServo.detach();
- slotServo.detach();
- digitalWrite(12, LOW);
- //TURN THE RFID READER ON AGAIN
- digitalWrite(RFIDEnablePin, LOW);
- }
- if(val == '2'){
- digitalWrite(LED_13, LOW);
- handleServo.attach(7);
- handleServo.write(90);
- }
- //IF SOMEONE IS CHURNING - ACTUATE HANDLEMOTOR
- if(val == '3'){
- digitalWrite(slotLight, LOW);
- handleServo.attach(7);
- handleServo.write(70);
- }
- //if someone isn't churning
- //code taken from http://labs.ideo.com/2014/03/20/vodafonepursecharger/
- if(val == '4'){
- static bool isLightOn = false; //-- initialize as static (i.e. persistent in this function)
- static unsigned long lastTimeRecorded = millis(); //-- history of time where we switched the lights
- unsigned long currentTime = millis(); //-- read the current time once
- if ( currentTime - lastTimeRecorded > 1000 ) { //-- if it's been more than 1000ms
- if( isLightOn ) { //-- and if the light is on
- digitalWrite(slotLight, LOW); //-- turn it off
- isLightOn = false; //-- set our flag to false
- } else { //-- otherwise, we know the light is off
- digitalWrite(slotLight, HIGH); //-- so turn it on
- isLightOn = true; //-- set our flag to true
- }
- lastTimeRecorded = currentTime; //-- update the last time we switched
- }
- }
- if(val == '5'){
- digitalWrite(LED_13, HIGH);
- printer.begin();
- //PRINT STORY
- printer.setSize('S');
- printer.println("Keep churning to the end!!");
- delay(3000);
- digitalWrite(LED_13, LOW);
- digitalWrite(LED_13, HIGH);
- //printer.feed(1);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- printer.println(currentStory);
- delay(3000);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- digitalWrite(LED_13, LOW);
- //PRINT QR
- if(trackNum == 1){
- //printer.printBitmap(story1sml_width, story1sml_height, story1sml_data);
- }
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- digitalWrite(LED_13, HIGH);
- //printer.feed(2);
- printer.sleep();
- digitalWrite(LED_13, LOW);
- RFIDReader.begin(RFIDSerialRate);
- }
- if(val == '6'){
- //printer.wake();
- printer.begin();
- digitalWrite(LED_13, HIGH);
- //PRINT STORY
- printer.setSize('M');
- printer.println("Keep churning!");
- delay(3000);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- //printer.feed(3);
- digitalWrite(LED_13, LOW);
- printer.sleep();
- RFIDReader.begin(RFIDSerialRate);
- }
- if(val == '7'){
- digitalWrite(LED_13, HIGH);
- printer.wake();
- printer.begin();
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- printer.println("Lucky you - the butter is nearly made!");
- delay(2000);
- digitalWrite(LED_13, LOW);
- //printer.feed(1);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- digitalWrite(LED_13, HIGH);
- printer.println("check out the link to find out more!");
- delay(2000);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- printer.println(pdf_link);
- delay(2000);
- //printer.feed(1);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- printer.println("tear here");
- delay(2000);
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- digitalWrite(LED_13, LOW);
- //printer.feed(3);
- printer.sleep();
- RFIDReader.begin(RFIDSerialRate);
- }
- }
- else{
- Serial.write(firstSensor);
- Serial.write(encoderValue);
- Serial.write(thirdSensor);
- }
- }
- void establishContact() {
- while (Serial.available() <= 0) {
- Serial.print('A'); // send a capital A
- delay(300);
- }
- }
- void ReadSerial(String &ReadTagString)
- {
- int bytesread = 0;
- int val = 0;
- char code[10];
- String TagCode="";
- if(RFIDReader.available() > 0) { // If data available from reader
- if((val = RFIDReader.read()) == 10) { // Check for header
- bytesread = 0;
- while(bytesread<10) { // Read 10 digit code
- if( RFIDReader.available() > 0) {
- val = RFIDReader.read();
- if((val == 10)||(val == 13)) { // If header or stop bytes before the 10 digit reading
- break; // Stop reading
- }
- code[bytesread] = val; // Add the digit
- bytesread++; // Ready to read next digit
- }
- }
- if(bytesread == 10) { // If 10 digit read is complete
- for(int x=0;x<10;x++) //Copy the Chars to a String
- {
- TagCode += code[x];
- }
- ReadTagString = TagCode; //Update the caller
- while(RFIDReader.available() > 0) //Burn off any characters still in the buffer
- {
- RFIDReader.read();
- }
- }
- bytesread = 0;
- TagCode="";
- }
- }
- }
- void updateEncoder(){
- int MSB = digitalRead(encoderPin1); //MSB = most significant bit
- int LSB = digitalRead(encoderPin2); //LSB = least significant bit
- int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
- int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
- if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
- if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
- lastEncoded = encoded; //store this value for next time
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement