So, here the problem comes. I am trying to make a hybrid control of LEDs, using pushbuttons and IR remote. But, after some time of clicking the buttons on the IR remote, the receiver stops working, until I press one of the pushbuttons so it starts working again. I tried adding delay for stability (50 milliseconds), but it even made things worse. The pushbuttons started giving HIGH-LOW values randomly. Also, the same thing happens if I remove "delay (15);" in some of the last lines of code. I am suspecting the compiler is doing something wrong. I am using Arduino Mega 2560. Any help is appreciated.
#define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
#include <IRremote.h>
const short led [2] = {2, 3};
short led_brightness [ELEMENTS(led)];
short go_up [ELEMENTS(led)];
const short interval = 15;
const short button [ELEMENTS(led)] = {5, 6};
const byte IR_RECEIVE_PIN = 8;
const String ir_button [ELEMENTS(led)] = {"45", "46"};
void setup() {
Serial.begin(115200);
for (short i; i < ELEMENTS(led); i++) {
pinMode(led [i], OUTPUT);
}
for (short i; i < ELEMENTS(button); i++) {
pinMode(button [i], INPUT_PULLUP);
}
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void loop() {
for (short i = 0; i < ELEMENTS(led); i++) {
if (buttonstate(i) == 1) {
if (led_brightness [i] == 0) {
go_up [i] = 1;
}
if (led_brightness [i] < 255 && go_up [i] == 1) {
led_brightness [i] += interval;
} else {
led_brightness [i] -= interval;
go_up [i] = 0;
}
analogWrite(led [i], led_brightness [i]);
}
}
}
short buttonstate(const short input) {
if (digitalRead(button [input]) == LOW) {
while (digitalRead(button [input]) == LOW) {
}
delay(15);
return 1;
}
if (IrReceiver.decode()) {
String received = String(IrReceiver.decodedIRData.command, HEX);
received.trim();
IrReceiver.resume();
if (ir_button [input] == received) {
return 1;
}
}
return 0;
}
For one thing, led is an array, but you did not index the led array, so who knows what this is supposed to do.
@Paul_KD7HB I don't understand. Look what I wrote.
const short led [2] = {2, 3};
EDIT:
I think you are confused and you also confused me.
So,
ELEMENTS(led)
It is checking how much elements are there in an array, so no need (actually "forbidden") to index it.
#define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
I found it on the Arduino forum.
@ruilviana @UKHeliBob Please help if you can.
Hi
Please post a schematic of your project, (No fritzing),
(including the IR transmitter) .
@ruilviana So, should I take a picture?
Hi
make a freehand schematic and take a picture.
@ruilviana Sorry, I'm bad at drawing. I just took a "normal" picture. I am not using a IR transmitter, I am using a receiver and IR remote.
Hi
What is your IR model and can you tell what value it sends to the receiver?
@ruilviana The IR remote is unbranded, from Wavgat store on AliExpress. The receiver came in the box with it. The values for buttons 1 and 2 that I want to use are (as stated in the code): 45 and 46.
EDIT: Why are you asking so much about the hardware? I think it's problem in the software.
Hi
Please run this code and see what it prints on the serial monitor
after "received", by pressing each IR button.
And post the printout here.
#define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))
#include <IRremote.h>
const short led [2] = {2, 3};
short led_brightness [ELEMENTS(led)];
short go_up [ELEMENTS(led)];
const short interval = 15;
const short button [ELEMENTS(led)] = {5, 6};
const byte IR_RECEIVE_PIN = 8;
const String ir_button [ELEMENTS(led)] = {"45", "46"};
//const String ir_button [ELEMENTS(led)] = {"10", "11"};
//-------------------------------------------------------------
void setup() {
Serial.begin(115200);
// Serial.print("ELEMENTS(led) "); Serial.println(ELEMENTS(led));
for (short i; i < ELEMENTS(led); i++)
{
pinMode(led[i], OUTPUT);
// Serial.print("led[i] "); Serial.println(led[i]);
}
// Serial.print("ELEMENTS(button "); Serial.println(ELEMENTS(button));
for (short i; i < ELEMENTS(button); i++)
{
pinMode(button[i], INPUT_PULLUP);
// Serial.print("button[i] "); Serial.println(button[i]);
}
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
//-------------------------------------------------------------
void loop() {
for (short i = 0; i < ELEMENTS(led); i++)
{
if (buttonstate(i) == 1) //
{
// Serial.print("led_brightness[i] "); Serial.println(led_brightness[i]);
if (led_brightness[i] == 0)
{
go_up [i] = 1;
}
// Serial.print("go_up [i] "); Serial.println(go_up [i]);
if (led_brightness [i] < 255 && go_up [i] == 1)
{
led_brightness [i] += interval;
}
else
{
led_brightness [i] -= interval;
go_up [i] = 0;
}
analogWrite(led [i], led_brightness [i]);
}
}
}
//-------------------------------------------------------------
short buttonstate(const short input) {
if (digitalRead(button [input]) == LOW)
{
// Serial.print("input "); Serial.println(input);
while (digitalRead(button [input]) == LOW) {}
delay(15);
return 1;
}
if (IrReceiver.decode())
{
String received = String(IrReceiver.decodedIRData.command, HEX);
Serial.print("received "); Serial.println(received);
received.trim();
IrReceiver.resume();
if (ir_button [input] == received)
{
return 1;
}
}
return 0;
}
@ruilviana
received 45
received 45
received 46
received 46
received 45
received 45
received 45
received 46
received 46
received 46
received 0
received 0
received 0
...and then it stops working
I think this is all we need to know something is wrong with the code.
EDIT:
Now it works "normally" again, I don't know what is going on. I mean instead of "received 0" and stopping, it says "received 47". But still, after a while of pressing the buttons, it stops receiving codes, until I press one of the pushbuttons.
@ruilviana Now I tried with my TV remote and it's working fine. Probably the 0.50$ IR remote was "drunk". Sorry for wasting your time. Just one more request, please check if the code is working flawlessly with your hardware, if you didn't already.
And... Thanks!
EDIT: But again, it isn't the remote, and it wouldn't make sense. TV remote now has the same issue. I don't know how will I get around this issue.
kkkkkk
Hi
I've set up hardware just like yours (but with a mini) and I'm trying to see what's wrong.
My IR generates 14 15 and 0.
Hi
OK, I tested it and it crashed.
I'll see if I can find out why.
@ruilviana Thanks a lot. I made a video explanation but you won't need it now because you found out what is the "crash" I was talking about.
@ruilviana Oh, what do you mean by "just like mine"? The Mini doesn't have PWM for the LEDs on the same pins as on Mega.
Yes,, I have changed this pins for test.
@ruilviana Ok then. Good luck
.
Hi
I'm suspicious that there's a problem with the library
"IRremote.h".
Do you know which version of your IRremote.h library?
@ruilviana I downloaded it from the library manager, the newest version (3.5.2).