Hi,
So im trying to control the direction of a servo with two analog force sensors. Where SensorA controls the direction from (90º-180º), and Sensor B controls the direction (90º-0º). The initial position of the servo needs to be centered at roughly 90º. I need the displacement of the servo to be proportional to the input from the sensor, so i mapped the force sensor to the servo.
My problem is that i can only get my code to work for one of the sensors, and as a result the servo only moves in one direction.
How can manipulate my code so that i can have both sensorA and sensorB controlling the servo???
-any help is greatly appreciated.
here is my code so far
// These constants won't change:
#include <Servo.h> // include the servo library
Servo servoMotor;
const int analogPin = A0; // pin that the sensor is attached to
const int servoPin = 2; // pin that the servo is attached to
void setup() {
servoMotor.attach(servoPin);
}
void loop() {
// read the value of the force sensor:
int analogValue1 = analogRead(analogPin);
int analogValue = map(analogValue1, 0, 1023, 85, 0);
// if the analog value is high enough, move servo xº:
if (analogValue > 0) {
servoMotor.write(analogValue);
}
else {
servoMotor.write(90);
}
}
What do you propose to do if sensor 1 says go to 135 degrees while sensor 2 says to go to 45 degrees? Resolve that issue, and the code will be easy.
PaulS:
What do you propose to do if sensor 1 says go to 135 degrees while sensor 2 says to go to 45 degrees? Resolve that issue, and the code will be easy.
I figured out what my problem was. here is the code if anyone wants it.
// These constants won't change:
#include <Servo.h> // include the servo library
Servo servoMotor;
const int analogPinA = A0; // pin that the sensor is attached to
const int analogPinB = A1;
const int servoPin = 2; // pin that the servo is attached to
void setup() {
// initialize the servo pin as an output:
servoMotor.attach(servoPin);
servoMotor.write(85); // center
}
void loop() {
servoMotor.write(85);
// read the value of the sensorA:
int analogValue1 = analogRead(analogPinA);
// if the analog value is high enough, move øº
if (analogValue1 > 10) {
int analogValueA = map(analogValue1, 0, 1023, 85, 0);
servoMotor.write(analogValueA);
}
// read the value of the sensor:
int analogValue2 = analogRead(analogPinB);
if (analogValue2 > 10) {
int analogValueB = map(analogValue2, 0, 1023, 85, 179);
servoMotor.write(analogValueB);
}
delay(10);
}
I see what your saying with the conflicting inputs. This could definitely be a problem for my project. For this loop sensorB can override the position of the servo when sensorA is being used, but not vice versa.
How can i create a code that will allow me to override one sensor if the other is of greater value???
How can i create a code that will allow me to override one sensor if the other is of greater value???
Read both sensors. Figure out which returned the larger value. Use that value, and ignore the other.
int val1 = analogRead(pin1);
int val2 = analogRead(pin2);
if(val1 > val2)
{
// use val1
}
else
{
// use val2
}
that works like a charm, thankyou.
Everything is working well except for one thing.When i hold one of the force sensors down and keep the servo in a stationary position, after about one second it will jerk back to the center (with an displacement of a degree or so) and then come back to its original stationary position. Then it will remain stationary for another second, and repeat this action.
-How do i stop the unwanted movement??
How do i stop the unwanted movement??
Give it a stern talking-to.
Works every time.
Or you could find the source of the bad readings causing the problem.
I suggest some debug prints.
I figured it out. I just took out the centering commands in void setup and in void loop. heres the final code.
// JOE
// These constants won't change:
#include <Servo.h> // include the servo library
Servo servoMotor;
const int analogPinA = A0; // Pin that sensorA is attached to
const int analogPinB = A1; // Pint that sensorB is attached to
const int servoPin = 2; // Pin that servo is attached to
//---------------------------
void setup() {
// initialize the servo setup:
servoMotor.attach(servoPin);
// attach servo to appropriate pin:
}
//-----------------------------
void loop() {
// read the value of sensorA:
int analogValue1 = analogRead(analogPinA);
int analogValue2 = analogRead(analogPinB);
// if the analog value is high enough, move servo øº:
if (analogValue1 > analogValue2) {
int analogValueA = map(analogValue1, 0, 1023, 85, 179);
servoMotor.write(analogValueA);
}
// read the value of sensorB:
if (analogValue2 > analogValue1) {
int analogValueB = map(analogValue2, 0, 1023, 85, 0);
servoMotor.write(analogValueB);
}
delay(20);
}
//---------------------------------
thanks everyone