Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- infraredSensor LOW = detection
- metalSensor HIGH = detection
- pplasticSensor LOW = detection
- /------------------------------------------------------------------------/
- Truth Table
- 1 = DETECT 0 = NO_DETECT
- Please don't get this confused with the actual sensor input
- M Pl M Pa n
- IR SENSOR |1| |1| |1| |1| |0|
- PLASTIC SENSOR |1| |1| |0| |0| |0|
- METAL SENSOR |1| |0| |1| |0| |0|
- M = Metal - 1 1 1
- Pl = Plastic - 1 1 0
- Metal - 1 0 1
- Pa = Paper - 1 0 0
- null - 0 0 0
- /------------------------------------------------------------------------/
- */
- /*Charge duration CHANGE THESE*/
- const int metalChargeDuration = 0 * 1000;
- const int plasticChargeDuration = 10 * 1000;
- const int paperChargeDuration = 0 * 1000;
- /*Sensor pins*/
- const int plasticSensorPin = 2;
- int plasticSensorValue;
- const int metalSensorPin = 3;
- int metalSensorValue;
- const int infraredSensorPin = 5;
- int infraredSensorValue;
- const int relayModule = 6;
- /*Conditions*/
- uint8_t IR_DETECT = LOW;
- uint8_t IR_NO_DETECT = HIGH;
- uint8_t P_DETECT = LOW;
- uint8_t P_NO_DETECT = HIGH;
- uint8_t M_DETECT = HIGH;
- uint8_t M_NO_DETECT = LOW;
- uint8_t RELAY_ON = LOW;
- uint8_t RELAY_OFF = HIGH;
- unsigned long previousMillis = 0;
- bool checkNow = false;
- unsigned long lastPrint;
- void setup()
- {
- Serial.begin(115200);
- pinMode(plasticSensorPin, INPUT);
- pinMode(metalSensorPin, INPUT);
- pinMode(infraredSensorPin, INPUT);
- pinMode(relayModule, OUTPUT);
- digitalWrite(relayModule, RELAY_OFF);
- while (!Serial)
- {
- ; // Wait for serial to connect
- }
- }
- void loop()
- {
- detectObjects();
- }
- void detectObjects()
- {
- String materialKind;
- plasticSensorValue = digitalRead(plasticSensorPin);
- metalSensorValue = digitalRead(metalSensorPin);
- long chargeTime;
- int servoDegree;
- if (infraredSensorValue == IR_DETECT)
- {
- if ((plasticSensorValue == P_DETECT && metalSensorValue == M_DETECT) || (plasticSensorValue == P_NO_DETECT && metalSensorValue == M_DETECT))
- {
- chargeTime = 5000;
- servoDegree = 30;
- materialKind = "metal";
- }
- else if (plasticSensorValue == P_DETECT && metalSensorValue == M_NO_DETECT)
- {
- chargeTime = 4000;
- servoDegree = 40;
- materialKind = "plastic";
- }
- else if (plasticSensorValue == P_NO_DETECT && metalSensorValue == M_NO_DETECT)
- {
- chargeTime = 2000;
- servoDegree = 60;
- materialKind = "paper";
- }
- else
- {
- Serial.println("Invalid entry");
- materialKind = "invalid";
- }
- }
- checkMaterial(materialKind, chargeTime, servoDegree);
- }
- void checkMaterial( String &material, long longVariable, int servoDegree)
- {
- //Serial.println(material + " is detected charge time is " + chargeDuration + " servo degree is" + servoDegree);
- if (millis() - lastPrint >= 1000 && longVariable >= 1000) {
- lastPrint = millis();
- longVariable = longVariable - 1000;
- Serial.println(timeSort(longVariable));
- }
- }
- void storeValue(char* to, int val, char behind) {
- *to++ = '0' + val / 10;
- *to++ = '0' + val % 10;
- *to++ = behind;
- }
- char* timeSort(unsigned long num) {
- static char Buffer[10];
- int totalsec = num / 1000; //gives the total number of seconds
- int totalmin = totalsec / 60;
- int totalhour = totalmin / 60;
- storeValue(Buffer, totalhour, ':');
- storeValue(Buffer + 3, totalmin % 60, ':');
- storeValue(Buffer + 6, totalsec % 60, 0);
- return Buffer;
- }
Advertisement
Add Comment
Please, Sign In to add comment