0

Break Loop - Trigger Effects Lights
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Break Loop - Trigger Effects Lights

by sinjinmanta on Sat Feb 09, 2019 8:11 pm

I've managed to replicate John Park's wonderful Trigger Effects Remote Control and receiver. It is excellent! Now I'm making some code modifications to fit needs, and I'm trying to find a way to break out of a loop.

Basically, once a receiver receives a Letter code which corresponds to a Menu and Button on the remote, it executes a particular part of code. In my case, it is going to be a brightness fade from low to full to low 5 times. I'd like to be able to stop it mid loop if I need to. I've tried adding in an additional if statement, but it doesn't seem to be doing it... I'm hoping someone might be able to see where I've gone wrong.

The part of the code with the break is:
Code: Select all | TOGGLE FULL SIZE
 if (buf[0]=='A'){ //crystal white fade
     
      for(int k = 0; k < 5; k++){ //k is the number of cycles the lights should fade up and down
    for (j = 10; j < 255; j++){ //j is the color value. This for statement is what incrementally increases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) {  //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
    if (buf[0]=='D'){// stop the loop
       pixels.show();
       radiopacket[8] = 'A';
  }
    }
 delay(500);
 
    for (j = 255; j > 5; j--) { //j is the color value. This for statement is what incrementally decreases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) { //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
  }
   delay(0);

   }
 pixels.show();
       radiopacket[8] = 'A';

       }



The full code I'm using is:

Code: Select all | TOGGLE FULL SIZE
//Ada_remoteFXTrigger_RX_NeoPixel
//Remote Effects Trigger Box Receiver
//by John Park
//for Adafruit Industries
//
// Button box receiver with NeoPixels
//
//
//MIT License

#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <RH_RF69.h>
#include <Wire.h>

#define LED 13



/********** NeoPixel Setup *************/
#define PIN            6  //this is the default, you can change if you adjust jumpers on the NeoPixel FeatherWing
#define NUMPIXELS      100
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 50; // delay


/************ Radio Setup ***************/

// Change to 434.0 or other frequency, must match RX's freq!
#define RF69_FREQ 915.0

  #define RFM69_CS      8
  #define RFM69_INT     3
  #define RFM69_RST     4



// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);


bool oldState = HIGH;
int showType = 0;

void setup() {
 
  delay(500);
  Serial.begin(115200);
 

 
  //while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer

  pixels.begin(); // This initializes the NeoPixel library.
  pixels.setBrightness(255);
  for(int i=0;i<42;i++){
    pixels.setPixelColor(i,0,0,0); //clear them
  }

  //pixels.setPixelColor(0,0,buf[3],0);
  pixels.show();


 





  pinMode(LED, OUTPUT); 
     
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, LOW);

  Serial.println("Feather RFM69 RX/TX Test!");

  // manual reset
  digitalWrite(RFM69_RST, HIGH);
  delay(10);
  digitalWrite(RFM69_RST, LOW);
  delay(10);
 
  if (!rf69.init()) {
    Serial.println("RFM69 radio init failed");
    while (1);
  }
  Serial.println("RFM69 radio init OK!");
 
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
  // No encryption
  if (!rf69.setFrequency(RF69_FREQ)) {
    Serial.println("setFrequency failed");
  }

  // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  // ishighpowermodule flag set like this:
  rf69.setTxPower(14, true);

  // The encryption key has to be the same as the one in the server
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  rf69.setEncryptionKey(key);
 
  pinMode(LED, OUTPUT);

  Serial.print("RFM69 radio @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");

  delay(500);
}


void loop(){
  uint16_t i, j, k;
 
 
  if (rf69.waitAvailableTimeout(100)) {
    // Should be a message for us now   
    uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
   
    if (! rf69.recv(buf, &len)) {
      Serial.println("Receive failed");
      return;
    }
   
    digitalWrite(LED, HIGH);
   
    rf69.printBuffer("Received: ", buf, len);
    buf[len] = 0;
   
    Serial.print("Got: "); Serial.println((char*)buf);
    Serial.print("RSSI: "); Serial.println(rf69.lastRssi(), DEC);

   
    char radiopacket[20] = "Button #";//prep reply message to send

    if (buf[0]=='A'){ //crystal white fade
     
      for(int k = 0; k < 5; k++){ //k is the number of cycles the lights should fade up and down
    for (j = 10; j < 255; j++){ //j is the color value. This for statement is what incrementally increases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) {  //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
    if (buf[0]=='D'){// stop the loop
       pixels.show();
       radiopacket[8] = 'A';
  }
    }
 delay(500);
 
    for (j = 255; j > 5; j--) { //j is the color value. This for statement is what incrementally decreases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) { //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
  }
   delay(0);

   }
 pixels.show();
       radiopacket[8] = 'A';

       }
if (buf[0]=='B'){ //crystal red fade
   for(int k = 0; k < 5; k++){ //k is the number of cycles the lights should fade up and down
    for (j = 10; j < 255; j++){ //j is the color value. This for statement is what incrementally increases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) {  //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, 0, 0);
    }
    pixels.show();
    delay(15);
  }
 delay(500);
 
    for (j = 255; j > 10; j--) { //j is the color value. This for statement is what incrementally decreases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) { //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, 0, 0);
    }
    pixels.show();
    delay(15);
  }
   delay(0);

   }
 pixels.show();
       radiopacket[8] = 'B';

       }

 else if (buf[0]=='D'){ //the letter sent from the button
        for(int i=0;i<42;i++){
          pixels.setPixelColor(i,0,0,0);
        }
       pixels.show();
       radiopacket[8] = 'D';   
    }

  else if (buf[0]=='L'){ //the letter sent from the button
        for(int i=0;i<42;i++){
          pixels.setPixelColor(i,0,0,0);
        }
       pixels.show();
       radiopacket[8] = 'L';   


     radiopacket[9] = 0;

    Serial.print("Sending "); Serial.println(radiopacket);
    rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
    rf69.waitPacketSent();

    digitalWrite(LED, LOW);
  }

}

}

sinjinmanta
 
Posts: 39
Joined: Sat Jun 14, 2014 4:09 pm

Re: Break Loop - Trigger Effects Lights

by adafruit_support_bill on Sun Feb 10, 2019 7:50 am

The part of the code with the break is:

I don't see any break statements at all in that code.

I'd like to be able to stop it mid loop if I need to

This series of guides shows how to write code that remains responsive to user input. Part 3 deals specifically with LED patterns.
https://learn.adafruit.com/multi-taskin ... 1/overview
https://learn.adafruit.com/multi-taskin ... 2/overview
https://learn.adafruit.com/multi-taskin ... 3/overview

adafruit_support_bill
 
Posts: 71774
Joined: Sat Feb 07, 2009 10:11 am

Re: Break Loop - Trigger Effects Lights

by sinjinmanta on Sun Feb 10, 2019 10:44 am

Sorry, copied the wrong version of the code... The break comes as the neopixels are going through the for statement that increases the pixel brightness. The idea was to put an additional if statement that would check the buffer for a code (in this case, the letter D), and then break the loop if the code received was D.

Code: Select all | TOGGLE FULL SIZE
    if (buf[0]=='A'){ //crystal white fade
     
      for(int k = 0; k < 5; k++){ //k is the number of cycles the lights should fade up and down
    for (j = 10; j < 255; j++){ //j is the color value. This for statement is what incrementally increases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) {  //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
    if (buf[0]=='D'){// stop the loop
       pixels.show();
       radiopacket[8] = 'A';
       break;
  }
    }
 delay(500);
 
    for (j = 255; j > 5; j--) { //j is the color value. This for statement is what incrementally decreases the color value by 1 in the setPixelColor below.
    for (i = 0; i < pixels.numPixels(); i++) { //i is the pixel number and this for statement increases by 1 in order to move on to the next pixel.
     pixels.setPixelColor(i, j, j, j);
    }
    pixels.show();
    delay(15);
  }
   delay(0);

   }
 pixels.show();
       radiopacket[8] = 'A';

       }


Using the serial monitor, I can see the buffer code {A or B} received by the receiver before the neopixels begin fading, however the receiver doesn't recognize a change in the buffer while the fade for condition is being executed. Immediately after the fade completes, the serial monitor does show the first code received during the fade and executes it... I was thinking that if I added a check of the buffer

Sidetrack: I believe that the radiopacket[number] line is a return code which gets sent to the receiver, however this never seems to get sent or received, at least I never see it when connected to the serial monitor.

Bill, I will read the multitasking pages. Hopefully something there will tell me how to clear or check the buffer. Thank you for the suggestion!

sinjinmanta
 
Posts: 39
Joined: Sat Jun 14, 2014 4:09 pm

Re: Break Loop - Trigger Effects Lights

by adafruit_support_bill on Sun Feb 10, 2019 1:57 pm

The value of buf[0] will not change while you are in the loop - unless you also call rf69.recv() to read what is out there.

adafruit_support_bill
 
Posts: 71774
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.