Go Down

Topic: How to use a "?" when coding for servo control (Read 644 times) previous topic - next topic



I'm trying to figure out what a bit of code does.  I will paste it below and highlight the line that is confusing me.  I found this bit of code on the lynxmotion forums and I can't get a hold of anyone who can explain it to me.  I understand the purpose of everything generally BUT the glowing yellow portion.  What does that question mark do?

    int Delta = NewPosition - aiLastPositions[iServo];
    int CycleTime = 20;
    int CntOfCycles = Time / 20; 
    int DeltaPerCycle = Delta/CntOfCycles;
    if (DeltaPerCycle  == 0)
      // Not enough change to do it so...
      DeltaPerCycle = (Delta > 0)?1 : -1;
      CntOfCycles = abs(Delta);
      CycleTime = Time/CntOfCycles;


That's a fancy way of writing an 'if' statement.
DeltaPerCycle = (Delta > 0)?1 : -1;
That line is the same as:

Code: [Select]
if (Delta > 0){
  DeltaPerCycle = 1;
  DeltaPerCycle = -1;


That's a fancy succinct and potentially more flexible way of writing an 'if' statement


Thanks everyone, that really helps me analyze this code better!

Go Up