Hi guys, i'm working on a project to control a peltier cell via PID using a IR remote receiver. But I have a problem decoding IR code. After a few loops the library stops to identify the codes and responds with the following message:
Attempting NEC decode:
Protocol failed because number of raw samples wrong.
With the example code all works fine.
The sensor is an ax-1838HS https://arduino-info.wikispaces.com/file/view/IR-Receiver-AX-1838HS.pdf/264668680/IR-Receiver-AX-1838HS.pdf
Can you help me ?
#include <IRLib.h>
#include <IRLibMatch.h>
#include <IRLibRData.h>
#include <IRLibTimer.h>
/*#include <IRremote.h>
//#include <IRremoteInt - Copia.h>
#include <IRremoteInt.h>*/
//#include <MemoryFree.h>
#include <CountUpDownTimer.h> //https://github.com/AndrewMascolo/CountUpDownTimer
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PID_v1.h>
//#include <dht11.h>
#include "DHT.h"
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif
//DHT11 settings
//dht11 DHT;
//#define DHT11_PIN 7
#define DHTPIN 7 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//IR remote
#define RECV_PIN 3
IRrecv My_Receiver(RECV_PIN);
IRdecodeNEC My_Decoder;
unsigned int Buffer[100];
/*
IBT-2 Motor Control Board driven by Arduino.
Speed and direction controlled by a potentiometer attached to analog input 0.
One side pin of the potentiometer (either one) to ground; the other side pin to +5V
Connection to the IBT-2 board:
IBT-2 pin 1 (RPWM) to Arduino pin 10(PWM)
IBT-2 pin 2 (LPWM) to Arduino pin 11(PWM)
IBT-2 pins 3 (R_EN), 4 (L_EN) to Arduino 5V pin
IBT-2 pins 7 (VCC),(GND), 5 (R_IS) and 6 (L_IS) not connected
*/
#define RPWM_Output 10 // Arduino PWM output pin 10; connect to IBT-2 pin 1 (RPWM)
#define LPWM_Output 9 // Arduino PWM output pin 9; connect to IBT-2 pin 2 (LPWM)
#define FAN_RELAY 8 // Relay to control fan switch ON/OFF connected to pin 8
#define FAN1 5 // PWM pin to control FAN1
#define FAN2 11 // PWM pin to control FAN2
//Timer management
CountUpDownTimer T(DOWN);
unsigned int Timer_Duration = 480; // runtime in minutes
boolean TimerPaused = false;
uint8_t check[8] = {0x0, 0x1, 0x3, 0x16, 0x1c, 0x8, 0x0};
uint8_t temperature[8] = //icon for termometer
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
uint8_t humidity[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
uint8_t play[8] = { //char for play
0b11000,
0b11100,
0b11110,
0b11111,
0b11111,
0b11110,
0b11100,
0b11000
};
uint8_t pause[8] = { //char for pause
0b11011,
0b11011,
0b11011,
0b11011,
0b11011,
0b11011,
0b11011,
0b11011
};
//byte bell[8] = { B00100, B01010, B01010, B01010, B10001, B11111, B00100, B00000 };
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
int Humidity;
//Specify the links and initial tuning parameters
const int Kp = 2, Ki = 5, Kd = 3;
//const int Kp = 50, Ki = 100, Kd = 10;
//double Kp=1, Ki=0.05, Kd=0.25;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, AUTOMATIC);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);
pinMode(FAN_RELAY, OUTPUT);
pinMode(FAN1, OUTPUT);
pinMode(FAN2, OUTPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.createChar(1, temperature);
lcd.createChar(2, humidity);
lcd.createChar(3, check);
lcd.createChar(4, play);
lcd.createChar(5, pause);
//set and start timer
T.SetTimer(Timer_Duration * 60);
T.StartTimer();
//irrecv.enableIRIn(); // Start the receiver
//irrecv.blink13(false);
My_Receiver.enableIRIn(); // Start the receiver
My_Decoder.UseExtnBuf(Buffer);
Setpoint = 28;
// Set PID Output Range
myPID.SetOutputLimits(-255, 255); // 1-255 = reverse, -255 -1 =forward
myPID.SetMode(AUTOMATIC);
// Enable DHT reading
dht.begin();
Serial.begin(9600);
}
void loop()
{
T.Timer(); // run the timer
Input = dht.readTemperature();
Humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(Input) ) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
digitalClockDisplay();
lcd.setCursor(12, 0);
if (TimerPaused == true)
{
lcd.printByte(5);
}
else
{
lcd.printByte(4);
}
lcd.setCursor(0, 1);
lcd.printByte(1);
lcd.print(int(Input));
lcd.setCursor(4, 1);
lcd.printByte(2);
lcd.print(Humidity);
lcd.setCursor(8, 1);
lcd.printByte(3);
lcd.print(int(Setpoint));
if (My_Receiver.GetResults(&My_Decoder)) {
//Restart the receiver so it can be capturing another code
//while we are working on decoding this one.
My_Receiver.resume();
My_Decoder.decode();
Serial.println(My_Decoder.value, HEX);
switch (My_Decoder.value)
{
case 0xFF906F:
Setpoint++;
break;
case 0xFFA857:
Setpoint--;
break;
case 0xFFC23D:
Timer_Duration+=10;
T.SetTimer(Timer_Duration * 60);
break;
case 0xFF02FD:
if (Timer_Duration >10)
{
Timer_Duration-=10;
T.SetTimer(Timer_Duration * 60);
}
break;
case 0xFF22DD:
if (TimerPaused == true)
{
TimerPaused = false;
T.StartTimer();
}
else
{
TimerPaused = true;
T.StopTimer();
}
break;
}
}
myPID.Compute();
if (Output < -41 )
{
digitalWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, abs(Output));
analogWrite(FAN1, abs(Output));
analogWrite(FAN2, abs(Output));
}
if (Output > 41 )
{
digitalWrite(LPWM_Output, abs(Output));
analogWrite(RPWM_Output, 0);
analogWrite(FAN1, abs(Output));
analogWrite(FAN2, abs(Output));
}
if ((Output > -40 && Output < 40) || (T.ShowMinutes() == 0 && T.ShowHours() == 0) )
{
digitalWrite(FAN_RELAY, LOW);
digitalWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, 0);
}
else
{
digitalWrite(FAN_RELAY, HIGH);
}
//analogWrite(FAN1, abs(Output));
//analogWrite(FAN2, abs(Output));
Serial.print (Setpoint);
Serial.print ("\t");
Serial.print (Input);
Serial.print("\t");
Serial.print(Humidity);
Serial.print("\t");
Serial.println(Output);
//Serial.print("\t");
//Serial.println(Timer_Duration);
delay(200);
}
void printDigits(byte digits) {
// utility function for digital clock display: prints preceding colon and leading 0
if (digits < 10)
lcd.print('0');
lcd.print(digits, DEC);
}
void digitalClockDisplay() {
lcd.home();
// digital clock display of current time
printDigits(T.ShowHours());
lcd.print(":");
printDigits(T.ShowMinutes());
lcd.print("/");
printDigits(int(Timer_Duration / 60));
lcd.print(":");
printDigits(Timer_Duration % 60 );
}