Need help to figure it out

Hello. I started to deal with the IR sensor with the arduno remote control. You need to make sure that when you press the remote control button, the LED starts flashing. I wrote the code, but I think it's wrong because the system doesn't work. Please tell me how to write this correctly?

#include "IRremote.h"
IRrecv irrecv(6);
decode_results results;
byte led_pin = 3;
int led_status = LOW;
unsigned long last_time;
void setup() {
  pinMode(3, OUTPUT);
  irrecv.enableIRIn(); // запускаем прием
}

void loop() {
  if ( irrecv.decode( &results )) {
    switch(results.value){
      case 0xFFA25D:
      if(millis() - last_time > 500){
        last_time = millis();
        if(led_status == LOW){
          led_status = HIGH;}
        else{
          led_status = LOW;}}
        break;}
  irrecv.resume(); 
  }
}

That's a bit vague. What does it do that it should not, or not do that it should?

Forget about doing this in "case 0xFFA25D:"

  if (millis() - last_time > 500) {
    last_time = millis();
    if (led_status == LOW) {
      led_status = HIGH;
    }
    else {
      led_status = LOW;
    }
  }

Use the serial monitor and see if you are even receiving from the IR transmitter. Just make it print "I see that!" or whatever.

Then you can start worrying about when and whether an LED should blink.

a7

if ( irrecv.decode( &results )) {
    switch(results.value){
      case 0xFFA25D:
      Serial.println(results.value);
      if(millis() - last_time > 500){
        last_time = millis();
        if(led_status == LOW){
          led_status = HIGH;}



16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245
16753245

That's what the port monitor outputs, that is, it receives a signal

OK good. BTW, you can

    Serial.print(results.value, HEX);

and see the number in HEX, FFA25D check!

Now do those numbers tumble out of your code continuously as you are holding the button, and stop when you stop transmitting?

The next challenge (!) is to simply turn ON and OFF the LED using the remote. The problem to overcome is the messages flying at you, so the trick is

   when you get a message
     if enough time has gone by 
         act on the message (toggle the LED, for example)
     always reset the "too soon" timer

In that way, messages coming soon after previous messages are ignored. The system does not become responsive until, say, 500 milliseconds has elapsed with no messages seen. That value for the lock-out period can be adjusted later. 500 def long enough and long enough so the response will be correct but annoyimg.

The code that was trying to make the LED blink shows the use of millis() to not do something if time has not passed. See if you can use that kind of logic for ignoring messages while I go to the beach. :wink:

Unless and until you

    digitalWrite(led_pin, led_status);

the LED will remain off or on - you must write the value to the pin the LED is on at some point.

And surprise! It looks like you might be successful if only you had done that, write to the LED!

But to learn more and be better off, rather than take the freebie and maybe even not know why or just how it works...

get the LED to merely go on and off with the remote control.

a7

How to write so that the LED blinks, I figured out. Now I am faced with the task of making sure that when another button is pressed, the LED starts to fade smoothly and light up. I decided to try also to register this part through the function, BUT it does not work! I'm just starting to program myself and I can't figure it out without your help... How to write this task? Thank you in advance!

#include <IRremote.h>

int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
int LED = 3;

int ledstate = LOW;
unsigned long lasttime = 0;
bool ledblink = false;
bool ledfading = false;
int val = 0;
int dir = true;

void setup(){
Serial.begin(9600);
irrecv.enableIRIn(); 
pinMode(LED, OUTPUT);
}

void loop(){
if (irrecv.decode(&results)){
  switch(results.value){
  case 0xFFA25D:
    ledblink = !ledblink;
    break;
  case 0xFF629D:
    ledfading = !ledfading;
    break;}
irrecv.resume();}
Serial.println(results.value, HEX);
  
if(ledblink){
  Flashled();}
if(ledfading){
  Fading();}}

void Flashled(){
  if(millis() - lasttime > 500){
    lasttime = millis();
    if(ledstate == LOW){
      ledstate = HIGH;}
    else ledstate = LOW;
    digitalWrite(LED, ledstate);}}

void Fading(){
  if(millis() - lasttime > 10){
      lasttime = millis();
      if (dir == 1)val++;
      else val --;
      if (val >= 255 || val <= 0) dir = !dir;      
      analogWrite(LED, val);}
}

That conveys no information that we can use to help you. The code does something, what is that? How is it different than what you want?

Excuse me. My system is like this, each button on the IR remote control is responsible for switching the LED mode, that is, I pressed 1, my LED started flashing, I pressed two LEDs began to fade smoothly and light up. Mode 1 works, the LED flashes, but when switching 2, LED flashes occur with an interval of 20 seconds. What could be the matter?

@arduino556665 you can start by formatting your code in a traditional manner. This will make it easier to read for everyone, and easier for you to spot the logical structural errors that may be the root cause.

At a glance everything looks good. I'm at the beach so cannot format it myself to motivate taking a closer look.

In the IDE use the Tools/Autoformat and see your code in one of the standard conventional formats.

You could then copy that and paste it here in code tags, or use the IDE "Copy for Forum" command and just paste here.

L8R I can take a better look through a bigger window, so to speak.

a7

Don't think you need to call pinMode() for an analog out pin. Try commenting that out. Yes, that interferes with your LED on, LED off approach; you may need to use analogWrite() for those as well.

Reference:

"You do not need to call pinMode() to set the pin as an output before calling analogWrite() ."

#include <IRremote.h>

int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
int LED = 3;

int ledstate = LOW;
unsigned long lasttime = 0;
bool ledblink = false;
bool ledfading = false;
int val = 0;
int dir = true;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  pinMode(LED, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0xFFA25D:
        ledblink = !ledblink;
        break;
      case 0xFF629D:
        ledfading = !ledfading;
        break;
    }
    irrecv.resume();
  }


  if (ledblink) {
    Flashled();
  }
  if (ledfading) {
    Fading();
  }
}

void Flashled() {
  if (millis() - lasttime > 500) {
    lasttime = millis();
    if (ledstate == LOW) {
      ledstate = HIGH;
    }
    else ledstate = LOW;
    digitalWrite(LED, ledstate);
  }
}

void Fading() {
  if (millis() - lasttime > 10) {
    lasttime = millis();
    if (dir == 1)val++;
    else val --;
    if (val >= 255 || val <= 0) dir = !dir;
    analogWrite(LED, val);
  }
}

No, try

#include <IRremote.h>

int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
int LED = 3;

int ledstate = LOW;
unsigned long lasttime = 0;
bool ledblink = false;
bool ledfading = false;
int val = 0;
int dir = true;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
 // pinMode(LED, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0xFFA25D:
        ledblink = !ledblink;
        break;
      case 0xFF629D:
        ledfading = !ledfading;
        break;
    }
    irrecv.resume();
  }


  if (ledblink) {
    Flashled();
  }
  if (ledfading) {
    Fading();
  }
}

void Flashled() {
  if (millis() - lasttime > 500) {
    lasttime = millis();
    if (ledstate == LOW) {
      ledstate = HIGH;
      analogWrite(LED, 255);
    }
    else {
      ledstate = LOW;
      analogWrite(LED, 0);
    }
  }
}

void Fading() {
  if (millis() - lasttime > 10) {
    lasttime = millis();
    if (dir == 1)val++;
    else val --;
    if (val >= 255 || val <= 0) dir = !dir;
    analogWrite(LED, val);
  }
}

No, everything remains the same, when you press the button 2 flashes with an interval of 20 seconds

You need a unique time stamp for each of the two functions.

Make changes to both like this

void Flashled() {

// unique and permanent to this function

  static unsigned long lasttime;

if (millis() - lasttime > 500) {
    lasttime = millis();
    if (ledstate == LOW) {
      ledstate = HIGH;
      analogWrite(LED, 255);
    }
    else {
      ledstate = LOW;
      analogWrite(LED, 0);
    }
  }
}

Get rid of the global lasttime variable it is not needed.

a7

That's it, it started working. But there was a problem when I want to switch from mode 3 to mode 2 or 1 nothing happens. The scheme works only in this order 1 (blinking), 2 (fading), 3 (it is also fading). That is, when I get to mode 3, it's the last one and I can't switch from it, maybe some variable should be updated here. What could be the matter?`

#include <IRremote.h>
int RECV_PIN = 6; 
IRrecv irrecv(RECV_PIN);
decode_results results;
int LED = 5;
int ledstate = LOW;
static unsigned long lasttime = 0;
bool ledblink = false;
bool ledfading = false;
bool automat = false;

int val = 0;
int dir = true;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  pinMode(LED, OUTPUT);
}

void loop() {

  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0xFFA25D: // mode1
        ledblink = !ledblink;
        break;

      case 0xFF629D: // mode2
        ledfading = !ledfading;
        break;

      case 0xFFE21D: // mode3
        automat = !automat;
        break;
    }




    Serial.println(results.value, HEX);


    irrecv.resume();
  }


  if (ledblink) {
    Flashled();
  }
  if (ledfading) {
    Fading();
  }
  if (automat) {
    Automatled();
  }
}

void Flashled() {
  if (millis() - lasttime > 500) {
    lasttime = millis();
    if (ledstate == LOW) {
      ledstate = HIGH;
    }
    else ledstate = LOW;
    digitalWrite(LED, ledstate);
  }
}

void Fading() {
  if (millis() - lasttime > 10) {
    lasttime = millis();
    if (dir == 1)val++;
    else val --;
    if (val >= 255 || val <= 0) dir = !dir;
    analogWrite(LED, val);
  }
}
void Automatled() {
  analogWrite(LED, (millis() / 10) % 256);
}

The code you wrote has no order, it responds to button presses in the switch statement.

IMO the code is not finished but you've got the worst fixed already.

in this way?

#include <IRremote.h>
int RECV_PIN = 6; 
IRrecv irrecv(RECV_PIN);
decode_results results;
int LED = 5;
int ledstate = LOW;

bool ledblink = false;
bool ledfading = false;
bool automat = false;

int val = 0;
int dir = true;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  pinMode(LED, OUTPUT);
}

void loop() {

  if (irrecv.decode(&results)) {
    switch (results.value) {
      case 0xFFA25D:
        ledblink = !ledblink;
        break;

      case 0xFF629D:
        ledfading = !ledfading;
        break;

      case 0xFFE21D:
        automat = !automat;
        break;
    }




    Serial.println(results.value, HEX);


    irrecv.resume();
  }


  if (ledblink) {
    Flashled();
  }
  if (ledfading) {
    Fading();
  }
  if (automat) {
    Automatled();
  }
}

void Flashled() {
  static unsigned long lasttime = 0;
  if (millis() - lasttime > 500) {
    lasttime = millis();
    if (ledstate == LOW) {
      ledstate = HIGH;
    }
    else ledstate = LOW;
    digitalWrite(LED, ledstate);
  }
}

void Fading() {
  static unsigned long lasttime = 0;
  if (millis() - lasttime > 10) {
    lasttime = millis();
    if (dir == 1)val++;
    else val --;
    if (val >= 255 || val <= 0) dir = !dir;
    analogWrite(LED, val);
  }
}
void Automatled() {
  analogWrite(LED, (millis() / 10) % 256);
}


I need the remote control to turn on the corresponding mode when randomly pressing any button

val -- is not val--

I think that's not the point

Just to test the choices logic, take input from the keyboard.

--- pseudocode ----
if ( Serial.available() )
{
read the key
switch ( key )
case '1'
change key to remote code for 1
break
case '2' and '3' do likewise
default
key = 0
}

}