Pls help me i dont know what to do

So basically this project is supposed to be an RC bluetooth car with a gas sensor MQ2 installed, so its like a car that detects smoke/gas

this is the circuit diagram

and this is the code

int smokeA0=A0;
int buzzer =9;
char t;
float sensorValue;
void setup() {
	pinMode(13,OUTPUT);
	pinMode(12,OUTPUT);
	pinMode(11,OUTPUT);
	pinMode(10,OUTPUT);
  pinMode(buzzer,OUTPUT);
  pinMode(smokeA0,INPUT);
  Serial.begin(9600);
  Serial.println("Gas sensor warming up!");
  delay(6000);
  Serial.println("Gas sensor has warmed up!");
}

void loop() {
  //SMOKE DETECTOR
  sensorValue=analogRead(smokeA0);
  Serial.println(sensorValue);
  if(sensorValue > 150)
  {
    Serial.print("Smoke detected!");
    tone(buzzer,1000,200);
  }
  //BLUETOOTH CAR
  if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
if(t == 'F'){            
  digitalWrite(13,HIGH);
  digitalWrite(11,HIGH);
}
 
else if(t == 'B'){      //move reverse (all  motors rotate in reverse direction)
  digitalWrite(12,HIGH);
  digitalWrite(10,HIGH);
}
  
else if(t == 'L'){      //turn right (left side motors rotate in forward direction,  right side motors doesn't rotate)
  digitalWrite(11,HIGH);
}
 
else  if(t == 'R'){      //turn left (right side motors rotate in forward direction, left  side motors doesn't rotate)
  digitalWrite(13,HIGH);
}

else if(t ==  'W'){    //turn led on or off)
  digitalWrite(9,HIGH);
}
else if(t == 'w'){
  digitalWrite(9,LOW);
}
else if(t == 'S'){      //STOP (all motors stop)
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
}
delay(1000);
}

The problem here is that the smoke detecting system works but the car doesnt move, like the bluetooth module is working fine, the motor driver is working fine, i can connect it to my phone but it doesnt move

No this is no circuit-diagram this is a fricture.
A frizzy fancy picture showing 98% colors and too less of the important information that would be needed to analyse what is connected to what.

You put a lot of additional work on the shoulders of your potential helpers

You should use self-explaining names for the IO-pins instead of numbers

what does it mean if IO-pin 13 is HIGH?
what does it mean if IO-pin 12 is HIGH?
what does it mean if IO-pin 11 is HIGH?
what does it mean if IO-pin 10 is HIGH?

Did you test the driving functions
with small test-codes that do not depend on the other components?

You should provide the datasheet of the used motorshield

Are you using such a 9V battery ?

or something different?

Is it really powered with a 9V battery like comes in the smoke detector? They can't supply motors. There's not enough power there. You need some AAA or AA batteries or a LiPo.

You will need a proper 5V supply as well.
The sensor and the bluetooth receiver may well draw too much current for the built in regulator.
Start by removing one left motor and one right motor.... that will help to bring current down...

Does a Battery store power or energy?

image

P.S. Source https://www.acc-emotion.com/de/node/435

1 Like

Hi @valorantfreak,

while the main reason may be the power source, there are also some issues with your software.

  • When you use the commands F, B, L or R you only set specific motor pins. Depending on the previous command this leads to unwanted pin configurations.
  • Pin 9 is declared as buzzer pin but also used to switch a led on/off (commands W, w).
  • There is a delay(1000) at the end of loop() that will make your sketch less responsive than necessary.

Here is a commented version:

Commented original sketch
// Constant expressions should be declared as const or constexpr so that 
// they cannot be changed in the code 
int smokeA0=A0;
int buzzer =9;   // Here it is called buzzer and used by tone() but the pin is also used for a led ???
                 // see commands 'W' and 'w' with digitalWrite()
char t;

// sensorValue is used with analogRead() and can therefore only have integer values, use int or uint16_t
float sensorValue;

void setup() {
	pinMode(13,OUTPUT);
	pinMode(12,OUTPUT);
	pinMode(11,OUTPUT);
	pinMode(10,OUTPUT);
  pinMode(buzzer,OUTPUT);   // Not required for analogRead(), it is done in the function itself
  pinMode(smokeA0,INPUT);
  Serial.begin(9600);
  Serial.println("Gas sensor warming up!");
  delay(6000);
  Serial.println("Gas sensor has warmed up!");
}

void loop() {
  //SMOKE DETECTOR
  sensorValue=analogRead(smokeA0);
  Serial.println(sensorValue);
  if(sensorValue > 150)
  {
    Serial.print("Smoke detected!");
    tone(buzzer,1000,200);
  }
  //BLUETOOTH CAR
  if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
// if / else if/ else if / ... is not easy to follow ... 
// As all if-clauses depend on the variable t it is much easier
// to use switch/case! 

if(t == 'F'){            
  // If you are not sure about the status of the other pins 
  // setting only a part of the motor pins is dangerous!
  digitalWrite(13,HIGH); 
  digitalWrite(11,HIGH);
}
 
else if(t == 'B'){      //move reverse (all  motors rotate in reverse direction)
  // See above
  digitalWrite(12,HIGH);
  digitalWrite(10,HIGH);
}
  
else if(t == 'L'){      //turn right (left side motors rotate in forward direction,  right side motors doesn't rotate)
  digitalWrite(11,HIGH);
}
 
else  if(t == 'R'){      //turn left (right side motors rotate in forward direction, left  side motors doesn't rotate)
  digitalWrite(13,HIGH);
}

else if(t ==  'W'){    //turn led on or off)
// As written above --- is this an led or the buzzer?
  digitalWrite(9,HIGH);
}
else if(t == 'w'){
  // See above --- led or buzzer?
  digitalWrite(9,LOW);
}
else if(t == 'S'){      //STOP (all motors stop)
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
}
// Delay in loop() is not a good concept (in most cases), especially if you want to have 
// an application with quick responses!
delay(1000);
}

You can check what happens on Wokwi: https://wokwi.com/projects/383207629951970305

Feel free to check this out:

/*
  Forum: https://forum.arduino.cc/t/pls-help-me-i-dont-know-what-to-do/1196695
  Wokwi: https://wokwi.com/projects/383205963137534977

*/

constexpr byte  smokePin = A0;
constexpr byte  buzzerPin = 9;
constexpr byte  ledPin    = 8;  // If the buzzer uses 9 the led should have a different pin ...

constexpr byte LeftMotorPin1 = 10;
constexpr byte LeftMotorPin2 = 11;
constexpr byte RightMotorPin1 = 12;
constexpr byte RightMotorPin2 = 13;

char t;
int sensorValue;


void setup() {
  pinMode(LeftMotorPin1, OUTPUT);
  pinMode(LeftMotorPin2, OUTPUT);
  pinMode(RightMotorPin1, OUTPUT);
  pinMode(RightMotorPin2, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(smokePin, INPUT);   // not required for analogRead() !!!
  Serial.begin(115200);
  Serial.println("Gas sensor warming up!");
  delay(6000);
  Serial.println("Gas sensor has warmed up!");
}


void loop() {
  //SMOKE DETECTOR
  measure();
  //BLUETOOTH CAR
  if (Serial.available()) {
    t = Serial.read();
    Serial.println(t);
    handleAction();
  }
}

void handleAction() {
  switch (t) {
    case 'F':
      moveForward();
      break;
    case 'B':
      moveBackwards();
      break;
    case 'L' :
      turnLeft();
      break;
    case 'R':
      turnRight();
      break;
    case 'W' :
      digitalWrite(ledPin, HIGH);
      break;
    case 'w' :
      digitalWrite(ledPin, LOW);
      break;
    case 'S' :
      stopMotors();
      break;
    default:
      t = '-';
      break;
  }
}

void measure() {
  static unsigned long lastTime = 0;
  static int lastValue = -1;
  if (millis() - lastTime > 1000) {
    lastTime = millis();
    sensorValue = analogRead(smokePin);
    if (sensorValue != lastValue) {
      Serial.println(sensorValue);
      lastValue = sensorValue;
    }
    if (sensorValue > 150)
    {
      Serial.print("Smoke detected!");
      tone(buzzerPin, 1000, 200);
    }

  }
}

void setMotors(byte L1, byte L2, byte R1, byte R2) {
  digitalWrite(LeftMotorPin1, L1);
  digitalWrite(LeftMotorPin2, L2);
  digitalWrite(RightMotorPin1, R1);
  digitalWrite(RightMotorPin2, R2);
}

void moveBackwards() {
  setMotors(HIGH, LOW, HIGH, LOW);
}

void moveForward() {
  setMotors(LOW, HIGH, LOW, HIGH);
}

void turnRight() {
  setMotors(LOW, HIGH, LOW, LOW);
}

void turnLeft() {
  setMotors(LOW, LOW, LOW, HIGH);
}

void stopMotors() {
  setMotors(LOW, LOW, LOW, LOW);
}

On Wokwi; https://wokwi.com/projects/383205963137534977

There is still room for improvement, but it might be a good start ...

Good luck!

1 Like

You should start your own thread if you want to understand terms better. Please ask a moderator to split off this post and someone can educate you on power and energy and how the two are related by time.

1 Like

Woww this will help me a lotthank u very muchh im 13 yrs old and im doing this just bcoz im interested, i struggled with the code thank u very muchh

nono i use 4 3.7v 3000mah li-on batteries ik a 9v battery like that wont work

Vin needs to be between 7V and 12V, 5V will not work.

1 Like

UPDATE; i modified the code and it is working fine but it only works when I connect it to a computer and turn on the switch of the batteries

without more details everything is again just guessing:

Forgot to connect all GND with each other?
or
it might be a code-problem which can not be solved because you did not post your

yes i did all the connections right and the code was fine too i figured out the problem

the problem wasn't with the code or battery, the Bluetooth module didn't work when connected to 5V so i connected it directly to the switch and it works, to everyone who responded thank u very much for trying to help me out it motivated me to modify the code and try to solve this.. im a 13yr old kid so it means a lot for me that u actually gave me ur attention

thank u very much, ur code didnt work directly but u motivated me to modify my own code and its working

You are welcome! I'm glad that the support you got here empowered you to find the solution on your own. That's even more worth than just providing a solution because it increased your capabilities.

I'm confident that you are on a great way to become a member of this forum who will support others in future in a similar way.

Never stop learning and sharing knowledge with others ...

Have fun!

ec2021

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.