Hi, I've set up a breadboard with an IR emitter and receiver, and I'm trying to emit and receive certain pulse durations. The emission works fine, but I'm unable to receive more than illogical values from trying to read pulseIn durations. Can anyone see anything wrong in the code / how can I fix this?
My Circuit ( the power source is the PC it's plugged into):
My Code:
// Variables
int irReceiver = 7;
int irEmitter = 2;
int irValue = 0;
String stringTrigger = "send";
double duration_ms;
int pulseWidth = 20;
// Functions
int sendIRFunction(int x) {
Serial.println((String)"Sending "+ x + "ms");
int repeat = 1000/x;
while (repeat > 0) {
digitalWrite(irEmitter, HIGH);
delay(x);
digitalWrite(irEmitter, LOW);
repeat -= 1;
Serial.println(repeat);
}
}
// Basic setup
void setup() {
pinMode(irReceiver, INPUT);
pinMode(irEmitter, OUTPUT);
Serial.begin(9600);
}
// Waits for "send" input before calling send command
void loop() {
while (Serial.available() > 0) {
if (Serial.readString() == stringTrigger) {
sendIRFunction(pulseWidth);
}
}
readIR();
}
// Loop to constantly print received signals
void readIR() {
unsigned long duration = pulseIn(irReceiver, LOW, 110000);
duration_ms = duration/1000;
if (duration_ms > 0) {
Serial.println((String)"Receiving " + duration_ms);
}
// delay(100);
}
8 posts - 4 participants