On a microcontroller, main() should not return. There is no operating system to return to. I never tried it, but I expect that it will just stop and become non-responsive.
Change "main" to "loop" and add an empty setup() function. Then it will probably begin to work on your Arduino.
printf() won't print floating-point on a standard Arduino without a little extra work. It's usually better to just print the things in order. For example, instead of printf("PosDeg: %d PosUs: %.10f\n", PosDeg, PosUs); you should write...
There's no standard output on an Arduino. printf() on its own doesn't go anywhere. You have to specify if you want to print to Serial or wherever you want these prints to go.
On a DUE, a double is a 8 bytes float variable with a precision of 15 decimals, whereas (as stated above), a double is exactly a float on an AVR board.
Open the serial port. Use Serial.print() instead of printf. And flush the output before exiting or the last buffer full of text will never make it out.
int main()
{
Serial.begin(115200);
const int LowerPos = 520 ; //Mini stop of the servo
const int UpperPos = 2480 ; //Maxi stop of the servo
const int Range = 173 ; //Range of motion in degrees
const double T180 = LowerPos + (((double)(UpperPos - LowerPos) / Range) * 180) ; //Extrapolation to 180° = 2559,306358381503
const double UsRange = (T180 - LowerPos) / 180 ; // 1° = 11,32944444444444 µS
Serial.print("LowerPos: ");
Serial.print(LowerPos);
Serial.print(" UpperPos: ");
Serial.print(UpperPos);
Serial.print(" Range: ");
Serial.print(Range);
Serial.print(" T180: ");
Serial.print(T180, 10);
Serial.print(" UsRange: ");
Serial.println(UsRange, 10);
for (int PosDeg = 0; PosDeg < 100; ++PosDeg)
{
double PosUs = LowerPos + (PosDeg * UsRange); // degree in μS
Serial.print("PosDeg: ");
Serial.print(PosDeg);
Serial.print(" PosUs: ");
Serial.println(PosUs, 10);
}
Serial.flush();
return 0;
}
Hello,
I tried to simplify things!
The purpose of this code is to find the value μs closest to reality by respecting the minimum and maximum stops and the actual stroke of a servo.
The final program only accepts μS commands, they will be sent in “int”.
#include <Servo.h>
const byte ServoPin = 10;
const int LowerPos = 520 ; //Mini stop of the servo
const int UpperPos = 2480 ; //Maxi stop of the servo
const int Range = 173 ; //Range of motion in degrees
const float T180 = LowerPos + (((float)(UpperPos - LowerPos) / Range) * 180) ; //Extrapolation to 180° = 2559,306358381503
const float UsRange = (T180 - LowerPos) / 180 ; // 1° = 11,32944444444444 µS
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(115200);
myservo.attach(ServoPin, LowerPos, T180);
Serial.println("Welcome to the serial monitor.");
Serial.print("The 180° extrapolation of ");
Serial.print(Range);
Serial.print("° between the mini stop: ");
Serial.print(LowerPos);
Serial.print(" µS and the maximum stop: ");
Serial.print(UpperPos);
Serial.print(" μS is equal to: ");
Serial.print (T180,10);
Serial.println(" µS.");
Serial.print("1° Equals ");
Serial.print(UsRange,10);
Serial.println(" µS.");
Serial.println();
}
void loop() {
int PosDeg = Serial.parseInt(); // PosDeg = the position entered in the serial monitor from 0° to 173°
float PosUs = LowerPos + (PosDeg * UsRange); // degree in µS
if (Serial.available() > 0) {
Serial.print(PosDeg);
Serial.print("° = ");
// Serial.print(PosUs,10);
Serial.print(PosUs,0);
Serial.println(" µS");
}
clearSerialBuffer();
}
void clearSerialBuffer() {
//clear serial buffer (but do nothing with it)
while (Serial.available())
{
Serial.read();
}
}
How to block and alert the sending of command out of fields (0 ° to 173 °)?
I did not understand the use of the constrain command.