Error when I add two codes in void setup ()

So am writing a code for a Bluetooth car
And I want the back light to be on on startup
So I have this in the

void setup ()
{
Serial.begin(9600);
}

Here the it is the braud rate of the Bluetooth module and the car works fine with the rest of the code (not mentioned) it goes forward, backward, etc

But when I do this to have the led on

void setup ()
{
Serial.begin(9600);
digitalWrite (backlight, HIGH);
}

The rest of the code (not mentioned) does not work and the car does not go forward, backward, etc but the backlight turns on

Please help me resolve this

All you have done there is enabled the pull up resistor.

Please post your full sketch

Could you be more specific
Like what should I do to resolve it

How can I be more specific?
You enabled the pull up resistor on the pin "backlight".
I don't know if that's what you intended, and I don't know what "backlight" is.

ok so here is the full sketch
Note:- in this i am routing the backlight negative pole through arduino as it is of 12 volt so it is high that is positive and low that is negative

#include <AFMotor.h>
 
//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
 
int val;
int Speeed = 255;
int frontight = 11;
int horn = 9;
int parking = 10; 
int backlight = 7;

void setup()
{
  Serial.begin(9600);  
}
  

void loop(){
  if(Serial.available() > 0){
    val = Serial.read();
     
    Stop(); //initialize with motors stoped
     
          if (val == 'F'){
          forward();
          }
 
          if (val == 'B'){
          back();
          }
 
          if (val == 'L'){
          left();
          }
 
          if (val == 'R'){
          right();
          }
          if (val == 'I'){
          topright();
          }
 
          if (val == 'J'){
          topleft();
          }
 
          if (val == 'K'){
          bottomright();
          }
 
          if (val == 'M'){
          bottomleft();
          }
          
          if (val == 'T'){
          Stop();
          }

           if (val == 'W'){
          frontightson();
          }

           if (val == 'w'){
          frontightsoff();
          }

           if (val == 'U'){
          backightson();
          }

           if (val == 'u'){
          backightsoff();
          }

            if (val == 'V'){
          hornon();
          }

           if (val == 'v'){
          hornoff();
          }

            if (val == 'X'){
          parkingon();
          }

           if (val == 'x'){
          parkingoff();
          }
  }
}
          
 
           
 
 
 
void forward()
{
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}
 
void back()
{
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(Speeed); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(Speeed); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
 
void left()
{
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(Speeed); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(Speeed); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}
 
void right()
{
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(Speeed); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
 
void topleft(){
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed/3.1);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed/3.1);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}
 
void topright()
{
  motor1.setSpeed(Speeed/3.1); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(Speeed/3.1); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(Speeed);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(Speeed);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}
 
void bottomleft()
{
  motor1.setSpeed(Speeed); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(Speeed); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(Speeed/3.1); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(Speeed/3.1); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
 
void bottomright()
{
  motor1.setSpeed(Speeed/3.1); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(Speeed/3.1); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(Speeed); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(Speeed); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
 
 
void Stop()
{
  motor1.setSpeed(0); //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}

void frontightson()
{
digitalWrite (frontight, HIGH);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

void frontightsoff()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

void hornon()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, HIGH);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

void hornoff()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

void parkingon()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, HIGH);
digitalWrite (backlight, HIGH);
}

void parkingoff()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

void backightson()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, LOW);
}


void backightsoff()
{
digitalWrite (frontight, LOW);
digitalWrite (horn, LOW);
digitalWrite (parking, LOW);
digitalWrite (backlight, HIGH);
}

Not very clear.
Please confirm that twelve volts goes nowhere near your Arduino

What has that code got to do with backlight?

Have you ever heard of pinMode()?

2 Likes

No what's that

Useful for configuring pins as outputs.

1 Like

The backlight is of 12 volt and I can not get 12 volt directly from Arduino so what i did is connected + ve of the backlight to the battery and routed it's negative through Arduino that is when the Arduino will give High / + ve that will turn off the led but when it is low / - ve that will turn on the led giving the current a negative path

Ok that's good but how can I use it to resolve this

Did you imagine that these are automagically outputs?

Don't put twelve volts anywhere near your Arduino's I/O pins.

ok thanks for your help but if i am connecting 5 volts then also it is not working
anyways thats not the question it is when i add two statements in void setup () the rest of the code does not work
(refer the main question for more information)

You're joking, surely?
It took until reply #6 before you even posted the code, and we've yet to see a schematic, two hours later.

1 Like

No i thought that you might say that it is not clear :grinning::grinning::grinning::grinning:
anyways is there a solution

Yes, of course there's a solution, but it's

2 Likes

????

can someone please help

Have you updated your sketch since it was pointed out that your output pins were not specifically set to be outputs ?

2 Likes