I'm sorry for mistakes in schematic... I've corrected them to represent the real world situation, it was only on the paper. Ok, I will try some bigger decoupling cap, and see how it goes. As for pullup on irDATA line from TSOP1840 IR detector - it already has 100k internal pull up, plus arduino 20k plus extra 100k makes around 14k summary resistance, so I doubt the problem is pullups. When the glitch happen, I checked power with osciloscoope and it was clean and steady, so PSU is not the issue either. The main problem is I can't replicate the thing... it just comes and goes without any external factor, what makes the debugging a real nightmare. Anyway, here is the code (Yes, I know its a bit mess, and will be hard to understand since my comments are mainly in Latvian)...
int ir_pin = 2; //Sensor pin 1 wired through a 220 ohm resistor
int led_pin = 13; //"Ready to Receive" flag, not needed but nice
int debug = 0; //Serial connection must be started to debug
int start_bit = 2000; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
const int rly_pin = 8;
const int SPKR = 12;
const int sw_pin = 7;
const int ledsw_pin = 6;
int sw = 0;
int prevsw = 0;
int ledsw = 0;
int prevledsw = 0;
int fadeValue = 0;
volatile int fadetarget = 255;
volatile int offtrg = 0;
int pwm_pin = 5;
int fadestep = 25; // solis pa k?du main?s gaisma LED+ un LED-
int fadeSpeed = 1;
int prevValue;
volatile int gaisma = 0;
int minimumFade = 1; // minimaalais atljautais fade value
// variables encoderim --- //
const int pin_A = 3; // pin 3
const int pin_B = 4; // pin 11
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
int rfeedback;
// --- END --- //
void setup() {
pinMode(ir_pin, INPUT);
digitalWrite(ir_pin, HIGH);
digitalWrite(led_pin, LOW); //not ready yet
Serial.begin(115200);
pinMode(SPKR, OUTPUT);
pinMode(led_pin, OUTPUT);
pinMode(rly_pin, OUTPUT);
pinMode(sw_pin, INPUT);
digitalWrite(rly_pin, LOW);
attachInterrupt(0, getIR, LOW); // interrupt 0
pinMode(pwm_pin, OUTPUT);
// --- Variables encoderim --- //
pinMode(pin_A, INPUT);
digitalWrite(pin_A, HIGH);
pinMode(pin_B, INPUT);
digitalWrite(pin_B, HIGH);
attachInterrupt(1, encoder, CHANGE);
// --- END --- //
}
void beep(int garums, int frekvence){
for (int i=0; i < garums; i++) { // gjenereejam vajadziigo beepu
digitalWrite(SPKR, HIGH);
delayMicroseconds(frekvence);
digitalWrite(SPKR, LOW);
delayMicroseconds(frekvence);
}
}
void loop() {
if (led_pin == LOW || rfeedback <= 25) {
rfeedback++;
}else{
digitalWrite(led_pin, HIGH);
rfeedback = 0;
}
delay(7);
sledzis();
ledswitch();
fader();
}
void fader() {
if (fadetarget > fadeValue){
fadeValue += fadeSpeed;
analogWrite(pwm_pin, fadeValue);
if (fadeValue == 255) beep(100, 250);
// Serial.print("Feidojam LED uz: ");
Serial.println(fadeValue);
}
if (fadetarget < fadeValue){
fadeValue -= fadeSpeed;
analogWrite(pwm_pin, fadeValue);
if (fadeValue == minimumFade) beep(100, 250);
// Serial.print("Feidojam LED uz: ");
Serial.println(fadeValue);
}
}
void sledzis(){
sw = digitalRead(sw_pin);
if (sw == 0 && prevsw == 1){
beep(100, 250);
if (gaisma == LOW){
digitalWrite(rly_pin, HIGH);
gaisma = HIGH;
}else{
digitalWrite(rly_pin, LOW);
gaisma = LOW;
}
Serial.print("Gaismas statuss: ");
Serial.println(gaisma);
delay(200);
}
prevsw = sw;
}
void ledswitch(){
ledsw = digitalRead(ledsw_pin);
if (ledsw == 0 && prevledsw == 1){
beep(100, 250);
if (fadetarget == 0){
fadetarget = prevValue;
offtrg = 0;
Serial.println("Iesleedzam LED gaismas!");
}else{
fadetarget = 0;
offtrg = 1;
prevValue = fadeValue;
Serial.println("Izsleedzam LED gaismas!");
}
delay(200);
}
prevledsw = ledsw;
}
void getIR() {
detachInterrupt(0);
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 100)
{
int tempKey = getIRKey();
Serial.print("No pults sanjeemaam: ");
Serial.println(tempKey);
// if (tempKey > 0) beep(100, 250);
if(tempKey == 2320){ // ------------------- Gaismas on/off key
if (gaisma == LOW){
digitalWrite(rly_pin, HIGH);
gaisma = HIGH;
}else{
digitalWrite(rly_pin, LOW);
gaisma = LOW;
}
Serial.print("Gaismas statuss: ");
Serial.println(gaisma);
}
if(tempKey == 16){ // -------------------- LED Off key
if (fadetarget == 0){
fadetarget = prevValue;
offtrg = 0;
Serial.println("Iesleedzam LED gaismas!");
}else{
fadetarget = 0;
offtrg = 1;
prevValue = fadeValue;
Serial.println("Izsleedzam LED gaismas!");
}}
if(tempKey == 2064){ // ------------------ LED + key
if (offtrg == 1){
fadetarget = fadeValue;
offtrg = 0;
}
fadetarget = constrain((fadetarget + fadestep), minimumFade, 255);
Serial.print("LED gaismas liimenis: ");
Serial.println(fadetarget);
}
if(tempKey == 1040){ // ---------------- LED - key
if (offtrg == 1){
fadetarget = fadeValue;
offtrg = 0;
}
fadetarget = constrain((fadetarget - fadestep), minimumFade, 255);
Serial.print("LED gaismas liimenis: ");
Serial.println(fadetarget);
}
}
last_interrupt_time = interrupt_time;
attachInterrupt(0, getIR, LOW); // interrupt 0
}
void encoder(){
encoder_A = digitalRead(pin_A);
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
if(encoder_B) { // Clockwise
fadetarget = constrain((fadetarget + 10), minimumFade, 255);
} else { // Counterclockwise
fadetarget = constrain((fadetarget - 10), minimumFade, 255);
}
}
if((encoder_A) && (!encoder_A_prev)){
if(!encoder_B) { // Clockwise
fadetarget = constrain((fadetarget + 10), minimumFade, 255);
} else { // Counterclockwise
fadetarget = constrain((fadetarget - 10), minimumFade, 255);
}
}
encoder_A_prev = encoder_A; // Noglabaajam ieprieksheejo staavokli
}
int getIRKey() {
int data[12];
data[0] = pulseIn(ir_pin, LOW); //Saakam meeriit bitus
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);
digitalWrite(led_pin, LOW);
if(debug == 1) {
Serial.println("-----");
}
for(int i=0;i<=11;i++) { //Parse them
if (debug == 1) {
Serial.println(data[i]);
}
if(data[i] > bin_1) { //is it a 1?
data[i] = 1;
} else {
if(data[i] > bin_0) { //is it a 0?
data[i] = 0;
} else {
data[i] = 2; //Flag the data as invalid; I don't know what it is!
}
}
}
for(int i=0;i<=11;i++) { //Pre-check data for errors
if(data[i] > 1) {
return -1; //Return -1 on invalid data
}
}
int result = 0;
int seed = 1;
for(int i=11;i>=0;i--) { //Convert bits to integer
if(data[i] == 1) {
result += seed;
}
seed = seed * 2;
}
return result; //Return key number
}