#include <LiquidCrystal.h>
const int RS_Pin = A3;
const int EN_Pin = A4;
const int D4_Pin = 13;
const int D5_Pin = 12;
const int D6_Pin = 11;
const int D7_Pin = 10;
const int FirstButtonPin = 5;
const int SecondButtonPin = 6;
const int ThirdButtonPin = 7;
const int Latch_Pin = A2;
const int Data_Pin = A0;
const int Clock_Pin = A1;
bool FirstButtonPressed = false;
bool SecondButtonPressed = false;
bool ThirdButtonPressed = false;
LiquidCrystal lcd(RS_Pin, EN_Pin, D4_Pin, D5_Pin, D6_Pin, D7_Pin);
String LCD_Text = "Amrin Nursultan IT3-2203";
String substrings[3];
int substringsCount = 0;
int LCD_TextSize = LCD_Text.length();
int counter_value = 0;
const uint8_t digits[] = {
0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11111000,
0b10000000,
0b10010000,
};
void setup() {
lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
pinMode(FirstButtonPin, INPUT);
pinMode(SecondButtonPin, INPUT);
pinMode(ThirdButtonPin, INPUT);
pinMode(Latch_Pin, OUTPUT);
pinMode(Clock_Pin, OUTPUT);
pinMode(Data_Pin, OUTPUT);
randomSeed(analogRead(0));
reset(); // Ensure counter starts at 0 on first launch
}
void loop() {
if (digitalRead(FirstButtonPin) == HIGH && !FirstButtonPressed) {
FirstButtonPressed = true;
HorizontalText1();
} else if (digitalRead(FirstButtonPin) == LOW) {
FirstButtonPressed = false;
}
if (digitalRead(SecondButtonPin) == HIGH && !SecondButtonPressed) {
SecondButtonPressed = true;
RandomText();
} else if (digitalRead(SecondButtonPin) == LOW) {
SecondButtonPressed = false;
}
if (digitalRead(ThirdButtonPin) == HIGH && !ThirdButtonPressed) {
ThirdButtonPressed = true;
reset();
} else if (digitalRead(ThirdButtonPin) == LOW) {
ThirdButtonPressed = false;
}
}
void VerticalText() {
lcd.clear();
lcd.begin(20, 4);
int row = 0;
int col = 0;
int counter = 0;
for (int i = 0; i < LCD_TextSize; i++) {
lcd.setCursor(col, row);
lcd.print(LCD_Text.charAt(i));
incrementCounter();
delay(500);
if (row == 3) {
col += 2;
row = 0;
} else {
row += 1;
}
delay(250);
}
}
void HorizontalText () {
lcd.clear ();
lcd.begin (20, 4);
int col = 0;
int counter = 0;
for (int i = 0; i < LCD_TextSize; i++) {
lcd.setCursor (col, 0);
lcd.print(LCD_Text.charAt(i));
incrementCounter();
delay(300);
}
}
void HorizontalText1() {
lcd.clear();
lcd.begin(20, 4);
int position = 0;
int length = LCD_Text.length();
unsigned long previousMillis = 0;
int scrollSpeed = 300;
while (true) {
if (millis() - previousMillis > scrollSpeed) {
previousMillis = millis();
lcd.clear();
lcd.setCursor(0, 0);
String displayText = "";
for (int i = 0; i < 20; i++) {
if (position + i < length) {
displayText += LCD_Text.charAt(position + i);
} else {
displayText += LCD_Text.charAt((position + i) % length);
}
}
lcd.print(displayText);
position++;
if (position >= length) position = 0;
}
}
}
void RandomText() {
handleString(LCD_Text);
lcd.clear();
lcd.begin(20, 4);
const char possibleChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int lineLengths[3] = {substrings[0].length(), substrings[1].length(), substrings[2].length()};
int lineStarts[3] = {
(20 - lineLengths[0]) / 2, // Центрирование первой строки
(20 - lineLengths[1]) / 2, // Центрирование второй строки
(20 - lineLengths[2]) / 2 // Центрирование третьей строки
};
unsigned long startTime = millis();
int duration = 7000;
while (millis() - startTime < duration) {
for (int row = 0; row < 3; row++) {
lcd.setCursor(0, row);
for (int col = 0; col < 20; col++) {
if (col >= lineStarts[row] && col < lineStarts[row] + lineLengths[row]) {
int index = col - lineStarts[row];
int charTime = startTime + (float)(index + 1) / lineLengths[row] * duration;
if (millis() > charTime) {
lcd.print(substrings[row][index]);
} else {
int randomIndex = random(sizeof(possibleChars) - 1);
lcd.print(possibleChars[randomIndex]);
}
} else {
lcd.print(' ');
}
}
}
delay(150);
}
for (int row = 0; row < 3; row++) {
lcd.setCursor(lineStarts[row], row);
lcd.print(substrings[row]);
}
}
void reset() {
lcd.clear();
counter_value = 0;
updateCounter();
}
void incrementCounter() {
counter_value++;
updateCounter();
}
void updateCounter() {
digitalWrite(Latch_Pin, LOW);
if (counter_value < 10) { // Если однозначное число
shiftOut(Data_Pin, Clock_Pin, MSBFIRST, digits[counter_value]);
shiftOut(Data_Pin, Clock_Pin, MSBFIRST, digits[0]);
} else { // Если двузначное число
shiftOut(Data_Pin, Clock_Pin, MSBFIRST, digits[counter_value % 10]);
shiftOut(Data_Pin, Clock_Pin, MSBFIRST, digits[(counter_value / 10) % 10]);
}
digitalWrite(Latch_Pin, HIGH);
}
void handleString(String input) {
int startIndex = 0;
int endIndex = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == ' ' || i == input.length() - 1) {
endIndex = (i == input.length() - 1) ? i + 1 : i;
substrings[substringsCount++] = input.substring(startIndex, endIndex);
startIndex = i + 1;
if (substringsCount == 3) {
substringsCount = 0;
break;
}
}
}
}