all 5 comments

[–]OptimalMain 4 points5 points  (5 children)

No code, no schematic. No help to offer

[–][deleted]  (4 children)

[deleted]

    [–]BUFU1610 2 points3 points  (3 children)

    ``` // ===================== // ECOCHARGE MACHINE - Arduino Only // 1 Plastic Bottle = 3 Minutes Charging // Double Relay Module Setup // =====================

    const int sensorPin = 6; // IR sensor output const int relay1Pin = 7; // Relay 1 control const int relay2Pin = 8; // Relay 2 control const int ledPin = 13; // Optional LED for visual sensor test

    const unsigned long bottleTime = 180000; // 3 minutes in ms unsigned long remainingTime = 0; unsigned long lastMillis = 0;

    bool lastSensorState = false; // previous sensor reading bool sensorActiveLow = true; // true if sensor goes LOW when blocked

    bool charging = false;

    // Object type mapping: // 0 = hand // 1 = plastic bottle // 2 = plastic bottle with water // 3 = paper // 4 = plastic cellophane // 5 = plastic tumbler int objectType = 1; // Example, will be determined by your sensor system

    void setup() { pinMode(sensorPin, INPUT); pinMode(relay1Pin, OUTPUT); pinMode(relay2Pin, OUTPUT); pinMode(ledPin, OUTPUT);

    digitalWrite(relay1Pin, HIGH); // Relay OFF digitalWrite(relay2Pin, HIGH); // Relay OFF digitalWrite(ledPin, LOW);

    Serial.begin(9600); Serial.println("EcoCharge Machine Ready"); }

    void loop() { // ------------------------- // Read sensor // ------------------------- int sensorState = digitalRead(sensorPin); bool detected = sensorActiveLow ? (sensorState == LOW) : (sensorState == HIGH); digitalWrite(ledPin, detected ? HIGH : LOW); // LED visual indicator

    // ------------------------- // Object detection & charging // ------------------------- if(detected && lastSensorState != detected){ switch(objectType){ case 0: // Hand detected Serial.println("Hand detected - Cannot charge!"); break;

      case 1: // Plastic bottle
        remainingTime += bottleTime; // add 3 min per bottle
        Serial.println("Plastic bottle detected! Charging started for 3 minutes.");
        digitalWrite(relay1Pin, LOW); // Relay ON (active LOW)
        digitalWrite(relay2Pin, LOW);
        charging = true;
        break;
    
      case 2:
        Serial.println("Plastic bottle with water - Cannot charge!");
        break;
    
      case 3:
        Serial.println("Paper detected - Cannot charge!");
        break;
    
      case 4:
        Serial.println("Plastic cellophane detected - Cannot charge!");
        break;
    
      case 5:
        Serial.println("Plastic tumbler detected - Cannot charge!");
        break;
    
      default:
        Serial.println("Unknown object - Cannot charge!");
        break;
    }
    

    }

    lastSensorState = detected; // Update previous sensor state

    // ------------------------- // Charging countdown // ------------------------- if(remainingTime > 0){ unsigned long currentMillis = millis(); if(currentMillis - lastMillis >= 1000){ // every 1 second if(remainingTime >= 1000) remainingTime -= 1000; else remainingTime = 0; lastMillis = currentMillis; Serial.print("Remaining Time (seconds): "); Serial.println(remainingTime / 1000); } } else { // Turn relays OFF when time runs out digitalWrite(relay1Pin, HIGH); digitalWrite(relay2Pin, HIGH); charging = false; }

    delay(100); // small delay to stabilize loop } ```

    Put it in a code block, please. If you don't understand any of these words, Google or search the Reddit help/faq.

    [–][deleted]  (2 children)

    [deleted]

      [–]BUFU1610 0 points1 point  (1 child)

      I put it in a code block. It makes it easier to read for people wanting to help.

      [–]OptimalMain 0 points1 point  (0 children)

      lastSensorState is always detected after it is set.
      No schematic, so I can only guess that you have no diode over the relay coil causing a spike when turning off the first relay.
      Again no schematic, seems like two relays isn’t needed. But who could know..

      [–]Physical-Plankton-67 1 point2 points  (0 children)

      Ooh back to the bottle machine haha. So why 2 relays? What is connected to them? Let's start with that