Pages: 1 [2]   Go Down
Author Topic: Servo Sweep with conditions  (Read 888 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
umm yeah i think is not the sweep servo i should be using.. i just need to move from position 0 to 1 and from 1 to 0 with conditions
I suspect that it is a bit more complicated than that. What I suspect you want is to move the servo to a position, if it is not already there.

High moisture --> Move the servo to 180, if it is not already there
Low moisture --> Move the servo to 0, if it is not already there

So, you need to keep track of where the servo is. Move the servo from where it is, in steps, if a move is needed. Move from currPos to destPos, in steps, rather than from 0 to 180 or from 180 to 0 (assuming that currPos and destPos are the names you define/use). Update currPos after every movement. Set destPos to 0 or 180, if radical movement is needed.

Actually, it seems like you would want to tie the amount to move the servo to the amount that the current sensor reading differs from the desired reading. If the soil is only a tiny bit damp, don't throw the window wide open.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you only need the servo to move between two positions without speed control, you probably coulds modify the below code to simply move the servo depending on the desired value. You night also need to a deadband to keep the servo from hunting when the input is at the 400 value (if value > 402 or if value < 398).     

Code:
//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }   
 
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the help, ill test it asap

Edit: well i think im gonna try this

Code:
//OB SOIL
//# the sensor value description
//  # 0  ~300      dry soil
//  # 300~700     humid soil
//  # 700~950     in water
#include <Servo.h>
Servo myservo;
const int number = 440;
const int number2 = 360;

void setup(){
  Serial.begin(9600);
  myservo.attach(9);
}

void loop(){
  Serial.print("Moisture Sensor Value:");
  Serial.println(analogRead(2)); 
  delay(100);
  if (analogRead(2) > number) {                                 
    myservo.write(179);             
    delay(2000);                       
  }

  if (analogRead(2) < number2) {                             
    myservo.write(0);               
    delay(2000);                       
  }

}
« Last Edit: November 24, 2012, 02:00:39 pm by eclipsear » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, you need to keep track of where the servo is. Move the servo from where it is, in steps, if a move is needed. Move from currPos to destPos, in steps, rather than from 0 to 180 or from 180 to 0 (assuming that currPos and destPos are the names you define/use). Update currPos after every movement. Set destPos to 0 or 180, if radical movement is needed.

Well you were right, the servo keeps going wild when he reachs some condition. Where i can get a code for something like that? or does anybody know how it would be?
Logged

Pages: 1 [2]   Go Up
Jump to: