Go Down

### Topic: Why are Float points not working? Short Servo Example (Read 2910 times)previous topic - next topic

#### 777funk

##### Oct 12, 2012, 02:15 am
I have been trying to learn to program over the past couple years. I had a class on C++ when I was in college but I hated it and as a EE student I figured it wasn't that important. Dumb move!

Now that I'm self employed I'm trying to get better at automation for my little shop, anyways, I have been messing with this program (example) for a while.

I had float points working at one point bt for some reason it's not working now. Any ideas what could be wrong here?

Code: [Select]
`// Sweep// by BARRAGAN <http://barraganstudio.com> // This example code is in the public domain.#include <Servo.h> Servo myservo;  // create servo object to control a servo                 // a maximum of eight servo objects can be created float pos = 0;    // variable to store the servo position float left = 85;float right = 65;float posinc = 1; void setup() {   myservo.attach(9);  // attaches the servo on pin 9 to the servo object } void loop() {   for(pos = right; pos < left; pos += posinc)  // goes from 0 degrees to 180 degrees   {                                  // in steps of 1 degree     myservo.write(pos);              // tell servo to go to position in variable 'pos'     delay(60);                       // waits 15ms for the servo to reach the position   }   for(pos = left; pos>=right; pos-=posinc)     // goes from 180 degrees to 0 degrees   {                                    myservo.write(pos);              // tell servo to go to position in variable 'pos'     delay(60);                       // waits 15ms for the servo to reach the position   } } `

#### Delta_G

#1
##### Oct 12, 2012, 02:24 am
What do you mean not working?  What is it doing or not doing that isn't what you expect?

By the way, since the increment is 1 you really don't need a float.  Regular old into will do.
If at first you don't succeed, up - home - sudo - enter.

#### MarkT

#2
##### Oct 12, 2012, 02:48 am
If it was working and the code hasn't changed then it must be the hardware.  If the code has changed then look at what was changed.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### 777funk

#3
##### Oct 12, 2012, 03:00 am
Sorry! That was super unclear on my description.

What's not working is this... I have the servo taped to the table at my computer desk and I have the arm set to hit an unfixtured (free) piece and slide it to tell me if anything is changing. This tells me how wide the sweep is. If I run it to say 88.1 degrees then change it to 88.9 degrees, nothing changes in the sweep distance (as determined by the visual on my loose sliding piece I use to reference) but as soon as I go to the next Integer (89) it hits the piece telling me that it's now going a degree further.

As far as it was working before and now it's not, I'm not sure what I did but I had float points actually making changes before (as verified several times with the moving boundary described above.

So I am totally lost as to what's wrong with the code. It should give me float points in the degrees of sweep, no?

#### AWOL

#4
##### Oct 12, 2012, 03:07 am
Quote
It should give me float points in the degrees of sweep, no?

Why?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### PeterH

#5
##### Oct 12, 2012, 03:13 am

It should give me float points in the degrees of sweep, no?

Look at the interface you're using to define the servo position. If it takes an int argument and you try to provide a float value then all that'll happen is your float value is rounded to an int.

#### 777funk

#6
##### Oct 12, 2012, 03:20 amLast Edit: Oct 12, 2012, 03:24 am by 777funk Reason: 1
To AWOL, I'm not sure what you mean by "why?" I looked at your link and it doesn't say anything about float points. And the fact that I have had it working tells me that float values "should" do something. I believe I had more math in the statement where it worked but I'm positive that I noticed decimal point resolution before.

Thank you PeterH on your reply, I don't quite understand what you mean by interface. I have the Arduino board connected directly to a small hobby RC Servo. I'd guess you mean the Arduino as the interface?

Edit: Wow you UK guys are up late!

#### AWOL

#7
##### Oct 12, 2012, 03:23 am
The Servo write method has no overloads that accept a float, only integers, so the float you supply is simply truncated to an integer.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### zoomkat

#8
##### Oct 12, 2012, 05:38 am
The degree value for the servo position is just being mapped to a us value for the servo. If you want to make small servo movements, you will need to use us values. Below is servo test code where you can compare degree values to us values.

Code: [Select]
`// zoomkat 10-22-11 serial servo test// type servo position 0 to 180 in serial monitor// or for writeMicroseconds, use a value like 1500// for IDE 0022 and later// Powering a servo from the arduino usually *DOES NOT WORK*.String readString;#include <Servo.h> Servo myservo;  // create servo object to control a servo void setup() {  Serial.begin(9600);  myservo.writeMicroseconds(1500); //set initial servo position if desired  myservo.attach(7);  //the pin for the servo control   Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded}void loop() {  while (Serial.available()) {    char c = Serial.read();  //gets one byte from serial buffer    readString += c; //makes the string readString    delay(2);  //slow looping to allow buffer to fill with next character  }  if (readString.length() >0) {    Serial.println(readString);  //so you can see the captured string     int n = readString.toInt();  //convert readString into a number    // auto select appropriate value, copied from someone elses code.    if(n >= 500)    {      Serial.print("writing Microseconds: ");      Serial.println(n);      myservo.writeMicroseconds(n);    }    else    {         Serial.print("writing Angle: ");      Serial.println(n);      myservo.write(n);    }    readString=""; //empty for next input  } }`