0
\$\begingroup\$

I'm trying to display a random fruit on an lcd and I was wondering if there's a built in function in the MikroC PRO for pic v.6.4.0 library that does so.

I drew the fruit using the lcd custom character tool and I'm using the generate code option.I would really appreciate your help. Here's the generated code:

 void fruit1(char pos_row, char pos_char) {
  char i;
    Lcd_Cmd(64);
    for (i = 0; i<=7; i++) Lcd_Chr_CP(fruit[i]);
    Lcd_Cmd(_LCD_RETURN_HOME);
    Lcd_Chr(pos_row, pos_char, 0);
}
\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to me that drawing random fruit would be a very common function in a standard library. Also, is there a question in there somewhere? \$\endgroup\$ – alex.forencich Mar 12 '16 at 0:02
1
\$\begingroup\$

You need a random number.

That's what the STDLIB rand() is for.

Function returns a sequence of pseudo-random numbers between 0 and 32767. The function will always produce the same sequence of numbers unless srand is called to seed the start point.

Divide the result of result = rand() by the number of fruit you have to bring it down to a smaller range.

Then just use that number.

 if (result <= 30) then fruit1();
 else if (result <= 60) then fruit2();
 etc.

Use srand() to change the random number seed, or you will get the same set of "random" numbers eventually.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.