Hi everyone
I'm Having the following Problem:
I built a IR-Sender for our ceiling fan. The IR-Signal gets triggered by the push of a switch. the switch is pulled up by the Arduino (Arduino Nano).
When i turn on the lights in our bathroom (adjacent to the bedroom, where the ceiling fan is installed), the IR-Signal gets triggered as well. It does not happen all the time, but it got worse over last few weeks.
This is my code:
#include <IRremote.h>
#define irRecvPin 2
#define irLedPin 3 // PWM-Pin for IR
#define lightSwitch 5
#define fanPoti A0
byte fanPotiState = 0;
byte lastPotiState = 0;
byte lightSwitchState = 1;
byte lastLightSwitchState = 1;
void setup() {
pinMode(irLedPin, OUTPUT);
pinMode(lightSwitch, INPUT_PULLUP);
pinMode(fanPoti, INPUT);
//Serial.begin(9600);
}
void loop() {
//Serial.println(map(analogRead(fanPoti), 0, 1023, 4, 0));
lightSwitchState = digitalRead(lightSwitch);
if (lightSwitchState != lastLightSwitchState) {
if (lightSwitchState == LOW) {
delay(50);
sendCodeLight();
}
delay(200);
}
lastLightSwitchState = lightSwitchState;
fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
if (fanPotiState != lastPotiState) {
if ((map(analogRead(fanPoti), 0, 1023, 4, 0) == 0)||(map(analogRead(fanPoti), 0, 1023, 4, 0) == 1)) {
fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
delay(50);
sendCodeFan0();
delay(200);
sendCodeFan0();
//Serial.println("senden");
}
else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 2) {
fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
delay(50);
sendCodeFan1();
delay(200);
sendCodeFan1();
//Serial.println("senden");
}
else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 3) {
fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
sendCodeFan2();
delay(200);
sendCodeFan2();
//Serial.println("senden");
}
else if (map(analogRead(fanPoti), 0, 1023, 4, 0) == 4) {
fanPotiState = map(analogRead(fanPoti), 0, 1023, 4, 0);
delay(50);
sendCodeFan3();
delay(200);
sendCodeFan3();
//Serial.println("senden");
}
}
lastPotiState = fanPotiState;
}
// IR-Codes definieren
void sendCodeLight() {
byte irCode1[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} ;
byte irCode2[] = {1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0} ;
sendCodeLight(irCode1,irCode2);
}
void sendCodeFan0() {
byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0};
sendCode(irCode);
}
void sendCodeFan3() {
byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
sendCode(irCode);
}
void sendCodeFan2() {
byte irCode[] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0};
sendCode(irCode);
}
void sendCodeFan1() {
byte irCode[] = {1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1};
sendCode(irCode);
}
// IR-Code senden
void sendCode(byte irCode[]) {
for (byte r = 0; r < 3; r++) {
for (byte i = 0; i < 12; i++) {
sendCodeStep(irCode[i]);
}
delayMicroseconds(6620);
}
}
void sendCodeLight(byte irCode1[], byte irCode2[]) {
for (byte i = 0; i < 12; i++) {
sendCodeStep(irCode1[i]);
}
delayMicroseconds(6620);
for (byte i = 0; i < 12; i++) {
sendCodeStep(irCode2[i]);
}
delayMicroseconds(6620);
for (byte i = 0; i < 12; i++) {
sendCodeStep(irCode2[i]);
}
delayMicroseconds(6620);
for (byte i = 0; i < 12; i++) {
sendCodeStep(irCode2[i]);
}
delayMicroseconds(6620);
}
// aktuelle Stelle vom IR-Code senden
void sendCodeStep(byte actualStep) {
if (actualStep == 0) {
sendPulse(360);
delayMicroseconds(1300);
} else {
sendPulse(1200);
delayMicroseconds(450);
}
}
// Puls mit ca. 38 kHz senden
void sendPulse(int pulseLength) {
cli();
while (pulseLength > 0) {
digitalWrite(irLedPin, HIGH);
delayMicroseconds(10);
digitalWrite(irLedPin, LOW);
delayMicroseconds(10);
pulseLength -= 26;
}
sei();
}
Has anyone an idea why this is happening?
Why do our bathroom lights affect the Arduinos digitalRead?
Thank You for any Inputs!
Have a great day ![]()
