Select the number of the led in digitalWrite command (intermidiate user)

I try to choose the defined led by my counter (i) but I think Iwm doing it wrong.
I have 5 LEDs that I need to turn on (or of) by the number I input via bluetooth
This is the code

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>


#define I2C_ADDR    0x27
#define BACKLIGHT 3
#define En 2
#define Rw 1
#define Rs 0
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#define led1 7
#define led2 8
#define led3 9
#define led4 10
#define led5 11
#define fan 5
#define sda A4
#define scl A5
#define rx 0
#define tx 1


const int servo=3;
LiquidCrystal_I2C  lcd(I2C_ADDR,En,Rw,Rs,D4,D5,D6,D7);
void setup() {
  Serial.begin(9600);
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  pinMode(fan, OUTPUT);
  Servo servo;
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
}

void loop() {
        lcd.print("System ON");
        delay (4000);
  if(Serial.available()>0){
        lcd.clear();
        lcd.print("Device Connected");
        delay(4000);
        lcd.clear();
        char datain;
        datain= Serial.read();
        switch (datain){
              case 'L': Serial.println("How much light do you want to turn on?");
                        char prct=0;
                        prct=Serial.read();
                        int amount= prct - '0';
                        int i=0;
                        for (i<amount; i++;){
                          digitalWrite(led(i+1),HIGH);
                          delay(700);
                          }
                          break;
          }
    }
    
  

}

Also I would like to know if it’s possible to digitalread the state of an output pin.
Thnks in advance.

  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);

Don’t f**k with the hardware serial pin when using them as hardware serial pins.

        char datain;
        datain= Serial.read();

Look like you know what you are doing:

        char datain = Serial.read();
for (i<amount; i++;){

A for loop has three parts - an initialization section, a while clause, and in end-of-loop action section. i<amount hardly looks like an initialization statement. i++ hardly looks like a while clause.

led() is not a function that you have defined.

That code doesn’t even compile, does it?
It’s silly to have a switch statement that only has one case.

First thinh in my mind:

The library LCD.h is a real library or you 'make it'?

PaulS:

  pinMode(rx, INPUT);

pinMode(tx, OUTPUT);



Don't f**k with the hardware serial pin when using them as hardware serial pins.



char datain;
        datain= Serial.read();



Look like you know what you are doing:



char datain = Serial.read();






for (i<amount; i++;){



A for loop has three parts - an initialization section, a while clause, and in end-of-loop action section. i<amount hardly looks like an initialization statement. i++ hardly looks like a while clause.

led() is not a function that you have defined.

That code doesn't even compile, does it?
It's silly to have a switch statement that only has one case.

Am I not suppose to make pinMode to the rx and tx serial pins (I want to use it with bluetooth module and it’s my first time doing that)
the switch case is now finished (before I was just at the start of making the code).

I now changed the loop and make all of the LEDs as an array.
could you please take a look and tell how is it?
(I’ve compiled the new code and the IDE didn’t indicate on problems).

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>


#define I2C_ADDR 0x27     //LCD I2C address defined
#define BACKLIGHT 3      // here I defined the pins connections
#define En 2
#define Rw 1           
#define Rs 0
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#define led1 7
#define led2 8
#define led3 9
#define led4 10
#define led5 11
#define fan 5
#define sda A4
#define scl A5

int led[5]={7,8,9,10,11};
byte V[8]={B00000, B00001, B10110, B11100, B01000, B00000,};           //Created V symbole
byte X[8]={B00000, B11011, B01110, B00100, B01110, B11011, B00000,};   //Created X symbole
Servo servo;  //Defined servo
LiquidCrystal_I2C  lcd(I2C_ADDR,En,Rw,Rs,D4,D5,D6,D7);  //Defined LCD I2C display

void setup() {
  Serial.begin(9600);
  lcd.createChar(0,V);
  lcd.createChar(1,X);
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
  pinMode(led[1], OUTPUT);
  pinMode(led[2], OUTPUT);
  pinMode(led[3], OUTPUT);
  pinMode(led[4], OUTPUT);
  pinMode(led[5], OUTPUT);
  servo.attach(3);
  lcd.noCursor();
  lcd.setCursor(4,0);
  lcd.print("System ON");  //Prompt for turning on
  delay (4000);
        
}

void loop() {
        int location, state, i, amount, num, c, spd, input, pace, index=0, real;
        char inp[3], inChar, datain;
        
        
  if(Serial.available()>0){
        Serial.println("Hello, you are now connected"); //Prompt for connection
        Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        lcd.clear();
        lcd.print("Device Connected"); //Display on lcd connection
        delay(4000);
        lcd.home();
        lcd.print("Door:");
        lcd.write(byte(1));
        lcd.write(" Fan:OFF");
        lcd.setCursor(0,1);
        lcd.print("Lights:OFF");
        lcd.clear();
       
        datain= Serial.read();
       
        switch(datain){
              
              case 'L':         state=digitalRead(led1);
                                if(state=0){
                                    Serial.println("Please enter the amount of LEDs you want to turn on (MAX is 5)\n");
                                    char prct=0;
                                    prct=Serial.read();
                                    amount= prct - '0';
                                      for (i=0; i<amount; i++){
                                        digitalWrite(led[i],HIGH);
                                        delay(700);
                                        }
                                    num = ((amount)/5)*100;
                                    lcd.setCursor(0,1);
                                    lcd.print("Light:");
                                    lcd.print(num,DEC);
                                    lcd.print("%  ");
                                    Serial.print("The light is now up to ");
                                    Serial.print(num,DEC);
                                    Serial.print("%\n");
                                }
                                else{
                                    c=0;
                                    while(c<5){
                                        digitalWrite(led[c],LOW);
                                        delay(500);
                                        c++;
                                      }
                                    lcd.setCursor(0,1);
                                    lcd.print("Light:OFF ");
                                    Serial.println("The light is now turned off");
                                  }
                                 break;
              
              case 'D':       location=servo.read();
                              if(location=-90){
                                servo.write(0);
                                lcd.home();
                                lcd.print("Door:");
                                lcd.write(byte(1));
                              }
                              if(location=0){
                                servo.write(-90);
                                lcd.home();
                                lcd.print("Door:");
                                lcd.write(byte(0));
                              }
                              break;

              case 'F':     spd=digitalRead(fan);
                            if(spd=0){
                              Serial.println("Pkease enter the wanted fan intensity [precentages]");
                              if(index < 3){
                                 inChar = Serial.read();
                                 inp[index] = inChar;
                                 index++; 
                                 inp[index] = '\0';
                                  }
                              input = inp[1] - '0';
                              pace = (255)*((input)/10);
                              real = (input)*10;
                              analogWrite(fan, pace);
                              lcd.setCursor(7,0);
                              lcd.print("FAN:");
                              lcd.print(input, DEC);
                              lcd.print("%");
                              Serial.print("The fan is now operating at ");
                              Serial.print(real, DEC);
                              Serial.print("%\n\n");
                              }
                             else{
                                analogWrite(fan,0);
                                lcd.setCursor(7,0);
                                lcd.print("FAN: OFF");
                                Serial.print("The fan is now turned off\n\n\n");
                              }
                              break;
              
              case 'M': //play music
                        break;             
        }
    }

}

    void song1() {}
    void song2() {}

Also, can you tell me how do I convert an 3 parts of one int array to a whole number (every part is a digit) in another int array?
example:

 int i, inp[3]={1,2,3};
 for(i=0;i<3;i++){
                                input[i] = inp[1] - '0';
                              }
                              int n;
                              n=input[0],input[1],input[2];

Thank you very much for your time.

luisilva:
The library LCD.h is a real library or you ‘make it’?

I downloaded it for the I2C LCD I read about it here http://forum.arduino.cc/index.php?topic=128635.0

T-rex166:
Also, can you tell me how do I convert an 3 parts of one int array to a whole number (every part is a digit) in another int array?
example:

 int i, inp[3]={1,2,3};

for(i=0;i<3;i++){
                                input[i] = inp[1] - ‘0’;
                              }
                              int n;
                              n=input[0],input[1],input[2];

They have this thing called Math. Nobody pays any attention to it, and then it shows later…

If I have three digits, say a 2, a 3, and a 4, and I want to assemble them into a three digit number like two hundred thirty four, then all I have to do is multiply to put each number in its place.

int digits[3] = {2 , 3 , 4};

int n = (digits[0] * 100) + (digits[1] * 10) + digits[2];

It’s not any trick of programming. Just basic regular old math like you should have learned in the second grade.

Am I not suppose to make pinMode to the rx and tx serial pins (I want to use it with bluetooth module and it's my first time doing that)

No, that's the job that Serial.begin() does.