Hi.
I about to set a project related to flex sensor and servo motor. The project is that when the flex sensor bend, the servo motor will be move clockwise. But what I get at the test is that the servo motor turn anticlockwise when bend the flex sensor. How was it to be happen? Is it a coding error? Hope for the advises from the experts. Code show in below is my coding which I get from open source. Thanks in advance to all the expert.
Best Regards
Leey
#include <Servo.h>
int flexpin=A0;
int flexpin1=A1;
int pos=90;
int pos1=90;
Servo hip, knee;
int flex[20];
int flex1[20];
int flexsum=0;
int flexsum1=0;
void setup()
{
hip.attach(9);
knee.attach(10);
Serial.begin(9600);
}
void loop()
{
for(int x=0; x<20; x++)
{
flex[x]=analogRead(flexpin);
flex1[x]=analogRead(flexpin1);
flexsum=flexsum+analogRead(flexpin);
flexsum1=flexsum1+analogRead(flexpin1);
delayMicroseconds(14);
}
flexsum=flexsum/20;
flexsum1=flexsum1/20;
if(Serial.available())
{
Serial.println(flexsum);
Serial.println(flexsum1);
delay(100);
}
pos=map(flexsum,160,238,0,90);
pos1=map(flexsum1,300,440,0,90);
hip.write(pos);
knee.write(pos1);
delay(200);
}