I need to run one Arduino when a light is on for automatically watering my plants what should I do to my code?
// Arduino soil moisture sensor code
// declare variables for pins
const int sensorpin = A0;
const int sensorpower = 8;
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int pumppin = 11;
// variable for sensor reading
int sensor;
// delay time between sensor readings (milliseconds)
const int delayTime = 1;
// "wet" and "dry" thresholds - these require calibration
int wet = 800;
int dry = 500;
void setup(){ // code that only runs once
// set pins as outputs
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
pinMode(LED3,OUTPUT);
pinMode(sensorpower,OUTPUT);
pinMode(pumppin,OUTPUT);
// initialize serial communication
Serial.begin(9600);
}
void loop(){ // code that loops forever
// power on sensor and wait briefly
digitalWrite(sensorpower,HIGH);
delay(10);
// take reading from sensor
sensor = analogRead(sensorpin);
// turn sensor off to help prevent corrosion
digitalWrite(sensorpower,LOW);
// print sensor reading
Serial.println(sensor);
// If sensor reading is greater than "wet" threshold,
// turn on the blue LED. If it is less than the "dry"
// threshold, turn on the red LED and the pump.
// If it is in between the two values, turn on
// the yellow LED.
if(sensor>wet){
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,HIGH);
digitalWrite(pumppin,LOW);
}
else if(sensor<dry){
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
digitalWrite(pumppin,HIGH);
}
else{
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
digitalWrite(LED3,LOW);
digitalWrite(pumppin,LOW);
}
// wait before taking next reading
delay(delayTime);
}
This is something very new to me. will someone help me with a code to run the motor while the red light is on and then turn it off when the blue and yellow are on.
Thank you for trying to use code tags but you used 3 single quotes rather than 3 backticks. Please edit your post and change the quotes to backticks or delete them, highlight your code and click the < code >icon above the edit window to add the code tags
See if this helps it will probably save you some $$$.
Gil's Crispy Critter Rules for Processor Hardware:
Rule #1: An Arduino is NOT a Power Supply!
Rule #2: Never connect anything inductive (motors, speakers) directly to an Arduino!
Rule #3: Avoid connecting or disconnecting wires while the power is on.
Rule #4: Do not apply power to any pin unless you are certain of what you're doing.
Rule #5: Do not exceed the maximum voltage ratings.
Rule #6: Many Arduinos cannot power transmitters directly.
Rule #7: Before powering your project, take a break and double-check the wiring.
LaryD’s Corollaries:
Coro #1: When starting out, add a 220Ω resistor in series with both input and output pins to protect against shorts.
Coro #2: Invest in a Digital Multi-Meter (DMM) to measure voltages, currents, and resistance.
Note: Violating these rules can turn your Arduinos into crispy critters. For optimal performance, keep your wires under 25 cm (10 inches).
Additional Tips:
The L293 motor driver, though common, is inefficient as it can lose around 3V as heat when driving both legs of a motor. Consider using a motor driver with MOSFET outputs to reduce heat loss and conserve battery power.
That is important as any another project we see and more so to you. Thanks for the information.
The first thing you need to do is use a separate power supply for the motor. I am not sure what you are using to run the motor but per yur diagram it is a MOSFET, N channel. What is its part number.
While doing your code run the motor in two functions, one to turn it on and the other to turn it off. The sensor should be in another. Your existing code would work in a function. You can name whatever you want. Example:
void (motor_on) {Code to turn motor on}
Do the same with the LEDs. As you gain experience you will start combining some of these functions.
When debugging you can simply comment (//) out a function call to eliminate it temporary. Hopeful this helps, there are many others here that are much better at this then I am.
do you know the exact code i should use? all i need to do is send power through the two wires that are connected to the pump when the red light is on then turn the power off when the blue/yellow is on.