Advertisement
costi0n

objectCounter2

May 24th, 2023
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | Software | 0 0
  1. // Definisci i pin dei sensori
  2. #define TRANSMITTER_PIN 3  // IR Transmitter pin
  3. #define RECEIVER_PIN 2     // IR Receiver pin
  4.  
  5. int counter = 0;   // Object counter
  6. int lastState = LOW;  // Last detected IR state
  7.  
  8. void setup() {
  9.   pinMode(RECEIVER_PIN, INPUT);
  10.   pinMode(TRANSMITTER_PIN, OUTPUT);
  11.   Serial.begin(9600);  // Open serial connection
  12. }
  13.  
  14. void loop() {
  15.   // Generate 38kHz signal
  16.   for (int i = 0; i < 26; i++) {
  17.     digitalWrite(TRANSMITTER_PIN, HIGH);
  18.     delayMicroseconds(13);
  19.     digitalWrite(TRANSMITTER_PIN, LOW);
  20.     delayMicroseconds(13);
  21.   }
  22.  
  23.   int currentState = digitalRead(RECEIVER_PIN);  // Read the current state of the receiver
  24.  
  25.   // Check if the beam was broken
  26.   if (lastState == HIGH && currentState == LOW) {
  27.     counter++;  // Increment the counter
  28.     Serial.println("Object detected!");
  29.     Serial.print("Total number of objects detected: ");
  30.     Serial.println(counter);
  31.   }
  32.  
  33.   lastState = currentState;  // Update the last state
  34.  
  35.   delay(100);  // Delay to avoid multiple detection of the same object
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement