Go Down

Topic: Arduino LCD number adding (Read 385 times) previous topic - next topic

Tobias00

Hi there, im trying to get this code to work but I'm having a bit of trouble. My final objective is to get four buttons hooked up an LCD and make one button print num=1, button 2 print num=2, button 3 print num=5 and button 4 print num=10. Each time you press them they add up to a final result. I think I have to use  00 = addition; function and int num too. Im really not sure and I would appreciate some help. I think it's a fairly simple project but for some reason I can't get it to work. What I want is to add numbers,1 ,2, 5 and 10 to a final result.

#include <LiquidCrystal.h>
int value_in1=0;
int value_in2=0;
int value_in3=0;
int value_in4=0;
int flag1=0;
int flag2=0;
int flag3=0;
int flag4=0;
int number=0;
int input1=0;
int oper=0;
int output=0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup() {
  lcd.begin(16, 2);
  lcd.print("Numbers");
  delay(1000);
   pinMode(7,INPUT);
  pinMode(8,INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
   
}
 
void loop() {
  value_in1=digitalRead(7);
  value_in2=digitalRead(8);
  value_in3=digitalRead(9);
  value_in4=digitalRead(10);
  lcd.clear();
if ((value_in1==HIGH)&&(flag1==0)){
    flag1=1;
    }
    else {
      input1=input1+1;
    }
    number=input1;
  }
 
  else if (value_in1==LOW){
    input1=input1;
    flag1=0;
 

  lcd.setCursor(0,1);
      lcd.print(1);   
  }
  else{
  delay(200);
  }
}


Notes: Im not a experienced programmer, I think it's kind of a mess. I can't get it to print o work.
Tobias A.

0AlphaOmega

Curly braces are a mess ({ })
Is that your question? or do you need to fix them before posting?

And that smiley is going to take some debugging - Mod, can we have code tags please?
For whom does the clock pulse? It pulses for you!

0AlphaOmega

Okay, I have it running (well, not throwing errors)
Is value_in1 - 4 supposed to be an array?

You only deal with value_in1


using your code and adding int x - get rid of the else's for now....
if (value_in1==HIGH)){
   x=1;
   }
.
.
.

if (value_in4==HIGH)){
   x=10;
   }

 lcd.setCursor(0,1);
     lcd.print(x);  
 }

you need to do whatever you want with your flag etc.
For whom does the clock pulse? It pulses for you!

robtillaart

Slightly modified/stripped your code to get a working start
(code not tested)
Code: [Select]


#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int number = 0;

void setup()
{

  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);

  lcd.begin(16, 2);
  lcd.print("Numbers");

  delay(1000);
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print(number);
}

void loop()
{
  // CHECK BUTTONS
  boolean keyPressed = false;
  int value_1 = digitalRead(7);
  int value_5 = digitalRead(8);
  int value_10 = digitalRead(9);
  int reset = digitalRead(10);


  // DO THE MATH
  if (value_1 == HIGH)
  {
    number += 1;
    keyPressed = true;
  }
  if (value_5 == HIGH)
  {
    number += 5;
    keyPressed = true;
  }
  if (value_10 == HIGH)
  {
    number += 10;
    keyPressed = true;
  }
  if (reset == HIGH)
  {
    number = 0;
    keyPressed = true;
  }


  // DISPLAY RESULTS 
  // but only needed to refresh when key is pressed
  if (keyPressed)
  {
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print(number);
    delay(200);        // time to release key
  }


}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy