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