Control 4 LEDs over bluetooth

Dear All,

My requirement is to control 4 LEDs over a bluetooth signal. Signal will be a character or a number.

I need to turn on the first led with default brightness when received the character ‘o’

  • turn off the first led and turn on second when received the character ‘g’

  • turn off the second led and turn on third when received the character ‘y’

  • change the brightness level of the current led when received a number ‘0 - 9’

  • turn off the current led when received character ‘f’

I was hoping to do this with the simple code below but it seems it needs more than that.

Really appreciate any guide on this. Thanks in advance.

/

/ include header for bluetooth
#include <SoftwareSerial.h>

// define RX,TX ports
SoftwareSerial mySerial(10,11);

char temp;
int output;
int brightness;


void setup()
{
  //open serial communications and wait for port open
  Serial.begin(9600);       //start serial output
  pinMode(3,OUTPUT);        //white led
  pinMode(5,OUTPUT);        //green led
  pinMode(6,OUTPUT);        //yellow led
  pinMode(9,OUTPUT);        //red led
  
  Serial.println("Start");  //print start in serial
  mySerial.begin(9600);     //start serial recieve
  
  output = 3;
  brightness = 0;
}

void loop()
{
  if(mySerial.available())  {
  temp=mySerial.read();
   Serial.println(temp);
   
   if(temp=='o'){
     output = 3;
     brightness = 50;
     analogWrite(output,brightness);
     
   if(temp=='w')
     output = 3;
     analogWrite(output,brightness);  
 
   if(temp=='g')
     analogWrite(output,LOW);
     output = 5;
     analogWrite(output,brightness);}
     
   if(temp=='f')
     brightness = 0;
     analogWrite(output,brightness);
     
   if(temp=='y')
     output = 9;
     analogWrite(output,brightness);

   if(temp=='r')
     output = 9;
     analogWrite(output,brightness);
     
   if(temp=='0')
     brightness = 25;
     analogWrite(output,brightness);

   if(temp=='1')

   if(temp=='3')
     brightness = 102;
     analogWrite(output,brightness);

   if(temp=='6')
     brightness = 178;
     analogWrite(output,brightness);

   if(temp=='9')
     brightness = 255;
     analogWrite(output,brightness); 
                            }
  if(Serial.available())
  mySerial.write(Serial.read());
}

Pay very close attention to your ifs and code blocks ("{...}"). In the code above you're checking whether Temp is 'w' inside a check that already determined it's 'o'...