8浏览
查看: 8|回复: 2

[项目] 【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计

[复制链接]
Arduinoecia网页上展示了带有128x64 LCD图形显示屏的Arduino时钟、用于调节显示对比度的电位器、DS3231 RTC模块和按钮。RTC 模块连接到 A4 (SDA) 和 A5 (SCL) 引脚上的 Arduino。该按钮连接到数字引脚 3,Arduino 的内部上拉电阻通过软件启用。该程序使用一些您必须在编译前安装的库。解压缩库并将它们放在 Arduino IDE 的 LIBRARIES 文件夹中。
程序有两个屏幕:第一个屏幕(screen_1),显示模拟时钟、数字时钟以及当前日期和温度。
第二个屏幕(screen_2 )显示数字时钟、星期几、日期和当前温度信息、最低温度和最高温度。通过按下连接到数字引脚 3 的按钮来选择屏幕。

要设置RTC DS3231模块的日期和时间,请使用串行监视器并输入年、月、日、时、分、秒格式的信息。
如果您输入的信息正确,将显示消息“ ”。

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图1

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图2

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图3

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图4

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图5


【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图6

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图7

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图8

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图9

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图10

驴友花雕  中级技神
 楼主|

发表于 昨天 17:45

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计

项目代码

  1. //Programa: Display LCD 128x64 e RTC DS3231
  2. //Autor: Arduino e Cia
  3. #include <U8glib.h>
  4. #include <DS3232RTC.h>
  5. #include <Streaming.h>
  6. #include <Time.h>
  7. #include <Wire.h>
  8. //A linha abaixo define as conexoes do display e deve ser
  9. //ajustada conforme o modelo utilizado
  10. U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 , 7); //Enable, RW, RS, RESET
  11. int X2 = 0;
  12. int Y2 = 0;
  13. int X3 = 0;
  14. int Y3 = 0;
  15. float angulo = 0;
  16. int posicao = 0;
  17. int posicaoh = 0;
  18. int temperatura =0;
  19. int min_temp = 500;
  20. int max_temp = -500;
  21. int ScreenWith = 128;
  22. int ScreenWithC = 96;
  23. int ScreenHeight = 64;
  24. int ScreenHeightC = 32;
  25. #define botao 3
  26. int estado_botao = 0;
  27. char* dia_da_semana[]={
  28.   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  29. void tela_1()
  30. {
  31.   u8g.drawRFrame(0, 0, 128, 64, 3);
  32.   //Mostra temperatura
  33.   u8g.setPrintPos(20, 30);
  34.   u8g.print(temperatura);
  35.   u8g.drawStr( 42, 30, "C");
  36.   u8g.drawCircle(37, 34, 2);
  37.   mostra_relogio_digital();
  38.   //Mostra relogio analogico
  39.   fundo_relogio();
  40.   //Atualiza Ponteiro de horas
  41.   desenha_ponteiro(hour()-1, 12.0, 10);
  42.   //Atualiza Ponteiro de minutos
  43.   desenha_ponteiro(minute()-5, 60.0, 19);
  44.   //Atualiza Ponteiro de segundos
  45.   desenha_ponteiro(second()-5, 60, 21);
  46. }
  47. void tela_2()
  48. {
  49.   u8g.drawRFrame(0, 0, 128, 64, 3);
  50.   u8g.drawRFrame(68, 4, 55, 56, 2);
  51.   mostra_relogio_digital();
  52.   u8g.setFont(u8g_font_5x8);
  53.   u8g.drawStr( 78, 35, "MIN");
  54.   u8g.drawStr( 78, 53, "MAX");
  55.   u8g.setFont(u8g_font_6x13);
  56.   u8g.setPrintPos(25, 41);
  57.   u8g.print(dia_da_semana[(weekday()-1)]);
  58.   //Mostra temperatura atual
  59.   u8g.setPrintPos(83, 19);
  60.   u8g.print(temperatura);
  61.   u8g.drawStr( 105, 19, "C");
  62.   u8g.drawCircle(100, 12, 2);
  63.   //Mostra temperatura minima
  64.   u8g.setPrintPos(98, 36);
  65.   u8g.print(min_temp);
  66.   u8g.drawCircle(113, 29, 2);
  67.   //Mostra temperatura maxima
  68.   u8g.setPrintPos(98, 54);
  69.   u8g.print(max_temp);
  70.   u8g.drawCircle(113, 47, 2);
  71. }
  72. void desenha_ponteiro(float valor, float rotacao, int Radius)
  73. {
  74.   angulo = valor * 2.0 * 3.1415 / rotacao - 1,5707;
  75.   X2 = ScreenWithC + Radius * cos(angulo);
  76.   Y2 = ScreenHeightC + Radius * sin(angulo);
  77.   u8g.drawLine(ScreenWithC, ScreenHeightC, X2, Y2);
  78. }
  79. void fundo_relogio()
  80. {
  81.   u8g.drawCircle(ScreenWithC, ScreenHeightC, 27);
  82.   u8g.drawCircle(ScreenWithC, ScreenHeightC, 1);
  83.   u8g.setFont(u8g_font_6x13);
  84.   u8g.setFontPosTop();
  85.   u8g.drawStr(90, 9, "12");
  86.   u8g.drawStr(114, 25, "3");
  87.   u8g.drawStr(94, 44, "6");
  88.   u8g.drawStr(74, 25, "9");
  89.   for(int traco_minuto = 0; traco_minuto<12; traco_minuto++)
  90.   {
  91.     //Desenha linhas relogio analogico
  92.     angulo = traco_minuto / 12.0 * 2 * 3.1415;
  93.     X2 = ScreenWithC + 25 * cos(angulo);
  94.     Y2 = ScreenHeightC + 25 * sin(angulo);
  95.     X3 = ScreenWithC + 25 * cos(angulo);
  96.     Y3 = ScreenHeightC + 25 * sin(angulo);
  97.     u8g.drawLine(X2, Y2, X3, Y3);
  98.   }
  99. }
  100. void mostra_relogio_digital()
  101. {
  102.   //Mostra a data
  103.   u8g.setFont(u8g_font_5x8);
  104.   u8g.setPrintPos(8, 55);
  105.   u8g.print(day());
  106.   u8g.drawStr( 19, 55, "/");
  107.   u8g.setPrintPos(24, 55);
  108.   u8g.print(month());
  109.   u8g.drawStr( 35, 55, "/");
  110.   u8g.setPrintPos(41, 55);
  111.   u8g.print(year());
  112.   //Mostra hora e minutos
  113.   u8g.drawRBox(3, 4, 62, 21,2);
  114.   u8g.setColorIndex(0);
  115.   u8g.setFont(u8g_font_fub17);
  116.   u8g.drawStr(29,21,":");
  117.   //Acerta a posicao do digito caso a hora
  118.   //seja menor do que 10
  119.   if (hour() < 10)
  120.   {
  121.     u8g.drawStr(3,23,"0");
  122.     posicaoh = 16;
  123.   }
  124.   else posicaoh = 3;
  125.   u8g.setPrintPos(posicaoh, 23);
  126.   u8g.print(hour());
  127.   //Acerta a posicao do digito caso o minuto
  128.   //seja menor do que 10
  129.   if (minute() < 10)
  130.   {
  131.     u8g.drawStr(38,23,"0");
  132.     posicao = 51;
  133.   }
  134.   else posicao = 38;
  135.   u8g.setPrintPos(posicao ,23);
  136.   u8g.print(minute());
  137.   u8g.setColorIndex(1);
  138. }
  139. void setup()
  140. {
  141.   pinMode(3, INPUT_PULLUP);
  142.   Serial.begin(9600);
  143.   if ( u8g.getMode() == U8G_MODE_R3G3B2 )
  144.     u8g.setColorIndex(255);     // white
  145.   else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
  146.     u8g.setColorIndex(3);         // max intensity
  147.   else if ( u8g.getMode() == U8G_MODE_BW )
  148.     u8g.setColorIndex(1);         // pixel on
  149.   setSyncProvider(RTC.get);
  150.   Serial << F("RTC Sync");
  151.   if (timeStatus() != timeSet) Serial << F(" FAIL!");
  152.   Serial << endl;
  153. }
  154. void loop()
  155. {
  156.   temperatura = RTC.temperature() / 4.;
  157.   if (temperatura >= max_temp)
  158.   {
  159.     max_temp = temperatura;
  160.   }
  161.   if (temperatura <= min_temp)
  162.   {
  163.     min_temp = temperatura;
  164.   }
  165.   static time_t tLast;
  166.   time_t t;
  167.   tmElements_t tm;
  168.   //Verifica se foi setado um novo horario
  169.   //Formato: ano, mes, dia, hora, minuto, segundo
  170.   if (Serial.available() >= 12) {
  171.     int y = Serial.parseInt();
  172.     if (y >= 100 && y < 1000)
  173.       Serial<<F("Erro: Ano deve ter dois ou quatro digitos!") <<endl;
  174.     else {
  175.       if (y >= 1000)
  176.         tm.Year = CalendarYrToTm(y);
  177.       else    //(y < 100)
  178.       tm.Year = y2kYearToTm(y);
  179.       tm.Month = Serial.parseInt();
  180.       tm.Day = Serial.parseInt();
  181.       tm.Hour = Serial.parseInt();
  182.       tm.Minute = Serial.parseInt();
  183.       tm.Second = Serial.parseInt();
  184.       t = makeTime(tm);
  185.       RTC.set(t);
  186.       setTime(t);
  187.       Serial << F("Horario modificado para: ");
  188.       printDateTime(t);
  189.       Serial << endl;
  190.       while (Serial.available() > 0) Serial.read();
  191.     }
  192.   }
  193.   t = now();
  194.   if (t != tLast) {
  195.     tLast = t;
  196.     printDateTime(t);
  197.     Serial << endl;
  198.   }
  199.   //Verifica se o botao foi pressionado
  200.   boolean valor_botao = digitalRead(3);
  201.   if (valor_botao != 1)
  202.   {
  203.     while(digitalRead(3) != 1)
  204.     {
  205.       delay(100);
  206.     }
  207.     // Inverte o estado
  208.     estado_botao = !estado_botao;
  209.   }
  210.   //picture loop
  211.   u8g.firstPage();
  212.   do {
  213.     if (estado_botao == 0)
  214.     {
  215.       tela_1();
  216.     }
  217.     if (estado_botao == 1)
  218.     {
  219.       tela_2();
  220.     }
  221.   }
  222.   while( u8g.nextPage() );
  223.   delay(10);
  224. }
  225. //Mostra data e hora na serial
  226. void printDateTime(time_t t)
  227. {
  228.   printI00(day(t), 0);
  229.   Serial << monthShortStr(month(t)) << _DEC(year(t));
  230.   Serial << ' ';
  231.   printI00(hour(t), ':');
  232.   printI00(minute(t), ':');
  233.   printI00(second(t), ' ');
  234. }
  235. //Correcao para imprimir "00" ao inves de "0" caso
  236. //o valor seja menor do que 10
  237. void printI00(int val, char delim)
  238. {
  239.   if (val < 10) Serial << '0';
  240.   Serial << _DEC(val);
  241.   if (delim > 0) Serial << delim;
  242.   return;
  243. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 昨天 17:47

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计
项目链接:https://www.hackster.io/mircemk/ ... n-128x64-lcd-09ed02
项目作者:北马其顿 米尔塞姆克(Mirko Pavleski)

项目视频 :https://www.youtube.com/watch?v=wLDrSwut6yI
项目代码:https://www.hackster.io/code_files/268691/download


【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图2

【Arduino 动手做】128x64 LCD 上的模拟、数字时钟和温度计图1

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail