Tue Dec 20, 2016 8:28 pm
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
//declare all the variables needed for the game :
int chain = 1;
int chance = 0;
int fchain = 1;
void setup() {
// put your setup code here, to run once:
// initialize the Gamebuino object
gb.begin();
// show the start menu
gb.titleScreen(F("Shiny Probability"));
//random values
pinMode(chain, OUTPUT);
if (chain <= 69)
chance = chain / 4096;
if (chain >= 70)
chance = chain / 1024;
if (chain >= 256)
chain = 1;
}
void loop() {
// put your main code here, to run repeatedly:
if (gb.update()) {
//prints Chain length on the screen
gb.display.println(F("Chain Length:")Serial.print(fchain))
gb.display.println(F("Chance of Shiny:")Serial.print(chance))
if (gb.buttons.pressed(BTN_A)) {
chain = chain + 1
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain - 1
}
}
}
Tue Dec 20, 2016 8:48 pm
if (chain <= 69)
chance = chain / 4096;
if (chain >= 70)
chance = chain / 1024;
if (chain >= 256)
chain = 1;
if (chain <= 69) {
chance = chain / 4096;
}
if (chain >= 70) {
chance = chain / 1024;
}
if (chain >= 256){
chain = 1;
}
int chance = 0;
float chance = 0;
chance = chain / 4096;
chance = chain / 4096.0;
gb.display.println(F("Chain Length:")Serial.print(fchain))
gb.display.println(F("Chance of Shiny:")Serial.print(chance))
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance);
if (gb.buttons.pressed(BTN_A)) {
chain = chain + 1
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain - 1
}
if (gb.buttons.pressed(BTN_A)) {
chain = chain + 1;
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain - 1;
}
if (gb.buttons.pressed(BTN_A)) {
chain++;
}
if (gb.buttons.pressed(BTN_B)) {
chain--;
}
Tue Dec 20, 2016 9:10 pm
Sorunome wrote:There are a few issues with your code:
First of all, this isn't really an issue but more like bad habit which can lead to seemingly random errors:
- Code:
if (chain <= 69)
chance = chain / 4096;
if (chain >= 70)
chance = chain / 1024;
if (chain >= 256)
chain = 1;
In this block, while this would work as it are all one-line if conditions, it is still recommended to add the curly brackets as else, when adding another line and you forget adding the curly brackets, the code does unexpected things.
So it would look like this:
- Code:
if (chain <= 69) {
chance = chain / 4096;
}
if (chain >= 70) {
chance = chain / 1024;
}
if (chain >= 256){
chain = 1;
}
Second of all, you defined your variable 'chance' as an integer (Keyword int), however due to the context it seems like you actually want a floating point number (Keyword float), as it seems like you want to store the probability between 0 and 1 in it. An integer is only a whole number, so like 0,1,2,3 etc,
So instead of
- Code:
int chance = 0;
you'd do
- Code:
float chance = 0;
There is another thing to take into consideration here: You are deviding integers with one another, meaning that you are performing integer devision. So like 2/4 will actually return 0 instead of 0.5 as those are both integers, and thus the result is floored (everything after the comma is simply cut off).
To overgo this you'd replace lines such as
- Code:
chance = chain / 4096;
withHere I force one of the arguments to be a floating point number, due to the value being 4096.0 rather than 4096, and thus floating point devision is performed.
- Code:
chance = chain / 4096.0;
Now let's get to your syntax error (at least from what I could spot, if it still doesn't compile please copypaste us the error output :3 )
- Code:
gb.display.println(F("Chain Length:")Serial.print(fchain))
gb.display.println(F("Chance of Shiny:")Serial.print(chance))
I am not sure what you were trying to do here, were you trying to gb.display.println or were you trying to do Serial.print ?
If you were trying to do gb.display.println:If you wanted to do Serial.print then substitude accordingly.
- Code:
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance);
Then there is also this block:
- Code:
if (gb.buttons.pressed(BTN_A)) {
chain = chain + 1
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain - 1
}
It simply lacks semi-columns:
- Code:
if (gb.buttons.pressed(BTN_A)) {
chain = chain + 1;
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain - 1;
}
There are also shorthands for increasing and decreasing numbers:
- Code:
if (gb.buttons.pressed(BTN_A)) {
chain++;
}
if (gb.buttons.pressed(BTN_B)) {
chain--;
}
Whew, this post got long, I hope I could help!
Tue Dec 20, 2016 10:29 pm
Xenospark wrote:WOW! Thank you that was such a fast response, and it must have taken a lot of effort. It verifies fine now, but I'm still having some issues. One thing is, when I load it up, pressing A and B does nothing to the values of "chain".
Percentages are simply floating point numbers multiplied by 100.Also, This is supposed to be an odds calculator, and the odds are pretty darn low. So low that the probability showed up as 0.00 and then stopped. Is there a way I can either convert it to percentage or let it go to at least the 6 digits past the decimal? Thanks again, and sorry for not including which line had the problem earlier >.<
Tue Dec 20, 2016 10:53 pm
Sorunome wrote:Xenospark wrote:WOW! Thank you that was such a fast response, and it must have taken a lot of effort. It verifies fine now, but I'm still having some issues. One thing is, when I load it up, pressing A and B does nothing to the values of "chain".
My bad, in your code you were trying to output the value of the variable fchain rather than of chain, I can't see fchain being used elsewhere so you might as well remove that value I guess.
Also maybe the chance-recalculation should happen after changing the chain-variable?Percentages are simply floating point numbers multiplied by 100.Also, This is supposed to be an odds calculator, and the odds are pretty darn low. So low that the probability showed up as 0.00 and then stopped. Is there a way I can either convert it to percentage or let it go to at least the 6 digits past the decimal? Thanks again, and sorry for not including which line had the problem earlier >.<
I'm sorry, I don't know out of the top of my head how to output more digits with gb.display.print, however Serial.print (which sends it via the usb thingy to the serial monitor in your arduino IDE) displays more, I think
Tue Dec 20, 2016 10:57 pm
Xenospark wrote:So do you think changing it all out for Serial.print would let me multiply it by 100 and then display a bit farther?[...]
Wed Dec 21, 2016 12:06 am
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
// put your setup code here, to run once:
// initialize the Gamebuino object
gb.begin();
// show the start menu
gb.titleScreen(F("Shiny Probability"));
//random values
pinMode(chain, OUTPUT);
if (chain <= 69) {
chance = chain / 4096.0;
}
if (chain >= 70) {
chance = chain / 1024.0;
}
if (chain >= 256) {
chain = 1;
}
}
void loop() {
// put your main code here, to run repeatedly:
if (gb.update()) {
//prints Chain length on the screen
if (gb.buttons.pressed(BTN_A)) {
chain = chain ++;
fchain = fchain ++;
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain --;
fchain = fchain --;
}
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance*100);
}
}
Wed Dec 21, 2016 6:25 am
Xenospark wrote:Here is my code now. even at 100 times it just shows .02 no matter how many times I press A. Also, the chain display does not change from 1.
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
// put your setup code here, to run once:
// initialize the Gamebuino object
gb.begin();
// show the start menu
gb.titleScreen(F("Shiny Probability"));
//random values
pinMode(chain, OUTPUT);
}
void reCalcChance() {
if (chain <= 69) {
chance = chain / 4096.0;
}
if (chain >= 70) {
chance = chain / 1024.0;
}
if (chain >= 256) {
chain = 1;
}
}
void loop() {
// put your main code here, to run repeatedly:
if (gb.update()) {
//prints Chain length on the screen
if (gb.buttons.pressed(BTN_A)) {
chain = chain ++;
fchain = fchain ++;
reCalcChance();
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain --;
fchain = fchain --;
reCalcChance();
}
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance*100);
}
}
Hang in there!I'm determined to get this to work but it's a little frustrating haha (such is coding I guess ).
Sorry, I don't know what you mean with this question :/If I do Serial.print (chain)(DEC6) would that let it go past two decimals?
Wed Dec 21, 2016 3:12 pm
Sorunome wrote:Xenospark wrote:Here is my code now. even at 100 times it just shows .02 no matter how many times I press A. Also, the chain display does not change from 1.
That is because you have to re-calculate chance every time you change chain. Try this:
- Code:
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//importe the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;
//declare all the variables needed for the game :
int chain = 1;
float chance = 0;
int fchain = 1;
void setup() {
// put your setup code here, to run once:
// initialize the Gamebuino object
gb.begin();
// show the start menu
gb.titleScreen(F("Shiny Probability"));
//random values
pinMode(chain, OUTPUT);
}
void reCalcChance() {
if (chain <= 69) {
chance = chain / 4096.0;
}
if (chain >= 70) {
chance = chain / 1024.0;
}
if (chain >= 256) {
chain = 1;
}
}
void loop() {
// put your main code here, to run repeatedly:
if (gb.update()) {
//prints Chain length on the screen
if (gb.buttons.pressed(BTN_A)) {
chain = chain ++;
fchain = fchain ++;
reCalcChance();
}
if (gb.buttons.pressed(BTN_B)) {
chain = chain --;
fchain = fchain --;
reCalcChance();
}
gb.display.print(F("Chain Length:"));
gb.display.println(fchain);
gb.display.print(F("Chance of Shiny:"));
gb.display.println(chance*100);
}
}
Also what's the point of the fchain variable? Why have two variables for the same thing?Hang in there!I'm determined to get this to work but it's a little frustrating haha (such is coding I guess ).Sorry, I don't know what you mean with this question :/If I do Serial.print (chain)(DEC6) would that let it go past two decimals?
Serial.print(F("Chain Length:"));
Serial.print(fchain);
Serial.print(F("Chance of Shiny:"));
Serial.print(chance*100, 5);
Wed Dec 28, 2016 6:16 pm