Need help understanding Arduino code

Hello.

I am trying to build a game on the lcd with arduino.
To learn I am analyzing code from other people who have created games.

I came up to a part of code which I cannot figure out and was hoping that I could get some help.

void setup() 
{
  lcd.begin(16, 2);
  lcd.createChar(5, ballMid[0]);
  lcd.createChar(6, ballMid[1]);
  pinMode(6, INPUT);

  randomSeed(analogRead(0));
  
  bVelX = random(30, 60) / 100.0 * (random(0, 2) == 0 ? 1 : -1);
  bVelY = random(10, 30) / 100.0 * (random(0, 2) == 0 ? 1 : -1);
  
  Serial.begin(9600);
}

The game in this code is sort of ping pong with a ball and a player versus AI computer.

As I understand correctly the randomSeed is used so that numbers could be randomly generated later in the code.

The main concerns I have is with bVel as I assume these stand for ball velocity x and y.
What I am wondering is what all of this calculation means. Is this some sort of formula for calculating velocity?

Also what do the : and ? do in this part (random(0, 2) == 0 ? 1 : -1);
I was looking for answers online but couldnt find anything about the operators : and ?

And another thing is why is the Serial.begin needed? I was reading about it online but I cannot understand why it is necessary to have it here.

Thank you so much for the help!!

No, it is used to have different sequences of pseudo random numbers on each run.
It can be useful to repeat exact the same steps (like in debugging a game) or a random sequence,
so it is not automatically seeded.

It is kind of an inline if then else, but value based.
<condition> ? <valueForTrue> : <valueForFalse>
The whole expression is just a value, that depends.

It is only needed if you want to send or receive data over the serial port,
which is usually the attached PC.

1 Like

The expression randomly picks a number that is either -1 or +1.

1 Like

So this statement is saying...

 bVelX = random(30, 60) / 100.0 * (random(0, 2) == 0 ? 1 : -1);

x axis velocity is randomly between 30 & 59 (/ 100 so 0.30 - 0.59) - it's speed), and is randomly positive or negative (moving left or right)

1 Like

Thank you so much everyone!

This helped me out tremendously!

Understood most of the code by now but I have another question.

double clamp(double val, double min, double max)
{
  return val > max ? max : val < min ? min : val;
}

This function seems extremely complicated to me and even with knowing ternary operator I cannot figure it out.

Could someone explain what the function does with the provided values?

I am sorry if these are very newbie questions. I am very new to programming

It is constraining val to values between min and max.

return val > max ? max : val < min ? min : val;

could be

  if (val > max) {
    return max;
  }
  if (val < min) {
    return min;
  }
  return val;

Not much better to read, what do you think?

1 Like

Personally it is a lot easier to read it this way.
But I guess you just have to get used to the whole ? : thing.
It's the first time I'm seeing it so it's very confusing for me right now :smiley:

An inline function constrain(what, min, max) would make the intent clearer,
and it would not really matter how verbose its implementation is.
The generated code for both versions will be very close, if not identical.

The usefulness of the ternary operator is (only?) that it can select between different expressions, where a conditional statement like 'if-else' can only select between different statements.

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