Advertisement
Guest User

Untitled

a guest
May 5th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. #include<Chrono.h>
  2.  
  3. union ArrayToInteger {
  4.   byte array[4];
  5.   uint32_t integer;
  6. };
  7.  
  8. //DebugLEDS
  9. int DispensingQuartersLEDDPin = 8;
  10.  
  11. int LEDPin = 7;
  12.  
  13. //InputPins
  14. int QuarterCountAPin = 0;
  15.  
  16. //Setup message bytes
  17. byte inputBytesIn[10];
  18. int maxInBytes = 10;
  19.  
  20. //Coin Tracking
  21. int QuartersToDispense = 0;
  22. int NickelsToDispense = 0;
  23.  
  24. //Hopper Tracking
  25. bool QuarterHopperIsOn = false;
  26. bool NickelHopperIsOn = false;
  27.  
  28. //InputTracking
  29. int LastQuarterInput;
  30. bool QuarterWasValidLastTick = false;
  31.  
  32. //Timing
  33. Chrono chrono;
  34. //int EmptyHopperTIme = 3000;
  35. int EmptyHopperTIme = 30000;
  36.  
  37. //Setup
  38. void setup() {
  39.   //QuartersToDispense = 1;
  40.   QuarterHopperOFF();
  41.   Serial.begin(9600);
  42.   pinMode(DispensingQuartersLEDDPin, OUTPUT);
  43.   pinMode(LEDPin, OUTPUT);
  44.   chrono.restart();
  45.   chrono.stop();
  46. }
  47.  
  48. void printChrono() {
  49.  Serial.print("Elapsed: "); Serial.print(chrono.elapsed()); Serial.print(" ms ("); Serial.print(chrono.isRunning() ? "RUNNING" : "STOPPED"); Serial.println(")");
  50. }
  51.  
  52. //Main Loop
  53. void loop() {
  54.   //printChrono();
  55.  
  56.   //if(Serial.available())
  57.   //{
  58.   //  int numBytes = Serial.available();
  59.   //  Serial.print("Incoming Bytes");
  60.   //  Serial.println(numBytes);
  61.   //}
  62.  
  63.   if (Serial.available() >= maxInBytes)
  64.   {
  65.     //Serial.print("Reading Bytes ");
  66.     for(int i=0; Serial.available() > 0; ++i)
  67.     {
  68.       inputBytesIn[i] = Serial.read();
  69.       //Serial.print(inputBytesIn[i]);
  70.       //Serial.print(", ");
  71.     }
  72.     //Serial.println("End Reading Bytes");
  73.   }
  74.  
  75.   if(chrono.elapsed() > EmptyHopperTIme)
  76.   {
  77.     QuartersToDispense = 0;
  78.   }
  79.  
  80.   if(inputBytesIn[0] == 81)
  81.   {
  82.       //Let the PC know we are ready for more data
  83.       Serial.print(" ADPS");
  84.       ArrayToInteger QuartersIn = {inputBytesIn[1], inputBytesIn[2], inputBytesIn[3], inputBytesIn[4]};
  85.       QuartersToDispense += QuartersIn.integer;
  86.       Serial.print(" Q");
  87.       Serial.print(QuartersToDispense);
  88.       //ArrayToInteger NickelsIn = {inputBytesIn[6], inputBytesIn[7], inputBytesIn[8], inputBytesIn[9]};
  89.       //NickelsToDispense += NickelsIn.integer;
  90.       NickelsToDispense = 0;
  91.       Serial.print(" N");
  92.       Serial.print(NickelsToDispense);
  93.       inputBytesIn[0] = 0;
  94.   }else if(inputBytesIn[0] == 122)
  95.   {
  96.       char myBuffer[4];
  97.       myBuffer[0] = inputBytesIn[1];
  98.       myBuffer[1] = inputBytesIn[2];
  99.       myBuffer[2] = inputBytesIn[3];
  100.       myBuffer[3] = inputBytesIn[4];
  101.       Serial.println(atoi(myBuffer));
  102.       inputBytesIn[0] = 0;
  103.      
  104.       QuartersToDispense += atoi(myBuffer);
  105.       Serial.print(" Q");
  106.       Serial.println(QuartersToDispense);
  107.   }
  108.   else
  109.   {
  110.     if(QuartersToDispense > 0)
  111.     {
  112.       if(QuarterHopperIsOn == false)
  113.       {
  114.         QuarterHopperON();
  115.       }
  116.       QuarterCounting();
  117.     }else{
  118.       if(QuarterHopperIsOn)
  119.       {
  120.         QuarterHopperOFF();
  121.       }
  122.     }
  123.   }
  124. }
  125.  
  126. void QuarterCounting()
  127. {
  128.     int inValue = analogRead(QuarterCountAPin);
  129.     //Serial.println(inValue);
  130.     if(LastQuarterInput != inValue){
  131.       //Serial.print(LastQuarterInput);
  132.       //Serial.print(" , ");
  133.       //Serial.println(inValue);
  134.      
  135.       if(abs(inValue - 0) <= 5 && !QuarterWasValidLastTick)
  136.       {
  137.         //Serial.print(LastQuarterInput);
  138.         //Serial.print(" , ");
  139.         //Serial.println(inValue);
  140.         chrono.restart();
  141.         --QuartersToDispense;
  142.         Serial.print("Quarter Count = ");
  143.         Serial.println(QuartersToDispense);
  144.         QuarterWasValidLastTick = true;
  145.         //delay(100);
  146.       }else if(abs(inValue - 0) >= 10 && QuarterWasValidLastTick){
  147.         QuarterWasValidLastTick = false;
  148.       }
  149.       LastQuarterInput = inValue;
  150.     }
  151. }
  152.  
  153. void QuarterHopperON()
  154. {
  155.   QuarterHopperIsOn = true;
  156.   Serial.println("TODO: Turn QuarterHopper On");
  157.   digitalWrite(DispensingQuartersLEDDPin, LOW);
  158.   digitalWrite(LEDPin, HIGH);
  159.   chrono.restart();
  160. }
  161.  
  162. void QuarterHopperOFF()
  163. {
  164.   QuarterHopperIsOn = false;
  165.   digitalWrite(DispensingQuartersLEDDPin, HIGH);
  166.   digitalWrite(LEDPin, LOW);
  167.   Serial.println("TODO: Turn QuarterHopper Off");
  168.   chrono.restart();
  169.   chrono.stop();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement