trying to merge 2 sketches together and struggling

I currently have to run 2 arduino's to automate my lights and im looking to try to make them all work from one arduino. Im running a rgb shield using pins 3,5,6 and then ill be using a digital pin for the NeoPixle strip. im struggling to figure out how to combine the two codes and make them work. Is it possible?

NeoPixel code

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

#define NUM_LEDS 23

#define BRIGHTNESS 100

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);

byte neopix_gamma[] = {
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
    1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
    2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
    5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
   10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
   17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
   25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
   37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
   51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
   69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
   90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };


void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 50); // Red
  colorWipe(strip.Color(0, 255, 0), 50); // Green
  colorWipe(strip.Color(0, 0, 255), 50); // Blue
  colorWipe(strip.Color(0, 0, 0, 255), 50); // White

  whiteOverRainbow(20,75,5);  

  pulseWhite(5); 

  // fullWhite();
  // delay(2000);

  rainbowFade2White(3,3,1);


}

// Fill the dots one after the other with a color
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 pulseWhite(uint8_t wait) {
  for(int j = 0; j < 256 ; j++){
      for(uint16_t i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
        }
        delay(wait);
        strip.show();
      }

  for(int j = 255; j >= 0 ; j--){
      for(uint16_t i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
        }
        delay(wait);
        strip.show();
      }
}


void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) {
  float fadeMax = 100.0;
  int fadeVal = 0;
  uint32_t wheelVal;
  int redVal, greenVal, blueVal;

  for(int k = 0 ; k < rainbowLoops ; k ++){
    
    for(int j=0; j<256; j++) { // 5 cycles of all colors on wheel

      for(int i=0; i< strip.numPixels(); i++) {

        wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);

        redVal = red(wheelVal) * float(fadeVal/fadeMax);
        greenVal = green(wheelVal) * float(fadeVal/fadeMax);
        blueVal = blue(wheelVal) * float(fadeVal/fadeMax);

        strip.setPixelColor( i, strip.Color( redVal, greenVal, blueVal ) );

      }

      //First loop, fade in!
      if(k == 0 && fadeVal < fadeMax-1) {
          fadeVal++;
      }

      //Last loop, fade out!
      else if(k == rainbowLoops - 1 && j > 255 - fadeMax ){
          fadeVal--;
      }

        strip.show();
        delay(wait);
    }
  
  }



  delay(500);


  for(int k = 0 ; k < whiteLoops ; k ++){

    for(int j = 0; j < 256 ; j++){

        for(uint16_t i=0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
          }
          strip.show();
        }

        delay(2000);
    for(int j = 255; j >= 0 ; j--){

        for(uint16_t i=0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, strip.Color(0,0,0, neopix_gamma[j] ) );
          }
          strip.show();
        }
  }

  delay(500);


}

void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
  
  if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;

  int head = whiteLength - 1;
  int tail = 0;

  int loops = 3;
  int loopNum = 0;

  static unsigned long lastTime = 0;


  while(true){
    for(int j=0; j<256; j++) {
      for(uint16_t i=0; i<strip.numPixels(); i++) {
        if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
          strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
        }
        else{
          strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
        }
        
      }

      if(millis() - lastTime > whiteSpeed) {
        head++;
        tail++;
        if(head == strip.numPixels()){
          loopNum++;
        }
        lastTime = millis();
      }

      if(loopNum == loops) return;
    
      head%=strip.numPixels();
      tail%=strip.numPixels();
        strip.show();
        delay(wait);
    }
  }
  
}
void fullWhite() {
  
    for(uint16_t i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(0,0,0, 255 ) );
    }
      strip.show();
}


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

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

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3,0);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3,0);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0,0);
}

uint8_t red(uint32_t c) {
  return (c >> 16);
}
uint8_t green(uint32_t c) {
  return (c >> 8);
}
uint8_t blue(uint32_t c) {
  return (c);
}

RGB Fade code

// Rainbow color changing RGB leds example
// I am using common cathode RGB leds
int PIN_RED = 3;
int PIN_GREEN = 5;
int PIN_BLUE = 6;
int counter = 0;
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)



// Number of colors used for animating, higher = smoother and slower animation)
int numColors = 1300
;

 // The combination of numColors and animationDelay determines the
 // animation speed, I recommend a higher number of colors if you want
 // to slow down the animation. Higher number of colors = smoother color changing.
int animationDelay = 5; // number milliseconds before RGB LED changes to next color

void setup() {
 pinMode(PIN_RED, OUTPUT);
 pinMode(PIN_BLUE, OUTPUT);
 pinMode(PIN_GREEN, OUTPUT);
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
 

void loop() {
 // This part takes care of displaying the
 // color changing in reverse by counting backwards if counter
 // is above the number of available colors  
 float colorNumber = counter > numColors ? counter - numColors: counter;
 
 // Play with the saturation and brightness values
 // to see what they do
 float saturation = 1; // Between 0 and 1 (0 = gray, 1 = full color)
 float brightness = (outputValue/1023.); // Between 0 and 1 (0 = dark, 1 is full brightness)
 float hue = (colorNumber / float(numColors)) * 360; // Number between 0 and 360
 long color = HSBtoRGB(hue, saturation, brightness); 
 
 // Get the red, blue and green parts from generated color
 int red = color >> 16 & 255;
 int green = color >> 8 & 255;
 int blue = color & 255;

 setColor(red, green, blue);
 
 // Counter can never be greater then 2 times the number of available colors
 // the colorNumber = line above takes care of counting backwards (nicely looping animation)
 // when counter is larger then the number of available colors
 counter = (counter + 1) % (numColors * 2);
 
 // If you uncomment this line the color changing starts from the
 // beginning when it reaches the end (animation only plays forward)
 // counter = (counter + 1) % (numColors);

 delay(animationDelay);

// read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 1023);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue/1023.);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

void setColor (unsigned char red, unsigned char green, unsigned char blue) 
{        
   analogWrite(PIN_RED, red);
   analogWrite(PIN_GREEN, green);
   analogWrite(PIN_BLUE, blue);
} 

long HSBtoRGB(float _hue, float _sat, float _brightness) {
   float red = 0.0;
   float green = 0.0;
   float blue = 0.0;
   
   if (_sat == 0.0) {
       red = _brightness;
       green = _brightness;
       blue = _brightness;
   } else {
       if (_hue == 360.0) {
           _hue = 0;
       }

       int slice = _hue / 60.0;
       float hue_frac = (_hue / 60.0) - slice;

       float aa = _brightness * (1 - _sat);
       float bb = _brightness * (1 - _sat * hue_frac);
       float cc = _brightness * (1 - _sat * (1 - hue_frac));
       
       switch(slice) {
           case 0:
               red = _brightness;
               green = cc;
               blue = aa;
               break;
           case 1:
               red = bb;
               green = _brightness;
               blue = aa;
               break;
           case 2:
               red = aa;
               green = _brightness;
               blue = cc;
               break;
           case 3:
               red = aa;
               green = bb;
               blue = _brightness;
               break;
           case 4:
               red = cc;
               green = aa;
               blue = _brightness;
               break;
           case 5:
               red = _brightness;
               green = aa;
               blue = bb;
               break;
           default:
               red = 0.0;
               green = 0.0;
               blue = 0.0;
               break;
       }
   }

   long ired = red * 255.0;
   long igreen = green * 255.0;
   long iblue = blue * 255.0;
   
   return long((ired << 16) | (igreen << 8) | (iblue));
}

This is the code ive tried to morph together and its screaming at me. Am I going about this wrong? Im not asking for someone to do it for me but to help guide me and better my skills. the back story on this setup is the RGB is mood lighting for the hot tub mounted up in the trees and the NeoPixel is mounted in the factory light hole in the hot tub. Any and all help is much appreciated. I cant post the code directly because its more than 9000 characters

rgbandneopixle.ino (11 KB)

tatersalad:
This is the code ive tried to morph together and its screaming at me.

Is the screaming intelligible? I'd guess you're getting at least one error due to using pin 6 for both sketches. Will the Neo let you choose another pin?

You can quickly copy/paste the error message(s) to a forum post thusly:

Arduino IDE error capture.PNG

Merging code tutorial

Arduino IDE error capture.PNG

I used pin 13 in my combined sketch for the NeoPixel. Here is my list of error messages. Im still very new to arduino and learning without someone over your shoulder is a little difficult so forgive my doofis self.

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

rgbandneopixle:319: error: 'HSBtoRGB' was not declared in this scope

  long color = HSBtoRGB(hue, saturation, brightness); 

                                                   ^

rgbandneopixle:322: error: 'int red' redeclared as different kind of symbol

  int red = color >> 16 & 255;

      ^

C:\Users\Home\Desktop\rgbandneopixle\rgbandneopixle.ino:293:9: note: previous declaration 'uint8_t red(uint32_t)'

 uint8_t red(uint32_t c) {

         ^

rgbandneopixle:323: error: 'int green' redeclared as different kind of symbol

  int green = color >> 8 & 255;

      ^

C:\Users\Home\Desktop\rgbandneopixle\rgbandneopixle.ino:296:9: note: previous declaration 'uint8_t green(uint32_t)'

 uint8_t green(uint32_t c) {

         ^

rgbandneopixle:324: error: 'int blue' redeclared as different kind of symbol

  int blue = color & 255;

      ^

C:\Users\Home\Desktop\rgbandneopixle\rgbandneopixle.ino:299:9: note: previous declaration 'uint8_t blue(uint32_t)'

 uint8_t blue(uint32_t c) {

         ^

rgbandneopixle:326: error: expected constructor, destructor, or type conversion before '(' token

  setColor(red, green, blue);

          ^

rgbandneopixle:331: error: 'counter' does not name a type

  counter = (counter + 1) % (numColors * 2);

  ^

rgbandneopixle:337: error: expected constructor, destructor, or type conversion before '(' token

  delay(animationDelay);

       ^

rgbandneopixle:340: error: 'sensorValue' does not name a type

   sensorValue = analogRead(analogInPin);

   ^

rgbandneopixle:342: error: 'outputValue' does not name a type

   outputValue = map(sensorValue, 0, 1023, 0, 1023);

   ^

rgbandneopixle:344: error: expected constructor, destructor, or type conversion before '(' token

   analogWrite(analogOutPin, outputValue);

              ^

rgbandneopixle:347: error: 'Serial' does not name a type

   Serial.print("sensor = ");

   ^

rgbandneopixle:348: error: 'Serial' does not name a type

   Serial.print(sensorValue);

   ^

rgbandneopixle:349: error: 'Serial' does not name a type

   Serial.print("\t output = ");

   ^

rgbandneopixle:350: error: 'Serial' does not name a type

   Serial.println(outputValue/1023.);

   ^

rgbandneopixle:354: error: expected constructor, destructor, or type conversion before '(' token

   delay(2);

        ^

rgbandneopixle:355: error: expected declaration before '}' token

 }

 ^

exit status 1
'HSBtoRGB' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I won't pretend to be knowledgeable about error messages and, the compiler can be cryptic but, this,

rgbandneopixle:322: error: 'int red' redeclared as different kind of symbol

int red = color >> 16 & 255;

^

is straightforward. A search on 'red' will turn up these two lines:

int red = color >> 16 & 255;

and

float red = 0.0;

red cannot be a float and an int at the same time.

There's also this:

void setColor (unsigned char red, unsigned char green, unsigned char blue)

but I don't know if it's relevant. The same errors exist for green and blue.

My suggestion, and a better one will likely come along, is to rename all the reds, etc. in each original sketch to be able to distinguish them from each other - maybe redNeo and redRGB. There may also be conflicts in the library files (.cpp & .h)

Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

rgbandneopixle:319: error: 'HSBtoRGB' was not declared in this scope

  long color = HSBtoRGB(hue, saturation, brightness); 

                                                   ^

rgbandneopixle:326: error: expected constructor, destructor, or type conversion before '(' token

  setColor(red, green, blue);

          ^

rgbandneopixle:331: error: 'counter' does not name a type

  counter = (counter + 1) % (numColors * 2);

  ^

rgbandneopixle:337: error: expected constructor, destructor, or type conversion before '(' token

  delay(animationDelay);

       ^

rgbandneopixle:340: error: 'sensorValue' does not name a type

   sensorValue = analogRead(analogInPin);

   ^

rgbandneopixle:342: error: 'outputValue' does not name a type

   outputValue = map(sensorValue, 0, 1023, 0, 1023);

   ^

rgbandneopixle:344: error: expected constructor, destructor, or type conversion before '(' token

   analogWrite(analogOutPin, outputValue);

              ^

rgbandneopixle:347: error: 'Serial' does not name a type

   Serial.print("sensor = ");

   ^

rgbandneopixle:348: error: 'Serial' does not name a type

   Serial.print(sensorValue);

   ^

rgbandneopixle:349: error: 'Serial' does not name a type

   Serial.print("\t output = ");

   ^

rgbandneopixle:350: error: 'Serial' does not name a type

   Serial.println(outputValue/1023.);

   ^

rgbandneopixle:354: error: expected constructor, destructor, or type conversion before '(' token

   delay(2);

        ^

rgbandneopixle:355: error: expected declaration before '}' token

 }

 ^

exit status 1
'HSBtoRGB' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

That looks to have fixed a couple errors. This compiler makes my head hurt. I can write ladder logic like no tomorrow but this code makes me want to take up drinking. i attached the new updated code for anyone following along.

rgbandneopixle.ino (11.1 KB)

im struggling to figure out how to combine the two codes and make them work. Is it possible?

Yes it is possible but it requires a lot of work. Those two programs are written as blocking code. That is you can’t ever combine the two as written because when you use the delay call in one code it also stops the other code from running.

Both effects must be rewritten as a state machine without using delay and for loops that contain delays.

This problem has been tackled / explained many hundreds of times. There is even an example of how to do this sort of thing with the blink without delay example in the IDE itself.

Things to see
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

https://forum.arduino.cc/index.php?topic=503368.0

Grumpy_Mike:
Yes it is possible but it requires a lot of work. Those two programs are written as blocking code. That is you can’t ever combine the two as written because when you use the delay call in one code it also stops the other code from running.

Both effects must be rewritten as a state machine without using delay and for loops that contain delays.

This problem has been tackled / explained many hundreds of times. There is even an example of how to do this sort of thing with the blink without delay example in the IDE itself.

Things to see
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum

Ok I'll just keep them on seperate arduino's then and make life easy