This is my code... So, i am creating a firefighting robot that puts out fire with water. But as soon as i inputted the battery, the water pump also turns on. How to fix the the water pump to just turn on when our firefighting robot detects fire?
#include <Wire.h>
#include <Servo.h>
Servo myservo;
#define Left 9
#define Right 8
#define Forward 10
#define LM1 2
#define LM2 3
#define RM1 4
#define RM2 5
#define pump 6
#define smokeSensorPin A0
#define smokeThreshold 300
void setup() {
pinMode(Right, INPUT);
pinMode(Left, INPUT);
pinMode(Forward, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(pump, OUTPUT);
myservo.attach(11);
myservo.write(90);
}
void sweepServo() {
for (int pos = 50; pos <= 130; pos += 1) {
myservo.write(pos);
delay(10);
}
for (int pos = 130; pos >= 50; pos -= 1) {
myservo.write(pos);
delay(10);
}
}
void put_off_fire() {
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
digitalWrite(pump, HIGH);
sweepServo(); // Sweep the servo while extinguishing the fire
digitalWrite(pump, LOW);
}
void loop() {
// Read the smoke level from the MQ-2 smoke sensor
int smokeLevel = analogRead(smokeSensorPin);
// Check if the smoke level exceeds the defined threshold
if (smokeLevel > smokeThreshold) {
put_off_fire();
return; // Stop execution and put out the fire
}
myservo.write(90); // Center the servo
int leftSensor = digitalRead(Left);
int rightSensor = digitalRead(Right);
int forwardSensor = digitalRead(Forward);
if (leftSensor && rightSensor && forwardSensor) {
// Move forward
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
} else {
// Stop and adjust movement based on sensor inputs
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
if (leftSensor == LOW) {
// Turn left
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
}
if (rightSensor == LOW) {
// Turn right
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
}
}
put some Serial.println() statements in your code so that you know the state of variables and the flow of control, e.g.
// Check if the smoke level exceeds the defined threshold
if (smokeLevel > smokeThreshold) {
Serial.peint("putting out fire ");
Serial.peintln(smokeLevel);
put_off_fire();
return; // Stop execution and put out the fire
}
Hmmm. When i tried that, i don't know what is going on now with my water pump, first run it's still always on even if it didn't detect fire, the 2 lights (red and yellow) of the relay module (5v) is lighting. second run, water pump is not turning on even if it detect fire, only the red light in the relay module is lighting.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Your Fritzy image does not show enough accurate information.
Can you please post a picture of your project?
Can you please post data/specs on your pump relay?
Did you write your code in stages?
If so, have you got code that just turns the pump ON and OFF?
You need to do basic programming steps to develop your code, not write it all in one go and expect it to work.
So I am creating a fire fighting robot using this youtube tutorial as a guidance: (https://www.youtube.com/watch?v=yiTJZJmxDmA&t=119s). What I want is to add additional component (Sim 800L) that will send an sms and call to the registered number the moment the robot detects a fire. This is the circuit diagram:
Powering the controller via Vin, You will likely need to skip using the 5 volt pin.
The UNO is not a power supply for motors. Give the servo a supply on its own.
For sim 800L you should use a different power supply. Typically the required voltage is 4V, 2A. The grounds of sim800L and the Arduino should be shorted. Then Tx of the arduino should be connected to the RX of the SIM800. Tx of the SIM800L should be conencted to the RX of the Arduino.
But in the video, he just connected the servo motor in the 5v and gnd power pin and 8 digital pin, and it works perfectly fine. And I also watch some other youtube tutorials and they power the motor using only an arduino. What goes wrong with mine?
That's an ignorant amateur then. The tiny strips on the board are not designed for motor currents. Also the onboard 5 volt converter will be overloaded, overheat and likely getting damaged.
Your two or more topics on the same or similar subject have been merged.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
No. Direct power will not work. Also you'll need logic level converter in between the tx and rx pins of the arduino and sim800L. Kindly follow the comments made by Mr. @Railroader
If you power the Arduino from USB, there is just about enough power to run a small servo with little to no load on it.
Unfortunately, this has led to plenty of tutorials that have servos wired up this way, but for anything other than an exercise with a microserver moving a pointer, it's not a good idea, as noted above.