I have soil moisture, the value read by analogRead is below 50 servo work, over 50 servo should not work.
The code I’ve written seems to be smooth when I debug with serial.print.
But while the sensor reads the value, the servo motor doesnt work. I’m getting this issue because the servo motor library uses timer1. How can I solve this problem using interrupt?
(I must use interrupt) Thanks…
The ADC is a successive approximation convertor, and at the end of the conversion (typically around 100 microseconds after conversion start) the convertor can generate an end-of-conversion interrupt.
Enabling this interrupt and providing an interrupt service routine will give you the result you desire.
I can't see what is difficult about the sentence "please do that in your next Reply rather than modifying your earlier Post which has already been commented on"
OK, clearly you do not understand how to write a program to do what you want to do.
I suspect it is actually quite simple. You have posed the classic “XY problem” (check out the link!); you have shown a program attempting to do something in the manner you - wrongly - thought it might be done instead of simply detailing what the problem is.
So please, sit down and write out the plan - the requirements of the project - for us.
I get the idea from your first description that this is a relatively simple task where you read an analog input and compare it to a threshold value. If it is above a threshold, nothing happens, if it is below a threshold a servo needs to to something. What is that “something” then? Does it have to move from one position to another or does it have to repetitively perform a reciprocating action? When the threshold is again exceeded, does the servo need to return to a rest value or does it not matter?
If you can present a properly detailed plan, we can pull from your unfortunate code, the elements needed to do it properly.
Paul__B:
OK, clearly you do not understand how to write a program to do what you want to do.
If you can present a properly detailed plan, we can pull from your unfortunate code, the elements needed to do it properly.
The project that I try to do is the smart garden water system, the sensor “Soil Moisture Sensor for Arduino | Smart Prototyping” that I use is measuring the humidity in the soil and I want the servo to work if the humidity in the soil is below 5% and the servo motor doesn’t work if it is above 50%. I do the value reading from the sensor in the interrupt, I tried to run the servo motor in the loop, but the servo does not work. So my X problem is how I can start and stop the servo suddenly according to the value in the sensor.
#include <TimerOne.h>
#include <Servo.h>
#include <EEPROM.h>
Servo servo;
int sensorPin = A0;
int sensorValue = 0;
int percentValue = 0;
int servo_speed = 3000;
bool state = false;
bool servostate = false;
bool direct = false;
int pos = 0;
int address1 = 0;
int address2 = 1;
void setup()
{
Serial.begin(9600);
servo.attach(6);
pos = EEPROM.read(address1);
direct = EEPROM.read(address2);
Timer1.initialize(1000000);
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void timerIsr()
{
Timer1.detachInterrupt(); //stop the timer
sensorValue = analogRead(sensorPin);
Serial.print("\n\nAnalog Value: ");
Serial.println(sensorValue);
percentValue = map(sensorValue, 1023, 200, 0, 100);
Serial.print("\nPercentValue: ");
Serial.print(percentValue);
Serial.println("%");
if(percentValue > 50)
{
servostate = false;
}
else if(percentValue < 5)
{
servostate = true;
}
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void loop() {
if(servostate)
{
servo.write(pos);
if(pos == 180)
{
direct = true;
EEPROM.write(address2, direct);
}
else if (pos == 0)
{
direct = false;
EEPROM.write(address2, direct);
}
if(direct) pos--;
else pos++;
EEPROM.write(address1, pos);
delay(servo_speed/180);
}
}
You supposedly know what it is you want to do, but you fail to understand that we do not know what that is because you have not disclosed it. It is a matter of failed communication.
You continue to use completely nonsensical terms such as "run the servo motor" and "start and stop the servo". What is this servo motor? What does it do? What does "the servo motor doesn't work if it is above 50%" mean? I begin to wonder if you even know yourself!
You have got yourself wound up with completely irrelevant things such as interrupts and a timer library. Neither of these is in any way even remotely relevant to your project and posting code which includes them is a waste of everyone's time.
Why are you using an interrupt anyway. The timer should only be used to time when the soil moisture reading is taken. The speed and angle determine the time the servo will move.
I think it should be something like this : read moisture if moisture good do nothing else if moisture bad turn servo ( tap on ) read moisture if moisture good turn servo opposite direction ( tap off) else if moisture bad do nothing ( leave tap on ).
There is no way to trigger an interrupt using software ,it must be done using an input.
If you still need to use interrupt try using a digital write ( high) on a pin example pin 9. Run a wire from pin 9 to your chosen interrupt pin. When you require the interrupt the digitalWrite command on pin 9 will send a high signal to the interrupt pin and trigger the interrupt.
Due to complete ignorance of what an interrupt is in a microcontroller/ computer.
Ripcrow:
I think it should be something like this : read moisture if moisture good do nothing else if moisture bad turn servo ( tap on ) read moisture if moisture good turn servo opposite direction ( tap off) else if moisture bad do nothing ( leave tap on ).
I am probing for some description of whether the servo is supposed to turn a tap (in which case a little "hobby" servo would not be capable) or whether it must operate a bucket pump (same problem) or what!
Ripcrow:
If you still need to use interrupt try using a digital write ( high) on a pin example pin 9. Run a wire from pin 9 to your chosen interrupt pin. When you require the interrupt the digitalWrite command on pin 9 will send a high signal to the interrupt pin and trigger the interrupt.
Absolutely, incontestably, no possible use whatsoever for an interrupt.