I am a beginner and need some help with code and the lcd, and I would really appreciate some help!

Hello, I am new to using the arduino and this forum so please just tell me if I am doing something completely wrong. I am using an arduino uno that came in the the starter kit. Could someone please tell me how I can write code for pressing a button when I have a counter counting to 120 on the lcd and if the counter is 120 or over when I press the button, the screen displays a message, but if the counter is lower than 120 when I press the button. it displays a different message. I have the lcd wired up but I do not know how to write the code to do that.

I also do not know how to incorporate the button in both wiring it up and writing the code.

If someone could help me, I would very much appreciate it. If this is obviously not possible and I just don't know the limits of the arduino, that would also be helpful to know. And again, if I am going about this problem really wrong, or there are just other better resources I can go to, or if there is any other information that would be helpful, I would very much appreciate it.

Thank you in advance for the help!

Is this a school assignment?

Can you write fixed messages to the LCD?

What exact LCD do you have? Post a photo or a link the the LCD datasheet.

Post the code that you have tried so far. Read the forum guidelines to see how to properly post code and some information on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Your post was MOVED to its current location as it is more suitable.

This is not a school assignment, this is for Mark Robers Monthly class. I can write fixed messages to the LCD. I am using the code that is the "Hello World" in the example tab. Here it is

/*
  LiquidCrystal Library - Hello World

  Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  library works with all LCD displays that are compatible with the
  Hitachi HD44780 driver. There are many of them out there, and you
  can usually tell them by the 16-pin interface.

  This sketch prints "Hello World!" to the LCD
  and shows the time.

  The circuit:
   LCD RS pin to digital pin 12
   LCD Enable pin to digital pin 11
   LCD D4 pin to digital pin 5
   LCD D5 pin to digital pin 4
   LCD D6 pin to digital pin 3
   LCD D7 pin to digital pin 2
   LCD R/W pin to ground
   LCD VSS pin to ground
   LCD VCC pin to 5V
   10K resistor:
   ends to +5V and ground
   wiper to LCD VO pin (pin 3)

  Library originally added 18 Apr 2008
  by David A. Mellis
  library modified 5 Jul 2009
  by Limor Fried (http://www.ladyada.net)
  example added 9 Jul 2009
  by Tom Igoe
  modified 22 Nov 2010
  by Tom Igoe
  modified 7 Nov 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Teeth brushing");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}

I used the IDE autoformat tool before sending this, but it just said that there was nothing to format. I just do not know how to write the code to do what I said in my first post. I tried to post a picture of the LCD but it says that new users can not upload attachments. I have the LCD that was provided with the starter kit, and it has 2 rows and 16 columns.

This guy should share your tuition fees with the ppl here who help him teach.

Have you covered, I dunno, variables, mathematical and logical expressions, if/else statements and so forth?

The hard part at all is getting the LCD to show “HELLO WORLD!” or “brushing teeth!”, whatever, so good on you and that is the hard part.

The functionally you seek to add is trivially simlple and completely within the Arduino’s capabilities… have you seen all the amazing things that have been done with a little UNO cranking away merrily?

More easy to say what isn’t possible. :expressionless:

a7

1 Like

First, does the basic "hello world" run properly? That should tell you if you have things plugged in right.

Then, I'd go through and do all the basics in this tutorial.

Hopefully, this will give you a better understanding of how the fundamentals work.

For plugging in the other stuff, what are you using? Do you have a button already?

So you have a counter which is typically an integer variable. You can make it a global variable, like your 'lcd' variable, by declaring it outside any function.

Here are four ways to increment your counter variable (let's call it 'Counter':

Counter = Counter + 1;
Counter += 1;
Counter++;
++Counter;

To display the count on the LCD:
lcd.print(Counter);

Something like:

if (myButtonPressed) {
  if (myCounter >= 120) {
    // do over 120 and over stuff
  }
  else {
    // do under 120 stuff
  }
}

I did get it to work and I do have the button! Thank you!

That is very helpful, thank you! I am new to code and appreciate it a lot!

Thank you so much! This is what I needed! How do I use the code to tell the arduino where the button is?

pinMode() Arduino Reference - Arduino Reference

I am a beginner, so please help me with anything I am doing wrong The goal of my build to have a stopwatch on the LCD, and I want to press a button and If the stopwatch is at a time greater or equal to 120 then the LCD says a message. If the stopwatch is less than 120 and the button is pressed then it displays a different message. Could someone please tell me what is wrong with my code.

//The goal of this is to have a stopwatch on the LCD, and I want to press a button and If the stopwatch is at a time greater or equal to 120 then the LCD says a message. 
//If the stopwatch is less than 120 when the button is pressed then it displays a different message.


#define BUTTON_PIN 7
#include <LiquidCrystal.h>
#include <Bounce.2>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  pinMode(buttonPin, INPUT)
  lcd.print ("Tooth Brushing Time")
void loop() {
  lcd.setCursor(0, 0);
    lcd.print("SW (ss:)");
    lcd.setCursor(0, 1);
    lcd.print("                "); 
    unsigned long durSS = (elapsedMillis/1000);    //Seconds
}
  if (buttonstate == HIGH)
  if (unsigned long durSS >=w 120000Millis/1000) {
    lcd.print("Good Job! You did it!")
  }
  else {
  if (buttonstate == HIGH)
  if (unsigned long durSS < 120000Millis/1000) 
    lcd.print("Keep brushing, you are not there yet")
  }
}

Thank you in advance for the help!

No matching closing }

No line terminating ;

When asking for help say what the code does and what you want it to do.

Also say when it won't compile and post the error massages you get.

All this is outside any function, you can't do that. And lots of missing ; at the end of statements that need them.

void setup() {
  pinMode(buttonPin, INPUT)
  lcd.print ("Tooth Brushing Time")

should be

void setup() {
  pinMode(buttonPin, INPUT);
  lcd.print ("Tooth Brushing Time");
}
void loop() {
  lcd.setCursor(0, 0);
    lcd.print("SW (ss:)");
    lcd.setCursor(0, 1);
    lcd.print("                "); 
    unsigned long durSS = (elapsedMillis/1000);    //Seconds
}

That } ends the loop() function. The code lines after that are not in any function, which is not allowed.

Ok thank you, so here is the new code

//The goal of this is to have a stopwatch on the LCD, and I want to press a button and If the stopwatch is at a time greater or equal to 120 then the LCD says a message. 
//If the stopwatch is less than 120 when the button is pressed then it displays a different message.


#define BUTTON_PIN 7
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  pinMode(BUTTON_PIN 7, INPUT);
  lcd.print ("Tooth Brushing Time");
}
void loop() {
  lcd.setCursor(0, 0);
    lcd.print("SW (ss:)");
    lcd.setCursor(0, 1);
    lcd.print("                "); 
    unsigned long durSS = (elapsedMillis/1000);    //Seconds
}
  if (buttonstate == HIGH)
  if (unsigned long durSS >=w 120000Millis/1000) {
    lcd.print("Good Job! You did it!")
  }
  else {
  if (buttonstate == HIGH)
  if (unsigned long durSS < 120000Millis/1000) 
    lcd.print("Keep brushing, you are not there yet")
  }
}

Theoretically it is supposed to make a stopwatch that displays different messages when you press a button and if the stopwatch is greater than or less than 120. I have been copying things from other code that I have found, but I am new to this coding so I don't know what it all really means. Here are the error messages:

Arduino: 1.8.19 (Windows 10), Board: "Arduino Uno"

C:\Users\nelso\Documents\Arduino\Toothbrushing\Toothbrushing.ino: In function 'void setup()':

Toothbrushing:10:22: error: expected ')' before numeric constant

pinMode(BUTTON_PIN 7, INPUT);

                  ^

C:\Users\nelso\Documents\Arduino\Toothbrushing\Toothbrushing.ino: In function 'void loop()':

Toothbrushing:18:28: error: 'elapsedMillis' was not declared in this scope

 unsigned long durSS = (elapsedMillis/1000);    //Seconds

                        ^~~~~~~~~~~~~

C:\Users\nelso\Documents\Arduino\Toothbrushing\Toothbrushing.ino: At global scope:

Toothbrushing:20:3: error: expected unqualified-id before 'if'

if (buttonstate == HIGH)

^~

Toothbrushing:24:3: error: expected unqualified-id before 'else'

else {

^~~~

Toothbrushing:29:1: error: expected declaration before '}' token

}

^

exit status 1

expected ')' before numeric constant

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

So should I take out all of the } before the last one, or should I rewrite void loop where there is more brackets?

There is so much wrong with the code. You need to review basic C/C++ syntax and code structure.

  1. Your setup() function has no terminating brace.
  2. Your loop() function is prematurely terminated.
  3. buttonPin is not defined or initialized anywhere
  4. elapsedMillis is not defined or initialized anywhere
  5. buttonState is not defined or initialized anywhere
  6. You cannot define a variable inside an if expression
  7. You have a lot of statements without a semicolon

The last point in my post has not been addressed.
Remove that } after the loop function definition and make sure your braces match.

Well you can but the scope of that variable will be limited to inside that if statement.

1 Like