Combining multiple codes into a single program

Currently new to arduino programming.I am currently trying to add the same codes twice but using a different pin number to make the led strip lights to light up ( currently im using pin D4 but i want to add another pin number D3).However , i can't seem to make the second strip light light up.Anything wrong with my code.

Board used: Wemos D1(ESP2866)
Led Strip lights used: WS2812B

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

#include <Adafruit_NeoPixel.h>

#include <Fader.h>
#include <LedFunction.h>
#include <LedStates.h>
#include <PinStates.h>
#include <RainbowFunction.h>
#include <RF.h>
#include <SimpleRGBFunction.h>
#include <WaveFunction.h>



const char* ssid = "Redmi";
const char* password = "deyi1234";

ESP8266WebServer server(80);

const int LED_PIN = D4;
const int LED_PIN2= D3;


const int LED_COUNT = 200 ;
const int LED_COUNT1 = 200;


const int RF_OSC = 200;
const int RF_OSC1 = 200;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(LED_COUNT1, LED_PIN2, NEO_GRB + NEO_KHZ800);

LedStates currentLedStates(strip);
LedStates targetLedStates(strip);
Fader<LedStates> ledFader(currentLedStates, targetLedStates);
PinStates currentPinStates;
PinStates targetPinStates;
Fader<PinStates> pinFader(currentPinStates, targetPinStates);


LedStates currentLedStates1(strip1);
LedStates targetLedStates1(strip1);
Fader<LedStates> ledFader1(currentLedStates1, targetLedStates1);
PinStates currentPinStates1;
PinStates targetPinStates1;
Fader<PinStates> pinFader1(currentPinStates1, targetPinStates1);



void handleRoot() {
  String message = "<html><head></head><body style='font-family: sans-serif; font-size: 12px'>Following functions are available:<br><br>";
  message += "<a href='/APU?r=255&g=32&b=10&fade=5000'>/APU</a> a slow wave animation on LED on base color specified by arguments: r=<0..255> g=<0..255> b=<0..255><br>";
  message += "<a href='/FuelTransfer?r=255&g=32&b=10&fade=5000'>/FuelTransfer</a> a slow wave animation on LED on base color specified by arguments: r=<0..255> g=<0..255> b=<0..255><br>";
  message += "<a href='/ledsoff?fade=500'>/ledsoff</a> turns off LEDs<br>";
  server.send(200, "text/html", message);
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

int getArgValue(String name)
{
  for (uint8_t i = 0; i < server.args(); i++)
    if(server.argName(i) == name)
      return server.arg(i).toInt();
  return -1;
}

bool checkFadeAndSetLedFunction(LedFunction *f)
{
  int fade = getArgValue("fade");
  if(fade > -1)
  {
    targetLedStates.setFunction(f);
    ledFader.start(fade);
  }
  else
    currentLedStates.setFunction(f);  
}

bool checkFade1AndSetLedFunction1(LedFunction *f)
{
  int fade1 = getArgValue("fade");
  if(fade1 > -1)
  {
    targetLedStates1.setFunction(f);
    ledFader1.start(fade1);
  }
  else
    currentLedStates1.setFunction(f);  
}

void handleRf()
{
  const int pinNumbers[] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
  int pin = getArgValue("D");
  int t = getArgValue("t");
  if(t == -1) t = RF_OSC;
  int id = getArgValue("id");
  int ch = getArgValue("channel");
  int on = getArgValue("on");
  String out = "rf D";
  out += pin;
  out += " ";
  out += t;
  out += " ";
  out += id;
  out += " ";
  out += ch;
  out += " ";
  out += on;
  pinMode(pinNumbers[pin], OUTPUT);
  for(int i = 0; i < 5; i++)
    rfWriteCode(pinNumbers[pin], t, id, (1 << (ch + 1)) | (on > 0? 1: 0));
  server.send(200, "text/plain", out);  
}

void handleRf1()
{
  const int pinNumbers[] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
  int pin = getArgValue("D");
  int t = getArgValue("t");
  if(t == -1) t = RF_OSC1;
  int id = getArgValue("id");
  int ch = getArgValue("channel");
  int on = getArgValue("on");
  String out = "rf D";
  out += pin;
  out += " ";
  out += t;
  out += " ";
  out += id;
  out += " ";
  out += ch;
  out += " ";
  out += on;
  pinMode(pinNumbers[pin], OUTPUT);
  for(int i = 0; i < 5; i++)
    rfWriteCode(pinNumbers[pin], t, id, (1 << (ch + 1)) | (on > 0? 1: 0));
  server.send(200, "text/plain", out);  
}

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  //find it as http://lights.local
  /*if (MDNS.begin("lights")) 
  {
    Serial.println("MDNS responder started");
  }*/
  
  server.on("/", handleRoot);

 

  server.on("/APU", [](){                    
    server.send(200, "text/plain", "APU");
    WaveFunction *f = new WaveFunction();
    f->init(server);
    checkFadeAndSetLedFunction(f);
  });

  server.on("/FuelTransfer", [](){                    
    server.send(200, "text/plain", "Fuel Transfer");
    WaveFunction *f = new WaveFunction();
    f->init(server);
    checkFade1AndSetLedFunction1(f);
  });

  server.on("/ledsoff", [](){
    server.send(200, "text/plain", "ledsoff");
    checkFadeAndSetLedFunction(new SimpleRGBFunction());
  });
  server.on("/ledsoff1", [](){
    server.send(200, "text/plain", "ledsoff1");
    checkFade1AndSetLedFunction1(new SimpleRGBFunction());
  });


  server.on("/rf", handleRf);

  server.on("/rf", handleRf1);
  
  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop(void)
{
  server.handleClient();
  //MDNS.update();
  currentLedStates.render();
  if(ledFader.active)
    targetLedStates.render();
  if(!ledFader.fade())
    currentLedStates.commit();
  pinFader.fade();

   server.handleClient();
  //MDNS.update();
  currentLedStates1.render();
  if(ledFader1.active)
    targetLedStates1.render();
  if(!ledFader1.fade())
    currentLedStates1.commit();
  pinFader1.fade();
}

Try this: CombiningArduinoSketches - ArduinoInfo

1 Like

does that method apply to two of the same sketches ?

ok so i have tried following the guide that you showed but it still doesn't light up my 2nd led strip.Did i do anything wrong with my code?

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>


#include <Adafruit_NeoPixel.h>

#include "PinStates.h"
#include "LedStates.h"
#include "Fader.h"
#include "RainbowFunction.h"
#include "SimpleRGBFunction.h"
#include "WaveFunction.h"
#include "RF.h"

const char* ssid = "Redmi";
const char* password = "deyi1234";

ESP8266WebServer server(80);


const int LED_PIN = D4;
const int LED_COUNT = 300;

const int RF_OSC = 200;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

LedStates currentLedStates(strip);
LedStates targetLedStates(strip);
Fader<LedStates> ledFader(currentLedStates, targetLedStates);
PinStates currentPinStates;
PinStates targetPinStates;
Fader<PinStates> pinFader(currentPinStates, targetPinStates);


void handleRoot() {
  String message = "<html><head></head><body style='font-family: sans-serif; font-size: 12px'>Following functions are available:<br><br>";
  message += "<a href='/wave?r=255&g=32&b=10&fade=5000'>/wave</a> a slow wave animation on LED on base color specified by arguments: r=<0..255> g=<0..255> b=<0..255><br>";
  message += "<a href='/ledsoff?fade=500'>/ledsoff</a> turns off LEDs<br>";
  message += "<a href='/wave?r=255&g=32&b=10&fade=5000'>/wave</a> a slow wave animation on LED on base color specified by arguments: r=<0..255> g=<0..255> b=<0..255><br>";
  message += "<a href='/ledsoff?fade=500'>/ledsoff</a> turns off LEDs<br>";
  server.send(200, "text/html", message);
}

void handleNotFound(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

int getArgValue(String name)
{
  for (uint8_t i = 0; i < server.args(); i++)
    if(server.argName(i) == name)
      return server.arg(i).toInt();
  return -1;
}

bool checkFadeAndSetLedFunction(LedFunction *f)
{
  int fade = getArgValue("fade");
  if(fade > -1)
  {
    targetLedStates.setFunction(f);
    ledFader.start(fade);
  }
  else
    currentLedStates.setFunction(f);  
}

void handleRf()
{
  const int pinNumbers[] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
  int pin = getArgValue("D");
  int t = getArgValue("t");
  if(t == -1) t = RF_OSC;
  int id = getArgValue("id");
  int ch = getArgValue("channel");
  int on = getArgValue("on");
  String out = "rf D";
  out += pin;
  out += " ";
  out += t;
  out += " ";
  out += id;
  out += " ";
  out += ch;
  out += " ";
  out += on;
  pinMode(pinNumbers[pin], OUTPUT);
  for(int i = 0; i < 5; i++)
    rfWriteCode(pinNumbers[pin], t, id, (1 << (ch + 1)) | (on > 0? 1: 0));
  server.send(200, "text/plain", out);  
}

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);

  server.on("/wave", [](){
    server.send(200, "text/plain", "wave");
    WaveFunction *f = new WaveFunction();
    f->init(server);
    checkFadeAndSetLedFunction(f);
  });

  server.on("/Fuel Transfer", [](){
    server.send(200, "text/plain", "Fuel Transfer");
    WaveFunction *f = new WaveFunction();
    f->init(server);
    checkFade1AndSetLedFunction1(f);
  });


  server.on("/ledsoff", [](){
    server.send(200, "text/plain", "ledsoff");
    checkFadeAndSetLedFunction(new SimpleRGBFunction());
  });

  server.on("/ledsoff1", [](){
    server.send(200, "text/plain", "ledsoff1");
    checkFade1AndSetLedFunction1(new SimpleRGBFunction());
  });

   server.on("/rf", handleRf);

   server.on("/rf", handleRf1);
  
  server.onNotFound(handleNotFound);
  server.onNotFound(handleNotFound1);

  server.begin();
  Serial.println("HTTP server started");

  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}



const int LED_PIN1 = D3;
const int LED_COUNT1 = 200;

const int RF_OSC1 = 200;

Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(LED_COUNT1, LED_PIN1, NEO_GRB + NEO_KHZ800);

LedStates currentLedStates1(strip1);
LedStates targetLedStates1(strip1);
Fader<LedStates> ledFader1(currentLedStates, targetLedStates);
PinStates currentPinStates1;
PinStates targetPinStates1;
Fader<PinStates> pinFader1(currentPinStates, targetPinStates);





void handleNotFound1(){
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}

int getArgValue1(String name)
{
  for (uint8_t i = 0; i < server.args(); i++)
    if(server.argName(i) == name)
      return server.arg(i).toInt();
  return -1;
}

bool checkFade1AndSetLedFunction1(LedFunction *f)
{
  int fade1 = getArgValue1("fade");
  if(fade1 > -1)
  {
    targetLedStates1.setFunction(f);
    ledFader1.start(fade1);
  }
  else
    currentLedStates1.setFunction(f);  
}

void handleRf1()
{
  const int pinNumbers[] = {D0, D1, D2, D3, D4, D5, D6, D7, D8};
  int pin = getArgValue1("D");
  int t = getArgValue1("t");
  if(t == -1) t = RF_OSC1;
  int id = getArgValue1("id");
  int ch = getArgValue1("channel");
  int on = getArgValue1("on");
  String out = "rf D";
  out += pin;
  out += " ";
  out += t;
  out += " ";
  out += id;
  out += " ";
  out += ch;
  out += " ";
  out += on;
  pinMode(pinNumbers[pin], OUTPUT);
  for(int i = 0; i < 5; i++)
    rfWriteCode(pinNumbers[pin], t, id, (1 << (ch + 1)) | (on > 0? 1: 0));
  server.send(200, "text/plain", out);  
  }



void loop(void)
{
  server.handleClient();
  //MDNS.update();
  currentLedStates.render();
  if(ledFader.active)
    targetLedStates.render();
  if(!ledFader.fade())
    currentLedStates.commit();
  pinFader.fade();

  server.handleClient();
  //MDNS.update();
  currentLedStates1.render();
  if(ledFader1.active)
    targetLedStates1.render();
  if(!ledFader1.fade())
    currentLedStates1.commit();
  pinFader1.fade();




  
}

I'm not familiar with the libraries you're using but, many sections of code are near duplicates of each other, differentiated by appending a '1' to the variable name. Could it be you've missed the correct naming in this small piece:

LedStates currentLedStates1(strip1);
LedStates targetLedStates1(strip1);
Fader<LedStates> ledFader1(currentLedStates, targetLedStates);
PinStates currentPinStates1;
PinStates targetPinStates1;
Fader<PinStates> pinFader1(currentPinStates, targetPinStates);

Notice that currentPinStates and targetPinStates have no '1' appending.

so i have tried adding the appending no '1' to the codes but it still doesn't work .i still can't figure out whats wrong

this is the youtube that i was referring to for my project .

*youtube video

Does the code for the second strip work by itself. That is, if only the 2nd strip code is loaded does it work?

yes, the code for the second strip is independent but when i load the code for the 2nd strip, it does not light up, only the first one does .

Sorry, I'll have to bow out. I cannot see what the problem is.

oh its fine i understand

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.