Combining Codes

Hi guys, I am currently doing a small project that uses a smart phone to control LEDs. There are three functions that this project to have such as by turning them on and of, dimming them, and controlling them by voice.

For each function, I have tested the code and they work perfectly when they are separate.

However, when I combine them, they don't really work well with each other. I have been trying to combine them for several days, but the combined code still don't work.

Could you guys please help me how to combine them?
Here are the code for each function

// Turning LEDs by using buttons
int redLed = 2;  
int yellowLed = 3; 
int greenLed = 4; 
int whiteLed = 5; 
int data;

void allon() {
    digitalWrite(redLed, HIGH);
    digitalWrite(yellowLed, HIGH);
    digitalWrite(greenLed, HIGH);
    digitalWrite(whiteLed, HIGH);
}

void alloff() {
    digitalWrite(redLed, LOW);
    digitalWrite(yellowLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(whiteLed, LOW);
}
void setup() {
  
  pinMode(redLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(whiteLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  Serial.begin(9600);
}
void loop() {

 while (Serial.available()) {     
    data = Serial.read();
  } 

// turn on/off the Red LED
  if (data == '1') {
    digitalWrite(redLed, HIGH);
  }
  if (data == '2'){
     digitalWrite (redLed, LOW);
   }

 // turn on/off the yellow LED
  if (data == '3') {
    digitalWrite(yellowLed, HIGH);
  }
  if (data == '4') {
      digitalWrite(yellowLed, LOW);   
  }

// turn on/off the green LED
  if (data == '5') {
    digitalWrite(greenLed, HIGH);
  } 
  if (data == '6') {
       digitalWrite(greenLed, LOW);
       }

// turn on/off the white LED
  if (data == '7') {
    digitalWrite(whiteLed, HIGH);
  }
  if (data == '8') {
        digitalWrite(whiteLed, LOW);
      }

// Turn on/off all LEDs
if (data == '9') {
    allon();
  }
if (data == 'a') {
    alloff();
      }
// dimming an LED
int dimdata;
int dimled = 10;

void setup(){
  pinMode(dimled, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  
  if (Serial.available() > 0) {
    dimdata = Serial.read();
    Serial.println (dimdata);
    analogWrite (dimled, dimdata);
  }
}
// Control LEDs by voice
String voice;

int led1 = 3;
int led2 = 4;
int led3 = 5;
int led4 = 6;
int led5 = 10;
int led6 = 11;

void allon() {
  digitalWrite (led1, HIGH);
  digitalWrite (led2, HIGH);
  digitalWrite (led3, HIGH);
  digitalWrite (led4, HIGH);
  digitalWrite (led5, HIGH);
  digitalWrite (led6, HIGH);
}

void alloff() {
  digitalWrite (led1, LOW);
  digitalWrite (led2, LOW);
  digitalWrite (led3, LOW);
  digitalWrite (led4, LOW);
  digitalWrite (led5, LOW);
  digitalWrite (led6, LOW);
}

void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
}

void loop() {
  while(Serial.available()) {
    delay(10);
    char c = Serial.read();
    if(c =='#')
    {break; }
    voice += c;
}

if (voice.length() > 0) {
  Serial.println(voice);
  if (voice == "all on")
  {
  allon();    
  }
  else if (voice == "all off")
  {alloff() ; }
  
voice="";
}
}

p/s: I have the feeling that the "Serial.available" is the reason why they can't be combined, but I don't know how to fix it. Please help me.

Thank you for your time

Then start by posting them in code-tags, not quote-tags :wink:

Post your best attempt at combining them and describe exactly what you mean by "don't really work well with each other".

if (voice.length() > 0) {
  Serial.println(voice);
  if (voice == "all on")
  {
  allon();    
  }
  else if (voice == "all off")
  {alloff() ; }
  
voice="";
}

So, even if you've only received one character, reset the String?

septillion:
Then start by posting them in code-tags, not quote-tags :wink:

Sorry, I just edit the tag

pert:
Post your best attempt at combining them and describe exactly what you mean by "don't really work well with each other".

This is the combined code for controlling LEDs by voice and buttons.
This code doesn't work perfectly since I have to tap the buttons multiple times in order to turn on and off the LEDs, the same thing happens to the voice function, I have to speak multiple times in order to turn on and off the LEDs.

String voice;
int data;
int redLed = 2;  
int yellowLed = 3; 
int greenLed = 4; 
int whiteLed = 5; 
int sensor = 8;
int sled = 9;           
int dimled = 10;

void allon() {
    digitalWrite(redLed, HIGH);
    digitalWrite(yellowLed, HIGH);
    digitalWrite(greenLed, HIGH);
    digitalWrite(whiteLed, HIGH);
    digitalWrite(sled, HIGH);
    digitalWrite(dimled, HIGH);
}

void alloff() {
    digitalWrite(redLed, LOW);
    digitalWrite(yellowLed, LOW);
    digitalWrite(greenLed, LOW);
    digitalWrite(whiteLed, LOW);
    digitalWrite(sled, LOW);
    digitalWrite(dimled, LOW);
}

void setup() {
  
  pinMode(redLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(whiteLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(sled, OUTPUT);
  pinMode (sensor, OUTPUT);
  pinMode (dimled, OUTPUT);
  Serial.begin(9600);
}

void loop() {

 while (Serial.available()) {     
    delay(10);
    data = Serial.read();
     if (data == '1') {
    digitalWrite(redLed, HIGH);
  }
  if (data == '2'){
     digitalWrite (redLed, LOW);
   }

 // turn on/off the yellow LED
  if (data == '3') {
    digitalWrite(yellowLed, HIGH);
  }
  if (data == '4') {
      digitalWrite(yellowLed, LOW);   
  }

// turn on/off the green LED
  if (data == '5') {
    digitalWrite(greenLed, HIGH);
  } 
  if (data == '6') {
       digitalWrite(greenLed, LOW);
       }

// turn on/off the white LED
  if (data == '7') {
    digitalWrite(whiteLed, HIGH);
  }
  if (data == '8') {
        digitalWrite(whiteLed, LOW);
      }

// Turn on/off all LEDs
if (data == '9') {
    allon();
  }
if (data == 'a') {
    alloff();
      }

  int svalue = analogRead(A0);
  Serial.println(svalue);
  
//turn on/off the sensor
  if (data == 'b'){
  digitalWrite (sensor, HIGH); // turn on the sensor
  Serial.println(svalue);
  delay (500);
  
    if (svalue < 100){
      digitalWrite (sled, HIGH);
      delay (2000);
    }
    else if (svalue >100) {
      digitalWrite (sled, LOW);
      }
    }
  if (data == 'c') {
    digitalWrite (sensor, LOW); // turn of the sensor
    digitalWrite (sled, LOW);
  }
  } 

   while(Serial.available()) {
    delay(10);
    char c = Serial.read();
    if(c =='#')
    {break; }
    voice += c;
}
  

 if (voice.length() > 0) {
  Serial.println(voice);
  if (voice == "all on"){
      allon();    
  }
  
  else if (voice == "all off") {
    alloff() ; 
    }

  if (voice == "one"){
    digitalWrite (redLed, HIGH);
  }
  if (voice == "two"){
    digitalWrite (redLed, LOW);
  }
  
  
voice="";

 }
   
}

You need to combine the two "while(Serial.available())" blocks.

The delays in the turn on the sensor code are problematic but only when you send a 'b', it won't affect the general functionality of the system. The system will be unresponsive during those delays. You would be better to use the method demonstrated at File > Examples > 02.Digital > BlinkWithoutDelay.

pert:
You need to combine the two "while(Serial.available())" blocks.

The delays in the turn on the sensor code are problematic but only when you send a 'b', it won't affect the general functionality of the system. The system will be unresponsive during those delays. You would be better to use the method demonstrated at File > Examples > 02.Digital > BlinkWithoutDelay.

Thank you for replying.
I have combined the while blocks, but the voice function doesn't work in that case.

while (Serial.available()) {
    delay(10);
    data = Serial.read();
    char c = Serial.read();
    if (c == '#')
    {
      break;
    }
    voice += c;
  }

How do you think that is going to work? You read a character. You don't know if it's a "data" or a "voice" one but you put that character into data anyway. Then you read another, despite the problem that there may not be another available to read. That one (or lack of one) goes to the "voice" buffer.

Look at the character that you read before deciding where to store it. Don't try to read another one until you know for sure that there's one available.

Get rid of the delay(10)'s too.

MorganS:
How do you think that is going to work? You read a character. You don't know if it's a "data" or a "voice" one but you put that character into data anyway. Then you read another, despite the problem that there may not be another available to read. That one (or lack of one) goes to the "voice" buffer.

Look at the character that you read before deciding where to store it. Don't try to read another one until you know for sure that there's one available.

Get rid of the delay(10)'s too.

Sorry, I know that is a big mistake. So the "char c" shouldn't "= Serial.read()", but
what should I do with it now? I only know the "Serial"

Well, after you get the Serial.read() into char c, look at c. What is in it? It kind-of looks like if c is a digit it should be put in data and then you go and do something with that data. If c isn't greater than '0' and less than '9' then it's a voice command.

You may find Serial Input Basics to be helpful.

MorganS:
Well, after you get the Serial.read() into char c, look at c. What is in it? It kind-of looks like if c is a digit it should be put in data and then you go and do something with that data. If c isn't greater than '0' and less than '9' then it's a voice command.

You may find Serial Input Basics to be helpful.

Thank you