C to C ++ Arduino compatibility.

Hello,
I would like to use this code under Arduino can you help me make it compatible?

int main() {
  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

  printf("LowerPos: %d UpperPos: %d Range: %d T180: %.10f UsRange: %.10f\n", LowerPos, UpperPos, Range, T180, UsRange);
  for (int PosDeg = 0; PosDeg < 100; ++PosDeg) {
    double PosUs = LowerPos + (PosDeg * UsRange); // degree in μS
    printf("PosDeg: %d PosUs: %.10f\n", PosDeg, PosUs);
  }
  return 0;
}

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...

  Serial.print("PosDeg: ");
  Serial.print(PosDeg);
  Serial.print(" PosUs: ");
  Serial.print(PosUs);
  Serial.println();

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 many Arduinos, double is just a synonym for float, so you will not get as much precision as you expect.

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;
}

That piece of code is a ridiculous thing to run on an Arduino. Run it on a PC. Better still, run it in Excel.

Regards,
Ray L.

RayLivingston:
That piece of code is a ridiculous thing to run on an Arduino.

That was my thought initially too. It may not be if the mention in the comments referring to a servo indicate where this is going.

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.

nerixs:
How to block and alert the sending of command out of fields (0 ° to 173 °)?

Use assert() fonction, eg.:

#include <assert.h>

uint8_t var;
void setup() {
 
  Serial.begin(250000);
  var = 200;
}

void loop() {

  if (var > 173) {
    assert(false);
  }

}

ard_newbie:
Use assert() fonction, eg.:

  if (var > 173) {

assert(false);
 }

I think the assert() function is intended to be used like this:

  assert(var <= 173);