このプロジェクトでは、LCDスクリーンに時刻と日付を表示する方法を見ていきます。このプロジェクトは、目覚まし時計やただの時計を作るのにとても便利です。Arduinoボードに電池を追加し、3Dプリンターで箱を印刷するだけで、独立した本物の時計が完成します。 難易度: 必要な材料 それでは、このプロジェクトに必要なハードウェアを見ていこう:Arduino Unoボード16×2の液晶画面ポテンショメーター220Ωの抵抗接続ワイヤー(約15本) Arduinoクロック図 ポテンショメーターの目的は何ですか? ポテンショメーターは画面の明るさを調整するためのもの。ポテンショメーターからスクリーンへのGNDはArduinoボードに直接接続できる。 プログラム #include <LiquidCrystal.h> // Library for LCD Screen int time_date[6] = {10, 34, 18, 18, 10, 2022}; //second minute hour day month year int limits[6] = {60, 60, 24, 31, 12, 3000}; // increment limits LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Initialize the library with the connected pins void setup() { lcd.begin(16, 2); // Initialize the LCD with its dimensions } void loop() { time_date[0]++; // time increment for (int i = 0; i < 6 ; i++){ if (time_date[i] == limits[i]) { // check limit overflow if (i<5) {time_date[i+1]++;} time_date[i] = 0; } } lcd.setCursor(0,0); // Position cursor on first line for (int j = 3; j < 5; j++) { // display date if (time_date[j]<10){ lcd.print("0"); lcd.print(time_date[j]); } else {lcd.print(time_date[j]);} lcd.print("/"); } lcd.print(time_date[5]); lcd.setCursor(0,1); // Position cursor on second line for (int j = 2; j >= 0; j--) { // display time if (time_date[j]<10) { lcd.print("0"); lcd.print(time_date[j]); }else {lcd.print(time_date[j]);} if (j != 0){ lcd.print(":"); } } delay(250); } プログラムの日付と時刻を変更する! 画像からお分かりのように、開始日時は固定されており、必ずしも正しい日とは限りません。そのため、この日付はプログラム内で自分で変更することができます。ここに変更する行があります: int time_date[6] = {10, 34, 18, 18, 10, 2022}; //second, minute, hour, day, month, year Arduinoの電源が切れても現在時刻を維持するには? ご覧のように、Arduinoボードの電源を切ると、時計と日付は更新されなくなる。これは、例えば時計を作っている場合などには煩わしいかもしれません。この問題を解決するには、rtcクロックモジュール(DS3231など)をプロジェクトに追加すれば、Arduinoボードの電源を切っても、Arduinoボードの電源とは独立したバッテリーのおかげで、時刻を計算してくれます。 シミュレーション