Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Definisci i pin dei sensori
- #define TRANSMITTER_PIN 3 // IR Transmitter pin
- #define RECEIVER_PIN 2 // IR Receiver pin
- int counter = 0; // Object counter
- int lastState = LOW; // Last detected IR state
- void setup() {
- pinMode(RECEIVER_PIN, INPUT);
- pinMode(TRANSMITTER_PIN, OUTPUT);
- Serial.begin(9600); // Open serial connection
- }
- void loop() {
- // Generate 38kHz signal
- for (int i = 0; i < 26; i++) {
- digitalWrite(TRANSMITTER_PIN, HIGH);
- delayMicroseconds(13);
- digitalWrite(TRANSMITTER_PIN, LOW);
- delayMicroseconds(13);
- }
- int currentState = digitalRead(RECEIVER_PIN); // Read the current state of the receiver
- // Check if the beam was broken
- if (lastState == HIGH && currentState == LOW) {
- counter++; // Increment the counter
- Serial.println("Object detected!");
- Serial.print("Total number of objects detected: ");
- Serial.println(counter);
- }
- lastState = currentState; // Update the last state
- delay(100); // Delay to avoid multiple detection of the same object
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement