Loading...
Pages: [1]   Go Down
Author Topic: Breaking a loop with remote controller.  (Read 120 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone.

I have an idea about using a remote controller i had laying around to control a bunch of LED's.
The receiver is a tdl-9921. I did my homework and found the way to connect it to my Arduino Leonardo using the RCSwitch library.
At the moment i can make the arduino run a loop for each key i press in the remote. My goal is to interrupt that loop when a new key is pressed on the remote.
I read about interrupts on arduino, but since the voltage on the data pin of the receiver does not change when a key is pressed i need to go in another direction. Hope you can help me find it.

This is the code i have at the moment, modified from a RCSwitch example:
Code:
#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();
int led = 13;

void setup() {
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #3 on Leonardo
}

void loop() {
  if (mySwitch.available()) {
           
    if ( mySwitch.getReceivedValue() == 5592512) {
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led,LOW);
    } else
    if ( mySwitch.getReceivedValue() == 5592368) {
    for (int Count2=0; Count2 < 200; Count2++){
    digitalWrite(led,HIGH);
    delay(100);
    digitalWrite(led,LOW);
    delay(50);
    }                           
    } else
    if ( mySwitch.getReceivedValue() == 5592332) {
    //another type of blink goes here
    } else
    if ( mySwitch.getReceivedValue() == 5592323) {
    //and another one goes here
    }
   
    mySwitch.resetAvailable();

}

At this moment the code detects one key press, does whatever i put inside the if code and then stops, waiting for another key press.
For simplicity, lets just say that i want to be able to interrupt the blinking in the second if whenever i press another key in the controller.

Thanks.
Logged

UK
Offline Offline
Tesla Member
***
Karma: 89
Posts: 6391
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since you need to do multiple things concurrently (flash LEDs, receive commands from controller) you need to use a non-blocking approach. The blink without delay example sketch demonstrates how that can be achieved.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 96
Posts: 6376
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The way to make your sketch responsive to input is to NOT rely on delay() for timing.  During the delay() the Arduino is doing nothing useful.

You should look at the "BlinkWithoutDelay" example to see how to do timed things without the delay() function.

Each 'pattern' can be a separate function that is called over and over for as long as you want the pattern to run:
Code:
void pattern5592368() {
    static int state=0;
    static unsigned long startTime = 0;

    switch (state) {
    case 0:
        digitalWrite(led,HIGH);
        startTime = millis();
        state++;
        break;

    case 1:
        if (millis() - startTime > 100)
             state++;
        break;

    case 2:
        digitalWrite(led,LOW);
        startTime = millis();
        state++;
        break;

    case 3:
        if (millis() - startTime > 50)
             state = 0;  // Start the pattern over
        break;
    }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both for such fast responses.
I will look into "BlinkWithoutDelay" asap, and even try the code johnwasser kindly gave. I must confess that at this time i don't understand it fully, but i will read about it and learn.

Thanks again, and i will return on this when i have some kind of progress.  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, quick update on my side. I still can't use functions properly (hopefully i will get there) but in the mean time i decided to try something else.
Even though i understand why you pointed me out of the delay, and expect to get rid of it in the future, a small change in the code produced the effect i was expecting.

Code:
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
int led = 11;
int value = 0;
int value2 = 0;

void setup() {
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #3 on Leonardo
}
void loop() {
  if (mySwitch.available()) {
   
    int value = mySwitch.getReceivedValue();
    value2 = value;
   
    if ( mySwitch.getReceivedValue() == 5592512) {
    while (value2 == value){
    value2 = mySwitch.getReceivedValue();
    if (value2 != value) {
   break;
    }       
    digitalWrite(led,HIGH);
    delay(1000);
    digitalWrite(led,LOW);
    delay(500);
    }
    }
    else

Keep in mind that at this stage i don't care about the delay idling the Arduino as the delay is 1 second long max and as long as i press the button on the remote more than one second, it works.

It may not be the best practice in coding (i am just starting as you probably understand from my code), but it works.
Now it's time to optimize the code and learn about functions. Any push in the right direction is most appreciated.
Thanks.
Logged

Pages: [1]   Go Up
Print
 
Jump to: