pleasedontcode

Metal Monitor rev_04

Dec 8th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Metal Monitor
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-12-09 05:06:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Add additional sensors for 3 no's. so total 6 */
  21.     /* sensors. avoid any blocking code and the system to */
  22.     /* sense and work smoothly. */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. const int sensorPin = 2;
  35. bool lastMetalState = false;
  36. unsigned long lastPrintTime = 0;
  37.  
  38. void setup() {
  39.   pinMode(sensorPin, INPUT_PULLUP);  // NO PULLUP - let external circuit handle it
  40.   Serial.begin(9600);
  41.   Serial.println("Sensor monitor started.");
  42.   Serial.println("If nothing prints, check wiring:");
  43.   Serial.println("1. Sensor Brown -> +24V");
  44.   Serial.println("2. Sensor Blue -> 24V GND");
  45.   Serial.println("3. Sensor Black -> PC817 Anode");
  46.   Serial.println("4. PC817 Cathode -> 24V GND");
  47.   Serial.println("5. PC817 Output -> Arduino Pin 2");
  48.   Serial.println("6. PC817 GND -> Arduino GND");
  49. }
  50.  
  51. void loop() {
  52.   int sensorValue = digitalRead(sensorPin);
  53.  
  54.   // If pin reads LOW, metal is detected (PNP+PC817 active-low)
  55.   bool metalDetected = (sensorValue == LOW);
  56.  
  57.   // Only print when state changes
  58.   if (metalDetected != lastMetalState) {
  59.     if (metalDetected) {
  60.       Serial.println("METAL DETECTED!");
  61.     } else {
  62.       Serial.println("Metal removed.");
  63.     }
  64.     lastMetalState = metalDetected;
  65.     lastPrintTime = millis();
  66.   }
  67.  
  68.   // If no signal for 3 seconds, warn about possible disconnection
  69.   if (millis() - lastPrintTime > 3000) {
  70.     Serial.println("[System idle - sensor monitoring active]");
  71.     lastPrintTime = millis();
  72.   }
  73.  
  74.   delay(100);  // Check 10 times per second
  75. }
  76.  
  77. /* END CODE */
  78.  
Advertisement
Add Comment
Please, Sign In to add comment