variables

hello,

I’m in need of help again. This code is more for practice; i will load it to make sure it functions correctly. This is a magic the gathering life counter.

In my code I want “life” and “poison” to be names of variables that hold numerical values. so that the lcd will display the value of “life” and “poison” not the name (word) of the variable.

Also if “++life;” increments life by one; how would I increment life by five?

#include <LiquidCrystal.h>




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int mode = 8;
int add5life = 16;
int minus5life = 10;
int addlife = 15;
int minuslife = 6;
int addpoison = 7;
int minuspoison = 9;
int life;
int poison;
int poisonmax;
int gameover;



void setup()
{
  pinMode(add5life, INPUT);
  pinMode(minus5life, INPUT);
  pinMode(addlife, INPUT);
  pinMode(minuslife, INPUT);
  pinMode(addpoison, INPUT);
  pinMode(minuspoison, INPUT);
  pinMode(mode, INPUT);
  pinMode(gameover, OUTPUT);

  lcd.begin(16, 2);

  if (digitalRead(mode) == HIGH)
  {
    life == 40;
    poisonmax == 20;
  }

  else
  {
    life == 20;
    poisonmax == 10;
  }


}

void loop()
{
  lcd.begin(0, 1);
  lcd.print("LIFE : life");
  lcd.begin(1, 1);
  lcd.print("POISON : poison");


  if (life <= 0)
    gameover == HIGH;

  if ( poison >= poisonmax)
    gameover == HIGH;



  if (digitalRead(add5life) == HIGH)
    ++life;

  if (digitalRead(minus5life) == HIGH)
    --life;

  if (digitalRead(addlife) == HIGH)
    ++life;

  if (digitalRead(minuslife) == HIGH)
    --life;

  if (digitalRead(addpoison) == HIGH)
    ++poison;

  if (digitalRead(minuspoison) == HIGH)
    --poison;
}

Also if "++life;" increments life by one; how would I increment life by five?

life += 5;

Pay attention to the difference between ++life and life++. The latter is more typical.

    life == 40;
    poisonmax == 20;

Wrong. The = operator is the assignment operator. The == operator is the comparison operator. They are NOT interchangeable.

  lcd.print("LIFE : life");

That is going to print a string literal. That is NOT going to know that it is supposed to understand that there is a variable name in there somewhere.

   lcd.print("Life : ");
   lcd.print(life);

joshp689: In my code I want "life" and "poison" to be names of variables that hold numerical values. so that the lcd will display the value of "life" and "poison" not the name (word) of the variable.

Then don't include them in the text.

lcd.print("LIFE : ");
lcd.print(life);

joshp689: Also if "++life;" increments life by one; how would I increment life by five?

life += 5;

And some tips. You can use the build in pullups for the buttons instead of external pull down resistors. Use pinMode(pin, INPUT_PULLUP) and digitalRead(LOW) for a pressed button (inverted from now)

Also, the loop runs very fast so a statement like

 if (digitalRead(add5life) == HIGH)
    ++life;

Will run multiple times when you press the button (because the code is ran way faster then you can press a button. Also, you want some debounce in it. To make it al easy, have a look at the Bounce library and use button.fell() (if you use (the internal) pullups) to detect if the button is pressed since the last time you checked. This way the statement is only run once for every press.

add5life etc is a bit of a vague name. I had to read the rest to see it's a button. So use clearer names like

const byte ButtonAdd5life = 16; //also make it a const byte :)

Update

forgot to add debouce, I will do that tonight.

#include <LiquidCrystal.h>




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int mode = 8;
int add5life = 16;
int minus5life = 10;
int addlife = 15;
int minuslife = 6;
int addpoison = 7;
int minuspoison = 9;
int life;
int poison;
int poisonmax;
int gameover;



void setup()
{
  pinMode(add5life, INPUT);
  pinMode(minus5life, INPUT);
  pinMode(addlife, INPUT);
  pinMode(minuslife, INPUT);
  pinMode(addpoison, INPUT);
  pinMode(minuspoison, INPUT);
  pinMode(mode, INPUT);
  pinMode(gameover, OUTPUT);

  lcd.begin(16, 2);

  if (digitalRead(mode) == HIGH)
  {
    life = 40;
    poisonmax = 20;
  }

  else
  {
    life = 20;
    poisonmax = 10;
  }


}

void loop()
{
  lcd.begin(0, 1);
  lcd.print("LIFE : ");
  lcd.print(life);
  lcd.begin(1, 1);
  lcd.print("POISON : ");
  lcd.print(poison);


  if (life <= 0)
    gameover == HIGH;

  if ( poison >= poisonmax)
    gameover == HIGH;



  if (digitalRead(add5life) == HIGH)
    life += 5;

  if (digitalRead(minus5life) == HIGH)
    life -= 5;

  if (digitalRead(addlife) == HIGH)
    ++life;

  if (digitalRead(minuslife) == HIGH)
    --life;

  if (digitalRead(addpoison) == HIGH)
    ++poison;

  if (digitalRead(minuspoison) == HIGH)
    --poison;
}
    gameover == HIGH;

= and == are NOT interchangeable.

Here is an update, im not sure if the way I debounced the button will work.

I want it to keep and display the new value of “life”, I thought that was the ++ before “life” not after

if (digitalRead(minus5life) == HIGH)
    delay(10);
    if (digitalRead(minus5life) == HIGH)
      life -= 5;

  if (digitalRead(addlife) == HIGH)
    delay(10);
    if (digitalRead(addlife) == HIGH)
      ++life;

.

#include <LiquidCrystal.h>




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int mode = 8;
int add5life = 16;
int minus5life = 10;
int addlife = 15;
int minuslife = 6;
int addpoison = 7;
int minuspoison = 9;
int life;
int poison;
int poisonmax;
int gameover;

//int laststateadd5life;
//int laststateminus5life;
//int lastestateaddlife;
//int laststateminuslife;
//int lastestateaddpoison;
//int lastestateminuspoison;


void setup()
{
  pinMode(add5life, INPUT);
  pinMode(minus5life, INPUT);
  pinMode(addlife, INPUT);
  pinMode(minuslife, INPUT);
  pinMode(addpoison, INPUT);
  pinMode(minuspoison, INPUT);
  pinMode(mode, INPUT);
  pinMode(gameover, OUTPUT);

  lcd.begin(16, 2);

  if (digitalRead(mode) == HIGH)
  {
    life = 40;
    poisonmax = 20;
  }

  else
  {
    life = 20;
    poisonmax = 10;
  }


}

void loop()
{
  lcd.begin(0, 1);
  lcd.print("LIFE : ");
  lcd.print(life);
  lcd.begin(1, 1);
  lcd.print("POISON : ");
  lcd.print(poison);


  if (life <= 0)
    gameover = HIGH;

  if ( poison >= poisonmax)
    gameover = HIGH;



  if (digitalRead(add5life) == HIGH)
    delay(10);
    if (digitalRead(add5life) == HIGH)
      life += 5;

  if (digitalRead(minus5life) == HIGH)
    delay(10);
    if (digitalRead(minus5life) == HIGH)
      life -= 5;

  if (digitalRead(addlife) == HIGH)
    delay(10);
    if (digitalRead(addlife) == HIGH)
      ++life;

  if (digitalRead(minuslife) == HIGH)
    delay(10);
    if (digitalRead(minuslife) == HIGH)
      --life;

  if (digitalRead(addpoison) == HIGH)
    delay(10);
    if (digitalRead(addpoison) == HIGH)
      ++poison;

  if (digitalRead(minuspoison) == HIGH)
    delay(10);
    if (digitalRead(minuspoison) == HIGH)
      --poison;
}
  if (digitalRead(addlife) == HIGH)
    delay(10);
    if (digitalRead(addlife) == HIGH)
      ++life;

Indenting means nothing. You need to get into the habit, NOW, of ALWAYS using curly braces so the compiler knows where YOU think the code block starts and ends.

Sorry im playing with my code before work; Fixed.

Edit* still not right. I will fix after work.

#include <LiquidCrystal.h>




LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int mode = 8;
int add5life = 16;
int minus5life = 10;
int addlife = 15;
int minuslife = 6;
int addpoison = 7;
int minuspoison = 9;
int life;
int poison;
int poisonmax;
int gameover;

//int laststateadd5life;
//int laststateminus5life;
//int lastestateaddlife;
//int laststateminuslife;
//int lastestateaddpoison;
//int lastestateminuspoison;


void setup()
{
  pinMode(add5life, INPUT);
  pinMode(minus5life, INPUT);
  pinMode(addlife, INPUT);
  pinMode(minuslife, INPUT);
  pinMode(addpoison, INPUT);
  pinMode(minuspoison, INPUT);
  pinMode(mode, INPUT);
  pinMode(gameover, OUTPUT);

  lcd.begin(16, 2);

  if (digitalRead(mode) == HIGH)
  {
    life = 40;
    poisonmax = 20;
  }

  else
  {
    life = 20;
    poisonmax = 10;
  }


}

void loop()
{
  lcd.begin(0, 1);
  lcd.print("LIFE : ");
  lcd.print(life);
  lcd.begin(1, 1);
  lcd.print("POISON : ");
  lcd.print(poison);


  if (life <= 0)
    gameover = HIGH;

  if ( poison >= poisonmax)
    gameover = HIGH;



  if (digitalRead(add5life) == HIGH)
  {{
      delay(10);
    }
    if (digitalRead(add5life) == HIGH)
    {
      life += 5;
    }
  }

  if (digitalRead(minus5life) == HIGH)
  {{
      delay(10);
    }
    if (digitalRead(minus5life) == HIGH)
    {
      life -= 5;
    }
  }

  if (digitalRead(addlife) == HIGH)
  {{
      delay(10);
    }
    if (digitalRead(addlife) == HIGH)
    {
      ++life;
    }

    if (digitalRead(minuslife) == HIGH)
    {{
        delay(10);
      }
      if (digitalRead(minuslife) == HIGH)
      {
        --life;
      }
    }

    if (digitalRead(addpoison) == HIGH)
    {{
        delay(10);
      }
      if (digitalRead(addpoison) == HIGH)
      {
        ++poison;
      }
    }

    if (digitalRead(minuspoison) == HIGH)
    {{
        delay(10);
      }
      if (digitalRead(minuspoison) == HIGH)
      {
        --poison;
      }
    }
  }

{{ never looks good.

perhaps you meant

  if (digitalRead(add5life) == HIGH)
  {
      delay(10);
      if (digitalRead(add5life) == HIGH)
      {
        life += 5;
      }
  }

You have a lot of code that looks alike. You read the state of a pin. You wait. You read the state of the same pin. If the switch at that pin is still pressed, you do something.

Make a function that takes a pin number. Have it return a boolean, true means that the switch is pressed for some period of time, false means that it is not.

Call that function, and, if it returns true, do whatever needs doing.

Don't write the same code over and over and over.

I tried to write a function and it compiles so there’s no errors; but that doesn’t mean it will function correctly.
Take a look and let me know if you see something wrong

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int mode = 8;
int add5life = 16;
int minus5life = 10;
int addlife = 15;
int minuslife = 6;
int addpoison = 7;
int minuspoison = 9;
int life;
int poison;
int poisonmax;
int gameover;

void ButtonPress(int button, int life)
{
if(digitalRead(button) == HIGH)
  {
    delay (10);
  }
   if (digitalRead(button) == HIGH)
   {
    life;
   } 
}

void setup()
{
  pinMode(add5life, INPUT);
  pinMode(minus5life, INPUT);
  pinMode(addlife, INPUT);
  pinMode(minuslife, INPUT);
  pinMode(addpoison, INPUT);
  pinMode(minuspoison, INPUT);
  pinMode(mode, INPUT);
  pinMode(gameover, OUTPUT);

  lcd.begin(16, 2);

  if (digitalRead(mode) == HIGH)
  {
    life = 40;
    poisonmax = 20;
  }

  else
  {
    life = 20;
    poisonmax = 10;
  }
}

void loop()
{
  lcd.begin(0, 1);
  lcd.print("LIFE : ");
  lcd.println(life);
  lcd.begin(1, 1);
  lcd.print("POISON : ");
  lcd.println(poison);


  if (life <= 0)
    gameover = HIGH;

  if ( poison >= poisonmax)
    gameover = HIGH;

  ButtonPress(add5life, life += 5);
  ButtonPress(minus5life, life -= 5);
  ButtonPress(addlife, life++);
  ButtonPress(minuslife, life--);
  ButtonPress(addpoison, poison++);
  ButtonPress(minuspoison, poison--);


}
    life;

What is this supposed to do? That variable is local to the function. When it ends, the variable memory is reused. Nothing is returned to the caller.

Make a function that takes a pin number.

You did that.

Have it return a boolean

void is not boolean.

C (and C++) is a pass-by-value language. The value that was in life is incremented, or decremented, and passed BY VALUE to the function. The function does nothing with the value. The value that was passed to the function is lost, except for the side effects of the way that you are incrementing and decrementing life and poison in loop() in the function calls.

I don't understand the boolean part, I know what they do but I'm unsure on how to use it.

but I'm unsure on how to use it.

Create a function:

boolean switchReallyIsPressed(byte pinNum)
{
   boolean isPressed = false;
   if(digitalRead(pinNum) == HIGH)
   {
      delay(10);
      if(digitalRead(pinNum) == HIGH)
      {
         isPressed = true;
      }
   }
   return isPressed;
}

Call it:

   if(switchReallyIsPressed(add5life))
      life += 5;

Thank you I get that, I don't use void because every thing that happens in void****() stays in that void and no other part can use the information there?

joshp689:
Here is an update, im not sure if the way I debounced the button will work.

See this: http://forum.arduino.cc/index.php?topic=125297

joshp689: Thank you I get that, I don't use void because every thing that happens in void****() stays in that void and no other part can use the information there?

No.

// The void, in void****() is the return type. In this case nothing is returned.

void nothingIsReturnedFunction();

// this function returns an int type
int anIntIsReturnedFunction()
{
     int n = 5;
     return n;
}

// this function returns a boolean
boolean aBooleanIsReturned()
{
     return false;
}

In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:

type name ( parameter1, parameter2, ...) { statements }

From this page.