Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<Chrono.h>
- union ArrayToInteger {
- byte array[4];
- uint32_t integer;
- };
- //DebugLEDS
- int DispensingQuartersLEDDPin = 8;
- int LEDPin = 7;
- //InputPins
- int QuarterCountAPin = 0;
- //Setup message bytes
- byte inputBytesIn[10];
- int maxInBytes = 10;
- //Coin Tracking
- int QuartersToDispense = 0;
- int NickelsToDispense = 0;
- //Hopper Tracking
- bool QuarterHopperIsOn = false;
- bool NickelHopperIsOn = false;
- //InputTracking
- int LastQuarterInput;
- bool QuarterWasValidLastTick = false;
- //Timing
- Chrono chrono;
- //int EmptyHopperTIme = 3000;
- int EmptyHopperTIme = 30000;
- //Setup
- void setup() {
- //QuartersToDispense = 1;
- QuarterHopperOFF();
- Serial.begin(9600);
- pinMode(DispensingQuartersLEDDPin, OUTPUT);
- pinMode(LEDPin, OUTPUT);
- chrono.restart();
- chrono.stop();
- }
- void printChrono() {
- Serial.print("Elapsed: "); Serial.print(chrono.elapsed()); Serial.print(" ms ("); Serial.print(chrono.isRunning() ? "RUNNING" : "STOPPED"); Serial.println(")");
- }
- //Main Loop
- void loop() {
- //printChrono();
- //if(Serial.available())
- //{
- // int numBytes = Serial.available();
- // Serial.print("Incoming Bytes");
- // Serial.println(numBytes);
- //}
- if (Serial.available() >= maxInBytes)
- {
- //Serial.print("Reading Bytes ");
- for(int i=0; Serial.available() > 0; ++i)
- {
- inputBytesIn[i] = Serial.read();
- //Serial.print(inputBytesIn[i]);
- //Serial.print(", ");
- }
- //Serial.println("End Reading Bytes");
- }
- if(chrono.elapsed() > EmptyHopperTIme)
- {
- QuartersToDispense = 0;
- }
- if(inputBytesIn[0] == 81)
- {
- //Let the PC know we are ready for more data
- Serial.print(" ADPS");
- ArrayToInteger QuartersIn = {inputBytesIn[1], inputBytesIn[2], inputBytesIn[3], inputBytesIn[4]};
- QuartersToDispense += QuartersIn.integer;
- Serial.print(" Q");
- Serial.print(QuartersToDispense);
- //ArrayToInteger NickelsIn = {inputBytesIn[6], inputBytesIn[7], inputBytesIn[8], inputBytesIn[9]};
- //NickelsToDispense += NickelsIn.integer;
- NickelsToDispense = 0;
- Serial.print(" N");
- Serial.print(NickelsToDispense);
- inputBytesIn[0] = 0;
- }else if(inputBytesIn[0] == 122)
- {
- char myBuffer[4];
- myBuffer[0] = inputBytesIn[1];
- myBuffer[1] = inputBytesIn[2];
- myBuffer[2] = inputBytesIn[3];
- myBuffer[3] = inputBytesIn[4];
- Serial.println(atoi(myBuffer));
- inputBytesIn[0] = 0;
- QuartersToDispense += atoi(myBuffer);
- Serial.print(" Q");
- Serial.println(QuartersToDispense);
- }
- else
- {
- if(QuartersToDispense > 0)
- {
- if(QuarterHopperIsOn == false)
- {
- QuarterHopperON();
- }
- QuarterCounting();
- }else{
- if(QuarterHopperIsOn)
- {
- QuarterHopperOFF();
- }
- }
- }
- }
- void QuarterCounting()
- {
- int inValue = analogRead(QuarterCountAPin);
- //Serial.println(inValue);
- if(LastQuarterInput != inValue){
- //Serial.print(LastQuarterInput);
- //Serial.print(" , ");
- //Serial.println(inValue);
- if(abs(inValue - 0) <= 5 && !QuarterWasValidLastTick)
- {
- //Serial.print(LastQuarterInput);
- //Serial.print(" , ");
- //Serial.println(inValue);
- chrono.restart();
- --QuartersToDispense;
- Serial.print("Quarter Count = ");
- Serial.println(QuartersToDispense);
- QuarterWasValidLastTick = true;
- //delay(100);
- }else if(abs(inValue - 0) >= 10 && QuarterWasValidLastTick){
- QuarterWasValidLastTick = false;
- }
- LastQuarterInput = inValue;
- }
- }
- void QuarterHopperON()
- {
- QuarterHopperIsOn = true;
- Serial.println("TODO: Turn QuarterHopper On");
- digitalWrite(DispensingQuartersLEDDPin, LOW);
- digitalWrite(LEDPin, HIGH);
- chrono.restart();
- }
- void QuarterHopperOFF()
- {
- QuarterHopperIsOn = false;
- digitalWrite(DispensingQuartersLEDDPin, HIGH);
- digitalWrite(LEDPin, LOW);
- Serial.println("TODO: Turn QuarterHopper Off");
- chrono.restart();
- chrono.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement