# Servo Motor Response From Tfmini Output Data

**URL:** <https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937>\
**Category:** Sensors\
**Created:** [December 1, 2021, 5:00am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937 "2021-12-01T05:00:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 1, 2021, 5:00am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/1 "2021-12-01T05:00:13Z")

</div>

Hi! I'm trying to create a simple script where my servo motor moves to a 90 degree position while the distance measured by a tfmini is \<=5 and moves to 0 degrees otherwise.

This is the code that I have and I'm not too sure why it's not working. The motor is only essentially jittering back and forth in place.  
I'm using an arduino uno.

```cpp

#include <SoftwareSerial.h> //header file of software serial port
#include<Servo.h> // Servo Library
SoftwareSerial Serial1(0, 1); //define software serial port name as Serial1 and define pin0 as RX & pin1 as TX

// Define TFMini Variables
int dist; //actual distance measurements of LiDAR in cm
int strength; //signal strength of LiDAR
int check; //save check value
int i;
int uart[9]; //save data measured by LiDAR
const int HEADER = 0x59; //frame header of data package

// Define Servo Motor Control and Variables
Servo myservo; //Creating servo object to control the servo
int pos=0; // Angular position of servo motor

// TFMini Setup & Measurement Loop
void setup()
{
  Serial.begin(9600); //set bit rate of serial port connecting Arduino with computer
  Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
  myservo.attach(9); //Attaches servo object to pwm pin 9
}
 
 
void loop() {
  if (Serial1.available()) //check if serial port has data input
  {
    if (Serial1.read() == HEADER) //assess data package frame header 0x59
    {
      uart[0] = HEADER;
      if (Serial1.read() == HEADER) //assess data package frame header 0x59
      {
        uart[1] = HEADER;
        for (i = 2; i < 9; i++) //save data in array
        {
          uart[i] = Serial1.read();
        }
        check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
        if (uart[8] == (check & 0xff)) //verify the received data as per protocol
        {
          dist = uart[2] + uart[3] * 256; //calculate distance value
          strength = uart[4] + uart[5] * 256; //calculate signal strength value
          Serial.print("dist = ");
          Serial.print(dist); //output measure distance value of LiDAR
          Serial.print('\t');
          Serial.print("strength = ");
          Serial.print(strength); //output signal strength value
          Serial.print('\n');
          
          while (dist < 10);{
            myservo.write(90);
          }
          
          while (dist>5){
            myservo.write(0);
          }
                }
              }
            }
          }
        }

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 1, 2021, 7:18am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/2 "2021-12-01T07:18:46Z")

</div>

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use **code tags** (the \</\> icon above the compose window)&nbsp;to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 1, 2021, 7:20am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/3 "2021-12-01T07:20:14Z")

</div>

```cpp
SoftwareSerial Serial1(0, 1); //define software serial port name as Serial1 and define pin0 as RX & pin1 as TX

```

Why are you using the hardware Serial pins with SoftwareSerial ?

---

<div class="post-metadata">

**Author:** ![JCA34F](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jca34f/32/340148_2.png) [@JCA34F](https://forum.arduino.cc/u/JCA34F)\
**Post date:** [December 1, 2021, 7:47am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/4 "2021-12-01T07:47:38Z")

</div>

Which Arduino?

---

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 1, 2021, 12:10pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/5 "2021-12-01T12:10:24Z")

</div>

Thank you! I've made a few changes.

---

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 1, 2021, 12:12pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/6 "2021-12-01T12:12:31Z")

</div>

Uno R3

---

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 1, 2021, 12:14pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/7 "2021-12-01T12:14:43Z")

</div>

For that portion, I was following a sample code that got the sensor to measure distance. I don't have any particular reason why those pins are used.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 1, 2021, 12:59pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/8 "2021-12-01T12:59:05Z")

</div>

When using a Uno do not use pins 0 and 1 for SoftwareSerial. Choose another pair of pins

---

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 1, 2021, 1:11pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/9 "2021-12-01T13:11:04Z")

</div>

Which pins would you suggest?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 1, 2021, 2:42pm UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/10 "2021-12-01T14:42:49Z")

</div>

Any of them apart from 0 and 1

---

<div class="post-metadata">

**Author:** ![thesenate66](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@thesenate66](https://forum.arduino.cc/u/thesenate66)\
**Post date:** [December 2, 2021, 4:33am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/11 "2021-12-02T04:33:15Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 31, 2022, 4:33am UTC](https://forum.arduino.cc/t/servo-motor-response-from-tfmini-output-data/930937/12 "2022-05-31T04:33:36Z")

</div>

Closed on May 31, 2022, 4:33am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
