2023/11/12

Arduino #39 使用 u8g2 庫在 SSD1306 OLED 上動態繪製骰子 (Draw dynamic dice on SSD1306 OLED with u8g2 library)

 Hi, welcome to this week's Weekend project with Stonez56.

This week is Arduino Episode #39, "Drawing Dice Dynamically at 0.96" OLED with u8g2" This video is suitable for people who want to learn how to draw patterns on SD 1336 LED.


=== IMPORTANT NOTICE === Do you want to generate u8g2 UTF8 font for your own Arduino projects? Try this online web tool I created @ https://stonez56.com/u8g2 Just 3 easy steps to get UTF8 Arduino codes for your projects! Try it today!! === NOTICE ===

In the previous episode, I showed audiences how to use the accelerometer to draw dynamic dice in APP inventor 2. People who are interested can click this link to watch. (App Inventor Episode 11: https: //   • App Inventor #11 Write your own App (...   ) The next video is planned to transfer the dice points from App Inventor 2 to the OLED on ESP32 for display in real time. In this way, we can learn how to communicate from mobile App to ESP32. Stay tuned! References: * u8g2 font reference: https://github.com/olikraus/u8g2/wiki... * u8g2 Github: https://github.com/olikraus/u8g2
* u8g2 UTF8 font generator: https://stonez56.com/u8g2/ If you don’t have enough time, you can also choose the part you want to watch from the timeline below! Video timeline: 00:00 start 00:40 Results presentation (Demo) 01:15 Coding briefing 02:13 Use random in code 04:23 Show dice at random location 08:53 drawDice function 14:59 Show total points 16:07 Select u8g2 fonts

==================== 中文版 =================================

Hi,歡迎來到本週的 Weekend project with Stonez56。本週是 Arduino 第 39 集,“在 0.96" OLED 上使用 u8g2 動態繪製骰子”。此影片適合希望學習如何在 SD 1336 LED 上繪製圖案的人。


描述: 嗨,歡迎來到本週的 Stonez56 週末項目。本週是 Arduino 第 39 集,“在 0.96" OLED 上使用 u8g2 動態繪製骰子”。此視頻適合希望學習如何在 SD 1336 LED 上繪製圖案的人。 在上一集中,我向觀眾展示了如何使用加速計在 APP inventor 2 中繪製動態骰子。感興趣的人可以點擊此鏈接觀看。 (App Inventor 第 11 集:https://www.youtube.com/watch?v=2d6Si6u8FVE) 下一個視頻計劃將骰子點數從 App Inventor 2 轉移到 ESP32 上的 OLED 以進行實時顯示。這樣,我們就可以學習如何從移動應用程序與 ESP32 通信。敬請期待! 參考文獻:
如果您沒有足夠的時間,您還可以在以下時間軸中選擇您想觀看的部分! 視頻時間軸: 00:00 開始 00:40 結果展示(演示) 01:15 編碼簡報 02:13 在代碼中使用隨機 04:23 在隨機位置顯示骰子 08:53 drawDice 函數 14:59 顯示總分 16:07 選擇 u8g2 字體>


====================== ESP32 Codes ===============================
  1. #include <Arduino.h>
  2. #include <U8g2lib.h>
  3.  
  4. U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
  5.  
  6. void setup(void)
  7. {
  8. Serial.begin(115200);
  9. u8g2.begin();
  10. randomSeed(analogRead(5)); // randomSeed() must be in setup()!
  11. }
  12.  
  13.  
  14. // OLED TEXT ROW number, vertical position
  15. const byte ROW[5] = {0, 15, 31, 47, 63};
  16.  
  17. // 4 dice top-left {x, y} locations in tl[]
  18. const int tl[4][2] = {{2, 20}, {34, 28}, {68, 20}, {100, 28}};
  19. const int size = 28; // dice width and height
  20. const int diceRoundedCorner = 5;
  21. /**
  22. * drawDiceImage
  23. * n = dice serial (1 ~ 4)
  24. * pnt = dice points (1 ~ 6)
  25. */
  26. void drawDiceImage(int n, int pnt)
  27. {
  28. int tx = tl[n][0]; // top left x
  29. int ty = random(17, 35); // Makes the y showing at random height
  30. // int ty = tl[n][1]; //top left y, makes Y showing at fixed height
  31. int centerX = tx + (size / 2); // center of the dice X
  32. int centerY = ty + (size / 2); // center of th dice Y
  33. const int largeDotSize = 4; // drawing dot size for 1 only
  34. const int smallDotSize = 2; // drawing dot size for rest
  35.  
  36. u8g2.drawRFrame(tx, ty, size, size, diceRoundedCorner); // draw the dice border
  37.  
  38. // draw the points based on the variable: pnt
  39. switch (pnt)
  40. {
  41. case 1:
  42. u8g2.drawFilledEllipse(centerX, centerY, largeDotSize, largeDotSize, U8G2_DRAW_ALL);
  43. break;
  44. case 2:
  45. u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  46. u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  47. break;
  48. case 3:
  49. u8g2.drawFilledEllipse(centerX, centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  50. u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  51. u8g2.drawFilledEllipse(centerX, centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  52. break;
  53. case 4:
  54. u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  55. u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  56. u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  57. u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  58. break;
  59. case 5:
  60. u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  61. u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  62. u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  63. u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  64. u8g2.drawFilledEllipse(centerX, centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  65. break;
  66. case 6:
  67. u8g2.drawFilledEllipse(centerX - (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  68. u8g2.drawFilledEllipse(centerX - (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  69. u8g2.drawFilledEllipse(centerX - (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  70. u8g2.drawFilledEllipse(centerX + (size / 4), centerY - (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  71. u8g2.drawFilledEllipse(centerX + (size / 4), centerY, smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  72. u8g2.drawFilledEllipse(centerX + (size / 4), centerY + (size / 4), smallDotSize, smallDotSize, U8G2_DRAW_ALL);
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78.  
  79. void loop(void)
  80. {
  81. int dicePoint[4];
  82. int totalPoints = 0;
  83. u8g2.clearBuffer();
  84.  
  85. //Generate 4 random dice points
  86. for (int i = 0; i < 4; i++)
  87. {
  88. dicePoint[i] = random(1, 6); // get random dice number to point
  89. drawDiceImage(i, dicePoint[i]); // draw dice i with the random dicePoint
  90. totalPoints += dicePoint[i]; // adding current points to totalPoints
  91. }
  92.  
  93. //Print out the title + total points
  94. u8g2.setCursor(0, ROW[1]);
  95. u8g2.setFont(u8g2_font_lubBI14_te); // font list: https://github.com/olikraus/u8g2/wiki/fntlist16
  96. u8g2.print("POINTS:");
  97. u8g2.print(totalPoints);
  98.  
  99. u8g2.sendBuffer();
  100. delay(1500);
  101. }
==================================================================

2023/11/05

VS Code 初始化的時候出現 Cannot find Arduino tools. Use Arduino CLI bundled with this extension instead? 要如何處理?



"安裝 Arduino Extension for VS code 時,如果遇到 “Cannot find Arduino tools. Use Arduino CLI bundled with this extension instead?”錯誤訊息。




請嘗試以下方法解決此問題。

解決步驟:


    1. Arduino: command path -> 保持為空白
    2. Arduino: path -> 保持為空白
    3. Arduino: Use Arduino Cli -> 勾選“Use Arduino CLI installed instated of the legacy Arduino IDE”
    4. 重新啟動 VS code

============ ENGLISH =====================

When install Arduino Extension for VS code, if you encountered "Cannot find Arduino tools. Use Arduino CLI bundled with this extension instead?" error message. Try this to fix it.


Steps to fix:


    1. Arduino: command path -> leave it empty
    2. Arduino: path -> leave it empty
    3. Arduino: Use Arduino Cli -> tick "Use Arduino CLI installed instated of the legacy Arduino IDE"
    4. Restart VS code