Hi, i was assigned to do the project of showing welcome in LCD after pressing a button, after 1s it goes off and then it shows the temperature and brightness concurrently, at the same time.
This is my code :
#include <LiquidCrystal.h>
int potPin = 0;
int value = 0;
const int led = 13;
const int LED = 9;
const int pushButton = 7;
int count = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonState = 0;
void setup() {
lcd.begin(16, 2);
pinMode(led,OUTPUT);
pinMode(LED,OUTPUT);
pinMode(pushButton,INPUT_PULLUP);
lcd.noDisplay();
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(pushButton);
if (digitalRead(pushButton) == LOW)
{
count = count + 1;
digitalWrite(LED,LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("welcome!");
lcd.display();
delay(1000);
lcd.display();
}
else
{
digitalWrite(LED,HIGH);
}
double val;
double temp;
val=analogRead(A0);
Serial.println(val);
temp = val/1023*5/0.01;
lcd.setCursor(0,0);
lcd.print("temp:");
lcd.print(temp);
value=analogRead(A1);
Serial.println(value);
if(value<800)
{
digitalWrite(led, LOW);
}
else
{
digitalWrite(led, HIGH);
}
lcd.setCursor(0, 1);
lcd.print("brightness:");
lcd.print(value);
}
edit: it finally works!! thank you to everyone who is helping me out! really appreciate it! ^^
You need to do things when the button input changes state (button becomes pressed), not while it is pressed. The Arduino State Change example shows you how to do that.
You need to do things when the button input changes state (button becomes pressed), not while it is pressed. The Arduino State Change example shows you how to do that.
File>Examples>0.2Digital>StateChangeDetection
Please use code tags "</>" when posting code.
hello, ive tried the whole process and it turns out even worse, my "welcome" wont show up and directly show Temperature and Brightness. I edited the code, can you help check it out again T^t
You still aren't using the State Change approach. Have you studied the example carefully?
This probably does not do what you intended:
while (digitalRead(pushButton) == LOW <200)
(LOW<200) is always true (or 1, or HIGH) so the while statement loop is executed if digitalRead(pushButton) is HIGH.
At the moment, your code is a mishmash. You are trying to do too many things at once.
Start with something simple, like just displaying "Welcome" when the button BECOMES pressed, then erasing the screen after 1 second. Then work your way up.
I want to make a suggestion about these basic examples.
This "basic" buttonstate example is so "basically" that it is too simple.
Any modern programming language is based on defining methods, functions, procedures.
So the very first example should be showing a very very simple definition of a function and how to call this function.
Then the other examples should always use a function for whatever these examples want to do.
I 'm very aware of that this means - at least in some cases - that the writers of the examples have to think pretty hard
"how can I pack this example into a function?"
But it will lead the newbees into the right direction. Coding with functions. The questions "how do I combine two sketches" will almost disappear.
StefanL38:
I want to make a suggestion about these basic examples.
This "basic" buttonstate example is so "basically" that it is too simple.
Any modern programming language is based on defining methods, functions, procedures.
So the very first example should be showing a very very simple definition of a function and how to call this function.
Then the other examples should always use a function for whatever these examples want to do.
I 'm very aware of that this means - at least in some cases - that the writers of the examples have to think pretty hard
"how can I pack this example into a function?"
But it will lead the newbees into the right direction. Coding with functions. The questions "how do I combine two sketches" will almost disappear.
Hi Stefan,
Probably worth posting in Development / Suggestions for the Arduino project. It annoys me too that demonstration code is shown directly in loop(), not as a function, the consequence is seen over and over on here in huge programs written entirely in loop() with no functions. However, there is no way to write teaching material that suits everyone, or even most people. My pet annoyance is blink because it teaches people to rely on delay().
jremington:
You still aren't using the State Change approach. Have you studied the example carefully?
This probably does not do what you intended:
while (digitalRead(pushButton) == LOW <200)
(LOW<200) is always true (or 1, or HIGH) so the while statement loop is executed if digitalRead(pushButton) is HIGH.
At the moment, your code is a mishmash. You are trying to do too many things at once.
Start with something simple, like just displaying "Welcome" when the button BECOMES pressed, then erasing the screen after 1 second. Then work your way up.
thankyou for the suggestion, I will redo from the beginning again and see if it works,, if the time is possible tho;; my teacher is being too harsh :') i have to submit in few hours over 1 session class learning;; and I don't really have much basic of programming language so it is new to me :')
jremington:
You still aren't using the State Change approach. Have you studied the example carefully?
This probably does not do what you intended:
while (digitalRead(pushButton) == LOW <200)
(LOW<200) is always true (or 1, or HIGH) so the while statement loop is executed if digitalRead(pushButton) is HIGH.
At the moment, your code is a mishmash. You are trying to do too many things at once.
Start with something simple, like just displaying "Welcome" when the button BECOMES pressed, then erasing the screen after 1 second. Then work your way up.
hi! actually I have redone everything from scratch and i managed to do it! however, when the temp and brightness able to show up, the value wont change. the lines are correct tho since i tried it without the button functions and it worked! do you know where are my mistake? i have edited the code above..
StefanL38:
I want to make a suggestion about these basic examples.
This "basic" buttonstate example is so "basically" that it is too simple.
Any modern programming language is based on defining methods, functions, procedures.
So the very first example should be showing a very very simple definition of a function and how to call this function.
Then the other examples should always use a function for whatever these examples want to do.
I 'm very aware of that this means - at least in some cases - that the writers of the examples have to think pretty hard
"how can I pack this example into a function?"
But it will lead the newbees into the right direction. Coding with functions. The questions "how do I combine two sketches" will almost disappear.
It is easy to understand and a good mixture between writing about important concepts
and get you going.
best regards Stefan
@the community: if you know of other tutorials that are really good please post them or send me a PM
thank you for the tutorials, I just hope I have enough time to watch them, I almost done with my code and honestly my teacher is too rashed to assign me this hard(for me) homework by only a session class i dont even have basic programming so I am trying to get by and submit this hw.. thank you for the recommendations!
"value" and "temp"will only get updated and displayed when digitalRead(pushButton) == LOW because they are inside the "if".
If you want them update and displayed when the button is not pressed, better move them outside the if statement.
Please don't edit your original post and change the code posted. It ruins the whole flow of the thread and invalidates previous answers. If you modify your code post it again in the new post. Then we can see the progress you are making.
pcbbc:
"value" and "temp"will only get updated and displayed when digitalRead(pushButton) == LOW because they are inside the "if".
If you want them update and displayed when the button is not pressed, better move them outside the if statement.
Please don't edit your original post and change the code posted. It ruins the whole flow of the thread and invalidates previous answers. If you modify your code post it again in the new post. Then we can see the progress you are making.
thankyou!! after seeing this it instantly works!! thankyou for the help!