762x39
November 4, 2021, 9:59am
1
Good day to all,
Please assist in coding, I need the word "SMILIES" to blink on my display please.
Herewith the code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int x = 0;
int input = A0;
int state = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" ROUND COUNTER ");
lcd.setCursor(0, 1);
lcd.print(x);
lcd.print(" = SMILIES ");
}
void loop()
{
int counter = digitalRead(A0);
if (state == 0)
{
switch (counter) {
case 1 : state = 1; lcd.setCursor (0, 1); x = x + 1; lcd.print(x); break;
case 0 : state = 0; break;
}
}
}
Print the string "SMILIES", then sometime later print a string containing the same number of spaces as letters in "SMILIES".
Don't forget to position the cursor each time
Please remember to use code tags when posting code
762x39
November 4, 2021, 10:13am
3
Hi,
Appoligies re the code tags, I am new on the forum.
Thank you for the code, I did write it to the unit but SMILIES are not blinking, or am I not doing something correct?
You don't appear to be printing the spaces I suggested.
762x39
November 4, 2021, 10:18am
5
I do not want to be spoonfed as I really want to learn, ok I will try and print the spaces,
Thanks for your responses.
762x39
November 4, 2021, 10:25am
6
I would be grateful if you could post the entire code that I would need, my knowledge is limited at this early stage of this project.
Thank you very much
and
is somehow contradictory.
Post the last version of your sketch, in a new post, in code tags,
previously formatted with ctrl-T in the IDE.
Half an hour later...
So get your spoon ready and switch the brain off, if it was on.
#include <LiquidCrystal_I2C.h>
uint16_t x = 0;
const uint8_t input = A0;
bool smiliesVisible = true;
uint32_t lastSwap;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(input, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(F(" ROUND COUNTER "));
lcd.setCursor(0, 1);
lcd.print(x);
lcd.print(F(" = SMILIES "));
}
void loop() {
bool intputActive = digitalRead(input) == LOW;
if (intputActive ) {
lcd.setCursor (0, 1);
lcd.print(++x);
}
if (millis() - lastSwap >= 250) {
lastSwap = millis();
lcd.setCursor(lenNum(x), 1);
lcd.print(smiliesVisible ? F(" ") : F(" = SMILIES"));
smiliesVisible = not smiliesVisible;
}
}
uint8_t lenNum(uint16_t check) {
if (check < 10) {
return 1;
}
if (check < 100) {
return 2;
}
if (check < 1000) {
return 3;
}
if (check < 10000) {
return 4;
}
return 5;
}
762x39
November 4, 2021, 11:16am
9
Thank you very much, it is really a big help.
AWESOME !!!!!!!!!
I probably used a different way to attach the button (closing to ground),
and I changed the x, which should have a more descriptive name, to an unsigned type.
An overflow to zero sounds more reasonable than overflowing into negative numbers.
That sketch basically just does what @anon73444976 suggested.
762x39
November 4, 2021, 12:13pm
11
It appears that the counter is simply adding numbers like crazy, without even activating the ir sensor
So you probably have to remove the pinMode, and swap the logic of the signal.
762x39:
without even activating
I hope it only counts when the signal is not activated.
I hope you meant to say "it counts up like crazy".
It would really speed up, if it was not slowed down by all the LCD printing.
Your initial design was "count while the signal is active", and you wanted to blink text,
so I assumed that the while was a design choice.
If you want to count single events, here a version that sports that
(again with a button closing to ground as an input signal)
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
// https://github.com/thomasfredericks/Bounce2
uint16_t x = 0;
const uint8_t input = A0;
bool smiliesVisible = true;
uint32_t lastSwap;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Bounce button;
void setup() {
button.attach(input, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(F(" ROUND COUNTER "));
lcd.setCursor(0, 1);
lcd.print(x);
lcd.print(F(" = SMILIES "));
}
void loop() {
if (button.update() && button.fell()) {
lcd.setCursor (0, 1);
lcd.print(++x);
}
if (millis() - lastSwap >= 250) {
lastSwap = millis();
lcd.setCursor(lenNum(x), 1);
lcd.print(smiliesVisible ? F(" ") : F(" = SMILIES"));
smiliesVisible = not smiliesVisible;
}
}
uint8_t lenNum(uint16_t check) {
if (check < 10) {
return 1;
}
if (check < 100) {
return 2;
}
if (check < 1000) {
return 3;
}
if (check < 10000) {
return 4;
}
return 5;
}
1 Like
762x39
November 4, 2021, 4:49pm
13
This is exactly what I was after, I clearly did not convert my exact requirements in to text, thank you sir. I wish I had you on speed dial. lol
You would probably not like to spend that much money.
Have a look at the Bounce2 library, it is lean and easy to grasp,
and you don't have to program the state change detection and debouncing by hand.
You should nevertheless program both actions at least once, to understand what is going on.
762x39
November 4, 2021, 5:15pm
16
Understood, thanks again.
system
Closed
May 3, 2022, 5:16pm
17
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.