trying to mix some codes together

hey guys i need some help with this all i want is when i send code via bluetooth to keep running that code until i send another code and not just run it once and stop
thanks

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>


#define PIN 6


Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial myTAH (10,11);

const int sampleWindow = 10; 
unsigned int sample;
int maximum = 110;
int peak;
int dotCount;
#define PEAK_FALL 4  

void setup() 
{
  myTAH.begin(9600);
  strip.begin();
  strip.show(); 
}

void loop() {
  


   if (myTAH.available()) 
   {

     
     
    int red = myTAH.parseInt(); 
    // do it again:
    int green = myTAH.parseInt(); 
    // do it again:
    int blue = myTAH.parseInt(); 
    
    int pattern = myTAH.parseInt(); 
    
   
    if(myTAH.read() == 'R')
  {
    if ( pattern == 1) 
    {
      colorWipe(strip.Color(red, green,blue), 25);
   
    }
    
     else if(pattern == 2)
     {
     
      // rainbow(20);
      rainbowCycle(20);
      //theaterChaseRainbow(50);
     }
     else if (pattern == 3) 
     {
     

  
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 100;

   
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(A0);
      if (sample < 1024)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   
   int led = map(peakToPeak, 0, maximum, 0, strip.numPixels()) -1;
   
   for(int i; i <= led; i++)
   {
     int color = map(i, 0, strip.numPixels(), 0, 255);
     strip.setPixelColor(i, Wheel(color)); 
     
   }
   
   for(int i = strip.numPixels() ; i > led; i--)
   {
     strip.setPixelColor(i, 0); 
     
   }
  strip.show();
  
  if(led > peak)  peak = led; // Keep 'peak' dot at top
   
   if(peak > 0 && peak <= strip.numPixels()-1) strip.setPixelColor(peak,Wheel(map(peak,0,strip.numPixels()-1,0,255)));

   strip.show();
   
// Every few frames, make the peak pixel drop by 1:

    if(++dotCount >= PEAK_FALL) { //fall rate 
      
      if(peak > 0) 
      {peak--;
      dotCount = 0;
     }
    }
   }
  }
 }
}



uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

I don't understand what you want to do but I see several pieces of blocking code using WHILE and FOR which will prevent loop() from repeating quickly. For non-blocking code use IF and allow loop() to do the repetition.

Also your myTAH.parseInt()s block the repetition of loop() - potentially for a very long time.

Have a look at these links for non-blocking code
Serial Input Basics
Several Things at a Time

...R

thanks for your help i did change a little in my code added some stuff but now i need help with audio1 ();
when i break it with ( if (myTAH.read() == 'R') break;) i have to run the code twice to give the new order 1 for breaking 1 for the new order can i bypass this and just send 1 time ? and its not responsive sometimes
i have to send the code 4 or 5 times to make it work
thanks for your help

#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
int left_channel = A0;
int right_channel = A1;
int leftvolmax = 1;
int rightvolmax = 1;
const int length = 60;
const byte half = length / 2;
byte nextcolor = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
SoftwareSerial myTAH (10, 11);

const int sampleWindow = 10; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
int maximum = 110;
int peak;
int dotCount;
#define PEAK_FALL 4  // Rate of peak falling dot

void setup()
{
  pinMode(left_channel , INPUT);
  pinMode(right_channel , INPUT);
  myTAH.begin(9600);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  strip.setBrightness(100);
}

void loop() {
  if (myTAH.available())
  {
    int red = myTAH.parseInt();
    // do it again:
    int green = myTAH.parseInt();
    // do it again:
    int blue = myTAH.parseInt();
    int pattern = myTAH.parseInt();
    if (myTAH.read() == 'R')
    {

      if  (pattern == 1)
      {
        colorWipe(strip.Color(red, green, blue), 25);
      }

      else if (pattern == 2)
      {
        rainbowCycle(20);
      }
      while (pattern == 3)
      {
        audio0 ();
        if (myTAH.read() == 'R') break;
      }
      while  (pattern == 4) {
        audio1 ();
        if (myTAH.read() == 'R') break;
      }
    }
  }
}
void audio1 () {

  int leftvolume = analogRead(left_channel) * 4;
  leftvolume = map(leftvolume, 0, 300, 0, half);

  int rightvolume = analogRead(right_channel) * 4;
  rightvolume = map(rightvolume, 0, 300, 0, half);

  //left side
  for (uint16_t i = 0; i < half; i++) {
    int leftbounds = map(i, 0, 150, 0, 255);
    if (i <= leftvolume)
    {
      strip.setPixelColor(half - i - 1, Color(leftbounds, 255 - leftbounds, 0));
      strip.setPixelColor(half - i - 1, Wheel(GetNextColor()));
    }
    else
    {
      strip.setPixelColor(half - i - 1, Color(0, 0, 0));
    }
    if (i == leftvolmax) {
      strip.setPixelColor(half - i - 1, Color(0, leftbounds, 255 - leftbounds));
      strip.setPixelColor(half - i - 1, Wheel(GetNextColor()));
    }
  }


  //right side
  for (uint16_t i = 0; i < half; i++) {
    int rightbounds = map(i, 0, 150, 0, 255);
    if (i <= rightvolume)
    {
      strip.setPixelColor(half + i, Color(rightbounds, 255 - rightbounds, 0));
      strip.setPixelColor(half + i, Wheel(GetNextColor()));
    }
    else
    {
      strip.setPixelColor(half + i, Color(0, 0, 0));
    }

    if (i == rightvolmax) {
      strip.setPixelColor(half + i, Color(0, rightbounds, 255 - rightbounds));
      strip.setPixelColor(half + i, Wheel(GetNextColor()));
    }
  }


  strip.show();


  delay(10);
}

int GetNextColor()
{
  nextcolor++;
  if (nextcolor > 255)
  {
    nextcolor = 0;
  }
  return nextcolor;
}

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

void audio0 () {

  unsigned long startMillis = millis(); // Start of sample window
  unsigned int peakToPeak = 0;   // peak-to-peak level

  unsigned int signalMax = 0;
  unsigned int signalMin = 100;

  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
    sample = ((analogRead(left_channel) + analogRead(right_channel)) / 2);
    if (sample < 1024)  // toss out spurious readings
    {
      if (sample > signalMax)
      {
        signalMax = sample;  // save just the max levels
      }
      else if (sample < signalMin)
      {
        signalMin = sample;  // save just the min levels
      }
    }
  }
  peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude

  int led = map(peakToPeak, 0, maximum, 0, strip.numPixels()) - 1;

  for (int i; i <= led; i++)
  {
    int color = map(i, 0, strip.numPixels(), 0, 255);
    strip.setPixelColor(i, Wheel(color));

  }

  for (int i = strip.numPixels() ; i > led; i--)
  {
    strip.setPixelColor(i, 0);

  }
  strip.show();

  if (led > peak)  peak = led; // Keep 'peak' dot at top

  if (peak > 0 && peak <= strip.numPixels() - 1) strip.setPixelColor(peak, Wheel(map(peak, 0, strip.numPixels() - 1, 0, 255)));

  strip.show();

  // Every few frames, make the peak pixel drop by 1:

  if (++dotCount >= PEAK_FALL) { //fall rate

    if (peak > 0) peak--;
    dotCount = 0;
  }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if (WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
    WheelPos -= 170;
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}
void rainbow(uint8_t wait) {
  uint16_t i, j;
  for (j = 0; j < 256; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
  for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, c); //turn every third pixel on
      }
      strip.show();
      delay(wait);
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0); //turn every third pixel off
      }
    }
  }
}
//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
    for (int q = 0; q < 3; q++) {
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
      }
      strip.show();
      delay(wait);
      for (int i = 0; i < strip.numPixels(); i = i + 3) {
        strip.setPixelColor(i + q, 0); //turn every third pixel off
      }
    }
  }
}

encor50:
when i break it with ( if (myTAH.read() == 'R') break;) i have to run the code twice to give the new order 1 for breaking 1 for the new order can i bypass this and just send 1 time ? and its not responsive sometimes
i have to send the code 4 or 5 times to make it work

I'm not surprised it is not responsive. You still have a huge amount of tidying up to do. Completely separate the business of receiving data from the business of deciding what to do with that data.

And I don't understand your question about break and "new order 1".

If this was my project my code in loop() would probably be like this

void loop() {
  receiveSerialData();
  colorWipe();
  rainbowCycle();
  audio0();
  audio1();
}

and in each of the action functions I would check if the circumstances are correct for it to operate based on the data that had been received. That way as soon as new data is received it would be acted on.

...R

i mean like when i run rainbowCycle() with sending 0,0,0,2R it will take 30 sec to finish the rainbowCycle then i can give it another command and not break the rainbowCycle and run the new code and when i run audio0 or 1 its the same i have to give it couple of times another command until it respond

encor50:
i mean like when i run rainbowCycle() with sending 0,0,0,2R it will take 30 sec to finish the rainbowCycle

While it may take 30 seconds you should be writing the code so it moves through the cycle in little pieces, each taking a few microsecs. That way you can check for new input in between the little pieces. Have you looked carefully at how the code in Planning and Implementing a Program works?

...R

yes but its pretty hard i am not a programmer im trying to learn but its not easy ! :\

encor50:
yes but its pretty hard i am not a programmer yet im trying to learn but its not easy ! :\

You forgot one word :slight_smile:

If there is something you don't understand in my example I will try to help if you tell me what it is.

...R