// Rotation Text //
// Made by ji_wany //
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <string.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned int GetLCM(unsigned int num1, unsigned int num2) { // 숫자 2개의 최소공배수 구하기 (유클리드 호제법 참고)
unsigned int m_num = num1 * num2;
if(num1 < num2) {
unsigned int temp = num1;
num1 = num2;
num2 = temp;
}
unsigned int r_num;
while(true) {
r_num = num1 % num2;
if(r_num == 0) {
return(m_num/num2);
}
else {
num1 = num2;
num2 = r_num;
}
}
}
void RotationText(char text1[], char text2[], unsigned short d_time) { // RotationText("첫째 줄 텍스트 ", "둘째 줄 텍스트", 전체 순환 소요시간(단위 ms));
// 사용 변수 선언
const unsigned int w = 16; // width, 출력 너비
int fir_cha; // first_character, 처음 오는 문자 순번
int fol_cha; // follow_character, 이어 오는 문자 순번
int pri_num; // print_number, 배열의 출력할 순번
char pri_cha; // print_character, 출력할 문자
unsigned int len_text1 = strlen(text1); // length_text1, 1열 글자 길이
unsigned int len_text2 = strlen(text2); // length_text2, 2열 글자 길이
unsigned int t_len_text = GetLCM(len_text1,len_text2); // total_length_text, 총 글자 길이
// 글자 프린트
for(fir_cha = 0; fir_cha < t_len_text; fir_cha++) { // 모든 단어조합이 1개씩 나오도록 for문 실행
lcd.clear(); // lcd 초기화
// 1열 출력 시작 ( ~ 50 )
lcd.setCursor(0, 0); // 1열 출력 준비
for (fol_cha = 0; fol_cha <= w; fol_cha++) {
pri_num = ((fir_cha % len_text1) + fol_cha) % len_text1; // (시작 번 % 해당 텍스트 길이) + 후속 번 % 총 길이
pri_cha = text1[pri_num]; // 출력할 번호번째 문자 불러오기
lcd.print(pri_cha); // 해당 문자만 출력
}
// 2열 출력 시작 ( ~ 57 )
lcd.setCursor(0, 1); // 2열 출력 준비
for (fol_cha = 0; fol_cha <= w; fol_cha++) {
pri_num = ((fir_cha % len_text2) + fol_cha) % len_text2; // (시작 번 % 해당 텍스트 길이) + 후속 번 % 총 길이
pri_cha = text2[pri_num]; // 출력할 번호번째 문자 불러오기
lcd.print(pri_cha); // 해당 문자만 출력
}
delay(d_time); // 대기
}
}
void setup() {
lcd.init();
lcd.backlight();
}
void loop() {
// 사용 예시 ( 1열에 Rotation Text, 2열에 Made by ji_wany 를 100ms 간격으로 / 총 소요시간(11200ms, 11.2초) = 1열의 길이(14)와 2열의 길이(16)의 최소공배수(112) * 간격 시간(100)) :
RotationText("Rotation Text ","Made by ji_wany ",100);
delay(1000);
RotationText("Hello ","Arduino ",100);
delay(1000);
}
// 이게 왜 됨?