M
M
Maikesteps2016-01-30 02:55:54
Arduino
Maikesteps, 2016-01-30 02:55:54

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);
}
}

The bottom line: by pressing a button on the remote control, change the glow mode of the RGB tape.
If the "chameleon" type mode is launched, then the exit from it is not instantaneous, but until the entire "cycle of colors" has passed.
How to make it instant?
So far, the only idea is to add another arduino (the simplest one). And what would she do with the arrival of the remote control code different from the one that came before her earlier, she would give out one to her output, which is connected to the interruption of the main arduini (or reset, for sure).
Maybe there are other options?
Video of work now:
1 - red (switches instantly)
2 - green (switches instantly)
3 - blue (switches instantly)
4 - chameleon (it takes a very long time to switch)
5 - variations of pink (switching from it is faster, but still long)
Here is such a long question =)
Thank you!
PS There are a lot of tutorials on the net on how to make an arduino a remote control, but there is no how to make an arduino an actuating device (well, you understand). Does it make sense to paint in the sandbox how to do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Gusev, 2016-01-30
@Maikesteps

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.

M
magazovski, 2016-01-30
@magazovski

there is a little extra, but now there is no particular need to clean up

You have daeay in a loop. This hangs your controller. You need to master the timer.
In general, you need it at the same time , and you consistently stuffed everything.
At its simplest, in the loop function you should read the button and run one step of the toggle loop. That is, make one external for , and already do the logic inside.

V
vanyamba-electronics, 2016-02-05
@vanyamba-electronics

Try using leOS - multitasking kernel for Arduino .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question