Answer the question
In order to leave comments, you need to log in
Arduino programming (RF remote control, RF receiver, LED strips, RCSwitch). Why is switching slowly from cycles?
Hello. There is such a code (there is a little superfluous, but now there is no special need to clean it up):
UPD: I edit the code from time to time (I make it "beautiful" and "shorter", I remove the superfluous)
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const int kod1 = 5592323; //коди клавиш
const int kod2 = 5592332;
const int kod3 = 5592335;
const int kod4 = 5592368;
const int kod5 = 5592371;
const int kod6 = 5592380;
const int kod7 = 5592383;
const int kod8 = 5592512;
const int kod9 = 5592515;
const int kod10 = 5592524;
const int kod11 = 5592527;
const int kod12 = 5592560;
const int red = 12; //виходи на ленту
const int green = 11;
const int blue = 10;
int kod;
int i;
void setup() {
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(2, 1);
digitalWrite(5, 0);
Serial.begin(9600);
mySwitch.enableReceive(1); // приемник на пин2
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop() {
kod = mySwitch.getReceivedValue();
if (kod == kod1) {
analogWrite(red, 20);
analogWrite(green, 0);
analogWrite(blue, 0);
}
else if (kod == kod2) {
analogWrite(red, 0);
analogWrite(green, 20);
analogWrite(blue, 0);
}
else if (kod == kod3) {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 20);
}
else if (kod == kod4) {
analogWrite(green, 0);
for (i = 0; i < 30; i++)
{
analogWrite(red, 30);
analogWrite(blue, i);
delay(30);
}
analogWrite(blue, 30);
analogWrite(red, 30);
delay(300);
for (i = 30; i > 0; i--)
{
analogWrite(blue, 30);
analogWrite(red, i);
delay(30);
}
analogWrite(blue, 30);
delay(300);
for (i = 0; i < 30; i++)
{
analogWrite(blue, 30);
analogWrite(green, i);
delay(30);
}
analogWrite(green, 30);
analogWrite(blue, 30);
delay(300);
for (i = 30; i > 0; i--)
{
analogWrite(green, 30);
analogWrite(blue, i);
delay(30);
}
analogWrite(green, 30);
delay(300);
for (i = 0; i < 30; i++)
{
analogWrite(green, 30);
analogWrite(red, i);
delay(30);
}
analogWrite(green, 30);
analogWrite(red, 30);
delay(300);
for (i = 30; i > 0; i--)
{
analogWrite(red, 30);
analogWrite(green, i);
delay(30);
}
analogWrite(red, 30);
delay(300);
}
else if (kod == kod5) {
for (i = 1; i < 40; i++) {
analogWrite(red, 50);
analogWrite(green, 0);
analogWrite(blue, 10 + i);
delay(40);
}
analogWrite(red, 50);
analogWrite(green, 0);
analogWrite(blue, 49);
delay(150);
for (i = 40; i > 1; i--) {
analogWrite(red, 50);
analogWrite(green, 0);
analogWrite(blue, 10 + i);
delay(40);
}
analogWrite(red, 50);
analogWrite(green, 0);
analogWrite(blue, 11);
delay(150);
}
else if ((kod == kod6) || (kod == kod7) || (kod == kod8) || (kod == kod9)
|| (kod == kod10) || (kod == kod11) || (kod == kod12)) {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 0);
}
}
Answer the question
In order to leave comments, you need to log in
What happens when there is no signal?
As an option, something like this:
Add a condition check inside the loop. The algorithm is something like this:
Before starting the cycle, we fix the code by which we got to the condition.
In the loop, we periodically read the code from the receiver (for example, every 10 passes of the loop).
If the new read code is different from the one that got into the loop (and also from what is returned in the absence of a signal), then we do break to exit the loop early.
there is a little extra, but now there is no particular need to clean up
Try using leOS - multitasking kernel for Arduino .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question