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?
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.
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.
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);}
}
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.
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() ."
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);
}
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
}