I have connected my servo with a moisture sensor and am trying to make it spin for a few seconds when there is moisture. then I need it to stop spinning regardless of wether there is still moisture and then wait a few minutes until it reads again.
here is my code at the moment:
#define SensorPin A0
#include <Servo.h>
int servoPin=3;
int pos=1500;
int Tval=10;
int stopsig=1400;
Servo Servo1;
float sensorValue = 0;
void setup() {
Serial.begin(9600);
Servo1.attach(servoPin);
}
void loop() {
for (int i = 0; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);
int (valueChangeTime) = millis();
if (sensorValue > Tval){
Servo1.attach(servoPin);
Servo1.write(pos);
delay(1000);
Servo1.write(stopsig);
}
else{
Servo1.detach();}
}
I know its probably pretty bad but I'm trying my best ahahahah.
Hello
And welcome to this brilliant forum for Arduino friends.
What is your question and what do you expect?
Don´t for get to post the sketch in so called code tags <"/"> .
Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
Wow thanks so much, I’ll try this out tommorow. Is it possible for you to briefly list what I was doing wrong and how you managed to fix it? I’m trying to learn from my mistakes.
Thanks again
Servo.write() takes an angle 0-180. If you want to use values like 1400, 1500 then you should really use writeMicroseconds(). The library will try to use the values you give it but it makes the code confusing to read.
And attach() will send a default of write(90) which is equivalent to your 'pos' which is why the servo runs immediately.
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
void writeMicroseconds(int value); // Write pulse width in microseconds
void Servo::write(int value)
{
if(value < MIN_PULSE_WIDTH)
{ // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
If you change MIN_PULSE_WIDTH in Servo.h, its again wrong ...