0

Rx Code:

#include <Servo.h>
Servo myServo;

void setup() {
Serial.begin(9600);
myServo.attach(9);
delay(5000);
myServo.write(0);
}

void loop() {
  while(Serial.available()==0);

    int d = Serial.read() - '0';
    if(d>0)
    {
      int pos = map(d,0,9,0,180);
      pos = constrain(pos,0,180);
      myServo.write(pos);
      Serial.println(d);

      delay(50);
      }
      else
      {};

}

Tx code:

int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int SensorMid = 0;
const int POWER_OF_TWO_TO_AVERAGE = 6;

void setup() {
pinMode(13,OUTPUT);
  Serial.begin(9600);

  while (millis() < 10000) {
    digitalWrite(13,HIGH);
    sensorValue = analogRead(A1);
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }
  }
  SensorMid = (sensorMin + sensorMax )/2;
  digitalWrite(13,LOW);
  }

void loop() {
int val = map(analogRead(A1),SensorMid,sensorMax,0,9);
Serial.println(val);
delay(100);
}

I am building a robotic arm and I am facing some problems regrading to the servos stability with the flex, I am using the flex as the transmitter and the servo as the receiver, and when I press my finger my servo doesn't move properly.

Any suggestions?

  • Please show your code (a minimal, complete example that demonstrates the problems), your wiring diagram or schematic, and a more detailed description of the "doesn't move properly" issue. Otherwise, all anyone can do is make uninformed guesses. – jose can u c Apr 5 '19 at 15:20
  • @josecanuc, Please refer to the question, since I have edited the question – haas Apr 5 '19 at 16:09
  • what does this mean? i press my finger – jsotola Apr 5 '19 at 18:30
  • I am using flex sensor, it gives the signals by bending your finger @jsotola – haas Apr 5 '19 at 20:46
  • Have you tried taking a mean of for example 10 or 20 consecutive readings? All analog signals have noise on them, which you can reduce by taking the mean value – chrisl Apr 6 '19 at 12:24
1

Without knowing more about the actual problem of "doesn't move properly", what I initially see is that you are transmitting values 0-9 by using Serial.println(), which sends the byte data, and also a newline (ASCII value 10), so your RX code is probably getting alternating input of the intended value, and the value 10, which is getting subtracted from 48 (the ASCII value of '0'), mapped and constrained to 0.

Try changing your TX script to call Serial.print() instead. Your receiver doesn't care about each value showing up on a new line -- it doesn't know about lines.

  • What I mean by it doesn't move properly, that when I try bending the flex sensor, I guess that the servo doesn't receive the data properly due to the flex sensor resolution. – haas Apr 5 '19 at 20:48
  • I think that the servo is not stable because that the flex is keep sending values even when I’m not touching it, how can I solve this problem @jose can u c – haas Apr 5 '19 at 21:43
  • Why do you think that? If you want help from the public here, you have to provide actual data. – jose can u c Apr 5 '19 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.