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?

5
  • 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. Commented Apr 5, 2019 at 15:20
  • @josecanuc, Please refer to the question, since I have edited the question
    – haas
    Commented Apr 5, 2019 at 16:09
  • what does this mean? i press my finger
    – jsotola
    Commented Apr 5, 2019 at 18:30
  • I am using flex sensor, it gives the signals by bending your finger @jsotola
    – haas
    Commented Apr 5, 2019 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
    Commented Apr 6, 2019 at 12:24

1 Answer 1

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.

3
  • 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
    Commented Apr 5, 2019 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
    Commented Apr 5, 2019 at 21:43
  • Why do you think that? If you want help from the public here, you have to provide actual data. Commented Apr 5, 2019 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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