naivxnaivet

for emmiasim67 (fiverr)

Dec 7th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /*
  2. infraredSensor LOW = detection
  3. metalSensor HIGH = detection
  4. pplasticSensor LOW = detection
  5. /------------------------------------------------------------------------/
  6. Truth Table
  7. 1 = DETECT 0 = NO_DETECT
  8. Please don't get this confused with the actual sensor input
  9.  
  10. M Pl M Pa n
  11. IR SENSOR |1| |1| |1| |1| |0|
  12. PLASTIC SENSOR |1| |1| |0| |0| |0|
  13. METAL SENSOR |1| |0| |1| |0| |0|
  14.  
  15. M = Metal - 1 1 1
  16. Pl = Plastic - 1 1 0
  17. Metal - 1 0 1
  18. Pa = Paper - 1 0 0
  19. null - 0 0 0
  20. /------------------------------------------------------------------------/
  21.  
  22. */
  23.  
  24. /*Charge duration CHANGE THESE*/
  25. const int metalChargeDuration = 0 * 1000;
  26. const int plasticChargeDuration = 10 * 1000;
  27. const int paperChargeDuration = 0 * 1000;
  28.  
  29.  
  30.  
  31.  
  32. /*Sensor pins*/
  33. const int plasticSensorPin = 2;
  34. int plasticSensorValue;
  35.  
  36. const int metalSensorPin = 3;
  37. int metalSensorValue;
  38.  
  39. const int infraredSensorPin = 5;
  40. int infraredSensorValue;
  41.  
  42. const int relayModule = 6;
  43.  
  44. /*Conditions*/
  45. uint8_t IR_DETECT = LOW;
  46. uint8_t IR_NO_DETECT = HIGH;
  47.  
  48. uint8_t P_DETECT = LOW;
  49. uint8_t P_NO_DETECT = HIGH;
  50.  
  51. uint8_t M_DETECT = HIGH;
  52. uint8_t M_NO_DETECT = LOW;
  53.  
  54. uint8_t RELAY_ON = LOW;
  55. uint8_t RELAY_OFF = HIGH;
  56.  
  57. unsigned long previousMillis = 0;
  58.  
  59. bool checkNow = false;
  60.  
  61. unsigned long lastPrint;
  62.  
  63.  
  64.  
  65. void setup()
  66. {
  67. Serial.begin(115200);
  68. pinMode(plasticSensorPin, INPUT);
  69. pinMode(metalSensorPin, INPUT);
  70. pinMode(infraredSensorPin, INPUT);
  71. pinMode(relayModule, OUTPUT);
  72. digitalWrite(relayModule, RELAY_OFF);
  73. while (!Serial)
  74. {
  75. ; // Wait for serial to connect
  76. }
  77. }
  78.  
  79. void loop()
  80. {
  81. detectObjects();
  82.  
  83. }
  84.  
  85. void detectObjects()
  86. {
  87. String materialKind;
  88.  
  89. plasticSensorValue = digitalRead(plasticSensorPin);
  90. metalSensorValue = digitalRead(metalSensorPin);
  91. long chargeTime;
  92. int servoDegree;
  93.  
  94. if (infraredSensorValue == IR_DETECT)
  95. {
  96. if ((plasticSensorValue == P_DETECT && metalSensorValue == M_DETECT) || (plasticSensorValue == P_NO_DETECT && metalSensorValue == M_DETECT))
  97. {
  98. chargeTime = 5000;
  99. servoDegree = 30;
  100. materialKind = "metal";
  101.  
  102. }
  103.  
  104. else if (plasticSensorValue == P_DETECT && metalSensorValue == M_NO_DETECT)
  105. {
  106. chargeTime = 4000;
  107. servoDegree = 40;
  108. materialKind = "plastic";
  109.  
  110. }
  111. else if (plasticSensorValue == P_NO_DETECT && metalSensorValue == M_NO_DETECT)
  112. {
  113. chargeTime = 2000;
  114. servoDegree = 60;
  115. materialKind = "paper";
  116. }
  117. else
  118. {
  119. Serial.println("Invalid entry");
  120. materialKind = "invalid";
  121. }
  122. }
  123. checkMaterial(materialKind, chargeTime, servoDegree);
  124.  
  125. }
  126.  
  127. void checkMaterial( String &material, long longVariable, int servoDegree)
  128. {
  129. //Serial.println(material + " is detected charge time is " + chargeDuration + " servo degree is" + servoDegree);
  130.  
  131. if (millis() - lastPrint >= 1000 && longVariable >= 1000) {
  132. lastPrint = millis();
  133. longVariable = longVariable - 1000;
  134. Serial.println(timeSort(longVariable));
  135. }
  136. }
  137.  
  138. void storeValue(char* to, int val, char behind) {
  139. *to++ = '0' + val / 10;
  140. *to++ = '0' + val % 10;
  141. *to++ = behind;
  142. }
  143. char* timeSort(unsigned long num) {
  144. static char Buffer[10];
  145. int totalsec = num / 1000; //gives the total number of seconds
  146. int totalmin = totalsec / 60;
  147. int totalhour = totalmin / 60;
  148. storeValue(Buffer, totalhour, ':');
  149. storeValue(Buffer + 3, totalmin % 60, ':');
  150. storeValue(Buffer + 6, totalsec % 60, 0);
  151. return Buffer;
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment