Problem in Controlling brightness of led

Hi! I am trying to control the brightness of led as well as turning on/off the led. I seem to have trouble compiling the codes together. Any suggestions? :slight_smile:

Arduino Program for brightness:

byte packet[2];
int pIndex;
int rPin = 9;

byte rValue = 0;

void setup() {
Serial.begin(9600);
    pinMode(rPin, OUTPUT);
    analogWrite(rPin, 0);
}

void loop() {

// see if there's incoming serial data:
if (Serial.available() > 0) {
packet[pIndex++] = Serial.read();
}

if(pIndex >= 2){

switch(packet[0]){
                    case 't':
                           rValue = packet[0];
                            break;
                    case 'y':
                            rValue = packet[3];
                            break;
        case 'r':
                rValue = packet[1];
                break;
        default:
                ;
}

analogWrite(rPin, rValue);   // 0 - 255

pIndex = 0;

}

}

Arduino Program for Turn ON/OFF:

int ledPin = 11;
int ledPin2 = 12;
int ledPin3 = 13;     // use the built in LED on pin 13 of the Uno
int state = 0;
int flag = 0;        // make sure that you return the state only once

void setup() {
    // sets the pins as outputs:
    pinMode(ledPin, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    digitalWrite(ledPin, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);

    Serial.begin(9600); // Default connection rate for my BT module
}

void loop() {
    //if some data is sent, read it and save it in the state variable
    if(Serial.available() > 0){
      state = Serial.read();
      flag=0;
    }
    
    // if the state is 0 the led will turn off
  /*  if (state == '0') {
        digitalWrite(ledPin, LOW);
        digitalWrite(ledPin2, LOW);
        digitalWrite(ledPin3, LOW);
        if(flag == 0){
          Serial.println("LEDs: off");
          flag = 1;
        }
    }
*/    // if the state is 1 the led will turn on
   
    else if (state == '1') {
        digitalWrite(ledPin, HIGH);
       
        if(flag == 0){
          Serial.println("LED1: on");
          flag = 1;
        }
    }
    else if (state == '2') {
       
        digitalWrite(ledPin2, HIGH);
        
        if(flag == 0){
          Serial.println("LED2: on");
          flag = 1;
        }
    }
    else if (state == '3') {
       
        digitalWrite(ledPin3, HIGH);
        if(flag == 0){
          Serial.println("LED3: on");
          flag = 1;
        }
    }
     else if (state == '4') {
        digitalWrite(ledPin, LOW);
       
        if(flag == 0){
          Serial.println("LED1: off");
          flag = 1;
        }
    }
     else if (state == '5') {
        digitalWrite(ledPin2, LOW);
       
        if(flag == 0){
          Serial.println("LED2: off");
          flag = 1;
        }
    }
     else if (state == '6') {
        digitalWrite(ledPin3, LOW);
       
        if(flag == 0){
          Serial.println("LED3: off");
          flag = 1;
        }
    }
}

Why don't you just use a brightness of 0 for off and 255 for on?

Your "Program for Brightness" is bad:
If the first character is 't' the brightness is set to 't'.
If the first character is 'y' the brightness is set to an uninitialized value (the 4th character of a 2-character message)
Only if the first character is 'r' does the brightness get set to the value of the second character.

How, exactly, do you want the program to act?