0

led code getting stuck in loop & cycling too fast
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Mon Sep 09, 2019 3:16 pm

adafruit_support_bill wrote:One approach you can try is to just set a boolean flag in your interrupt handler and check for the flag in your main loop. When you detect the flag in your loop, do what you need to do then reset it.

It won't matter if the flag gets set a dozen times before the loop comes around. It will just do what it needs to do then reset it.


I will absolutely look into that!! Thank you!

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Mon Sep 09, 2019 6:18 pm

If in the meantime if anyone wants to add thoughts or solutions, I would be eager to hear them. ( I am extremely new at coding and researching the interrupt handler Boolean flag is going pretty slow and my poor head is spinning.)

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Fri Sep 13, 2019 12:47 pm

I really feel my answer lies in one of the following code sections.

Code: Select all | TOGGLE FULL SIZE
for (;;) {
    startTime = micros();                                // Current time
    if ((startTime - prevTime) >= (1000000L / FPS)) {    // 1/30 sec elapsed?
      prevTime = startTime;
      break;                                     // Yes, go update LEDs
    }                                            // otherwise...

    if ((c_witch = serial.read()) == '!') {               // Received UART app input?
      while ((c_witch = serial.read()) < 0);              // Yes, wait for command byte

      switch (c_witch)

      {
        case 'B':                    // Button (Control Pad)
          if (readAndCheckCRC(255 - '!' - 'B', buf, 2) & (buf[1] == '1')) {
            buttonPress(buf[0]);      // Handle button-press message
          }
          break;

        case 'C':                   // Color Picker
          if (readAndCheckCRC(255 - '!' - 'C', buf, 3)) {
            color = pixels.Color(buf[4] / 4, buf[4] / 4, buf[4] / 4);
          }
          break;


Code: Select all | TOGGLE FULL SIZE
  boolean readAndCheckCRC(uint8_t sum, uint8_t *buf, uint8_t n) {
  for (int c;;) {
    while ((c = serial.read()) < 0);      // Wait for next byte
    if (!n--) return (c == sum);       // If CRC byte, we're done
    *buf++ = c;                        // Else store in buffer
    sum   -= c;                        // and accumulate sum
  }
}

void skipBytes(uint8_t n) {
  while (n--) {
    while (serial.read() < 0);
  }
}

void buttonPress(char c)
{


Maybe I could add an update line in there somewhere?

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by adafruit_support_bill on Fri Sep 13, 2019 1:07 pm

I really feel my answer lies in one of the following code sections.
\
Can you tell us what exactly the question is? I don't see any interrupt code there. Have you abandoned that approach?

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

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Fri Sep 13, 2019 2:05 pm

I haven't abandoned it. I just don't know what to do. I've tried an interrupt to the rx pin, that made the code unusable. I think the app was receiving lots of garbled info.

I guess my definitive question is: What code do I add to exit the rainbow cycle and where does it go?
(I've dug into the skull headphone and eeg sketch, but the grammar and approach seems too different from my code to apply it to my project. )

I know there is a readAndCheckCRC going on that is associated with the button press. Could I use the readAndCheckCRC as my interrupt? Maybe, I could use a readAndCheckCRC, if buttonpress. update activepattern?

(I don't understand boolean flags, but know there is some boolean code in my sketch.)

(I am so sorry for my coding ignorance. I have got to get myself into a guided class. Trying to self learn is not working very well at all.)

On a positive note: This is what my shoes looked like a year ago. https://www.instagram.com/p/BnNYhezArz3/ And they are pretty darn cool. :-)

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by adafruit_support_bill on Fri Sep 13, 2019 2:19 pm

I know there is a readAndCheckCRC going on that is associated with the button press. Could I use the readAndCheckCRC as my interrupt?

You need to define better what you are trying to do. Do you need to parse the serial input from the button press, or are you just looking for an interrupt to get you out of the loop?

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

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Fri Sep 13, 2019 2:36 pm

An interrupt (utilizing the bluefruit flora) to get me out of the loop would likely be perfect.

What code/s do I use and where do I use them?



(I want to be able to have the app buttons reasonably responsive. So when I press one of the arrow buttons it goes into rainbow mode but when I press one of the number buttons it changes to that.)

(I can not add an additional, external button to perform the interrupt.)

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by adafruit_support_bill on Fri Sep 13, 2019 3:17 pm

An interrupt (utilizing the bluefruit flora) to get me out of the loop would likely be perfect.

(I want to be able to have the app buttons reasonably responsive. So when I press one of the arrow buttons it goes into rainbow mode but when I press one of the number buttons it changes to that.)

So you are saying that you want both. You need to parse the buttons to know which one was pressed. You also want a way to interrupt the loop for an early exit.

Serial I/O already uses interrupts. So you don't need to have your own interrupt handler to set a flag. All you need to do is add a check in your loops to see if any new bytes have been received by the serial port: https://www.arduino.cc/en/Serial.Available

If there are bytes available, then ext from your loop so you can process them and change modes appropriately.

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

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Fri Sep 13, 2019 11:27 pm

I wish this was sinking in. :-(

Thank you for spending time trying to help me.

Cheers.

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Sat Sep 14, 2019 2:45 am

I tried adding an available.

The code didn't work to exit the rainbowcycle loop when a button was pressed, but it did at least compile. lol

Code: Select all | TOGGLE FULL SIZE
for (;;) {
    t = micros();                                // Current time
    if ((t - prevTime) >= (1000000L / FPS)) {    // 1/30 sec elapsed?
      prevTime = t;
      break;                                     // Yes, go update LEDs
    }                                            // otherwise...
    if ((c = ser.read()) == '!') {               // Received UART app input?
      while ((c = ser.read()) < 0);              // Yes, wait for command byte

         if (ser.available()> 0) {
          c = ser.read();
            ser.print(c);
      }

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by adafruit_support_bill on Sat Sep 14, 2019 6:59 am

You need to put the test for ser.available inside your rainbowcycle loop and exit from that loop if there are characters available. Then you can parse them as usual once you are out of rainbowcycle.

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

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Sat Sep 14, 2019 12:18 pm

Got any links to explain how to go about that?

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by adafruit_support_bill on Sat Sep 14, 2019 12:30 pm

Using the rainbow code from the examples in the neopixel library:

Code: Select all | TOGGLE FULL SIZE
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait)
{
    for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256)
    {
        for(int i=0; i<strip.numPixels(); i++)
        { // For each pixel in strip...
            if (ser.available()> 0)
            {
                return;  // exit now.  We have button presses to handle
            }
            int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
            strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
        }
        strip.show(); // Update strip with new contents
        delay(wait);  // Pause for a moment
    }
}

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

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Sat Sep 14, 2019 12:31 pm

Thank you very much. I'll try that. :-)

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Re: led code getting stuck in loop & cycling too fast

by dionnesimone on Sat Sep 14, 2019 2:11 pm

Man, oh man!!

Almost there adafruit_support_bill !!

It is now acknowledging the button press!!! :-) <3

The button press triggers the rainbow cycle to start over from the beginning of the rainbowcycle,
but does not revert to one of the other patterns.

Do I add a line in there about the animation pattern in the rainbow section or in the serial read section?

or maybe at the serial.print? (I don't use the term print anywhere else in my code.)

I uncommented the lines I tried, that compiled fine, but did not work.

Code: Select all | TOGGLE FULL SIZE
digitalWrite(CTS_PIN, LOW); // Signal to BLE, OK to send data!
  for (;;) {
    t = micros();                                // Current time
    if ((t - prevTime) >= (1000000L / FPS)) {    // 1/30 sec elapsed?
      prevTime = t;
      break;                                     // Yes, go update LEDs
    }                                            // otherwise...
    if ((c = ser.read()) == '!') {               // Received UART app input?
      while ((c = ser.read()) < 0);              // Yes, wait for command byte

        if (ser.available()> 0) {
          c = ser.read();
                        //pixels.clear ();
            ser.print(c);
                       //ser.print(animMode);
                       //pixels.clear ();
                      // switch (animMode);
      }


Code: Select all | TOGGLE FULL SIZE
 {
      uint16_t i, j;
         for(j=0; j<256; j++) { // cycles of all colors on wheel
            for(i=0; i< pixels.numPixels(); i++) {

              if (ser.available()>0)
            {
                return;
                   // pixels.clear ();
                   // switch (animMode);
            }
             
               pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
      }
      pixels.show();
        }
       }
      break;

dionnesimone
 
Posts: 25
Joined: Wed Feb 27, 2019 9:37 pm

Please be positive and constructive with your questions and comments.