Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Metal Monitor
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-12-09 05:06:10
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Add additional sensors for 3 no's. so total 6 */
- /* sensors. avoid any blocking code and the system to */
- /* sense and work smoothly. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- const int sensorPin = 2;
- bool lastMetalState = false;
- unsigned long lastPrintTime = 0;
- void setup() {
- pinMode(sensorPin, INPUT_PULLUP); // NO PULLUP - let external circuit handle it
- Serial.begin(9600);
- Serial.println("Sensor monitor started.");
- Serial.println("If nothing prints, check wiring:");
- Serial.println("1. Sensor Brown -> +24V");
- Serial.println("2. Sensor Blue -> 24V GND");
- Serial.println("3. Sensor Black -> PC817 Anode");
- Serial.println("4. PC817 Cathode -> 24V GND");
- Serial.println("5. PC817 Output -> Arduino Pin 2");
- Serial.println("6. PC817 GND -> Arduino GND");
- }
- void loop() {
- int sensorValue = digitalRead(sensorPin);
- // If pin reads LOW, metal is detected (PNP+PC817 active-low)
- bool metalDetected = (sensorValue == LOW);
- // Only print when state changes
- if (metalDetected != lastMetalState) {
- if (metalDetected) {
- Serial.println("METAL DETECTED!");
- } else {
- Serial.println("Metal removed.");
- }
- lastMetalState = metalDetected;
- lastPrintTime = millis();
- }
- // If no signal for 3 seconds, warn about possible disconnection
- if (millis() - lastPrintTime > 3000) {
- Serial.println("[System idle - sensor monitoring active]");
- lastPrintTime = millis();
- }
- delay(100); // Check 10 times per second
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment