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!