pleasedontcode

# Walking Figure rev_01

Jan 8th, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: # Walking Figure
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2026-01-09 03:42:24
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I have a I2C Oled Display and I want to code it to */
  21.     /* show The animation of walking in wokwi */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_SSD1306.h>   //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  30. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void initializeDisplay(void);
  36. void drawWalkingAnimation(void);
  37. void drawWalkingFigureFrame1(int16_t xPos);
  38. void drawWalkingFigureFrame2(int16_t xPos);
  39. void drawWalkingFigureFrame3(int16_t xPos);
  40. void drawWalkingFigureFrame4(int16_t xPos);
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. const uint8_t oledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21        = 21;
  44. const uint8_t oledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22        = 22;
  45. const uint8_t oledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS      = 60;
  46.  
  47. /****** DEFINITION OF DISPLAY PARAMETERS *****/
  48. const uint16_t SCREEN_WIDTH = 128;
  49. const uint16_t SCREEN_HEIGHT = 64;
  50. const uint8_t OLED_RESET_PIN = -1;
  51. const uint16_t I2C_ADDRESS = 0x3C;
  52.  
  53. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  54. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET_PIN);
  55. U8G2_FOR_ADAFRUIT_GFX u8g2Display;
  56.  
  57. /****** ANIMATION PARAMETERS *****/
  58. const uint16_t ANIMATION_DELAY_MS = 500;
  59. const uint16_t FIGURE_X_START = 0;
  60. const uint16_t FIGURE_X_SPEED = 5;
  61. int16_t figureCurrentXPos = FIGURE_X_START;
  62. uint8_t animationFrameIndex = 0;
  63.  
  64. void setup(void)
  65. {
  66.     // Initialize Serial communication for debugging (optional)
  67.     Serial.begin(115200);
  68.     delay(100);
  69.    
  70.     // Initialize I2C communication with custom pins
  71.     Wire.begin(oledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21, oledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
  72.     delay(100);
  73.    
  74.     // Initialize the OLED display
  75.     initializeDisplay();
  76. }
  77.  
  78. void loop(void)
  79. {
  80.     // Clear the display buffer
  81.     display.clearDisplay();
  82.    
  83.     // Draw the walking animation
  84.     drawWalkingAnimation();
  85.    
  86.     // Update the display with the buffer contents
  87.     display.display();
  88.    
  89.     // Delay between animation frames
  90.     delay(ANIMATION_DELAY_MS);
  91. }
  92.  
  93. void initializeDisplay(void)
  94. {
  95.     // Initialize display with I2C address
  96.     if (!display.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS))
  97.     {
  98.         Serial.println(F("SSD1306 allocation failed"));
  99.         while (1);
  100.     }
  101.    
  102.     // Initialize U8g2 for Adafruit GFX to enable advanced font rendering
  103.     u8g2Display.begin(display);
  104.    
  105.     // Clear the display
  106.     display.clearDisplay();
  107.     display.display();
  108.    
  109.     Serial.println(F("Display initialized successfully"));
  110. }
  111.  
  112. void drawWalkingAnimation(void)
  113. {
  114.     // Select the appropriate frame based on animation index
  115.     switch (animationFrameIndex)
  116.     {
  117.         case 0:
  118.             // Frame 1: Left leg forward
  119.             drawWalkingFigureFrame1(figureCurrentXPos);
  120.             break;
  121.            
  122.         case 1:
  123.             // Frame 2: Both legs together
  124.             drawWalkingFigureFrame2(figureCurrentXPos);
  125.             break;
  126.            
  127.         case 2:
  128.             // Frame 3: Right leg forward
  129.             drawWalkingFigureFrame3(figureCurrentXPos);
  130.             break;
  131.            
  132.         case 3:
  133.             // Frame 4: Both legs together (stepping phase)
  134.             drawWalkingFigureFrame4(figureCurrentXPos);
  135.             break;
  136.            
  137.         default:
  138.             animationFrameIndex = 0;
  139.             break;
  140.     }
  141.    
  142.     // Update animation frame index
  143.     animationFrameIndex++;
  144.     if (animationFrameIndex >= 4)
  145.     {
  146.         animationFrameIndex = 0;
  147.     }
  148.    
  149.     // Update figure position for walking movement
  150.     figureCurrentXPos += FIGURE_X_SPEED;
  151.    
  152.     // Reset position when figure reaches the right edge of the screen
  153.     if (figureCurrentXPos > SCREEN_WIDTH)
  154.     {
  155.         figureCurrentXPos = FIGURE_X_START;
  156.     }
  157. }
  158.  
  159. void drawWalkingFigureFrame1(int16_t xPos)
  160. {
  161.     // Frame 1: Left leg forward, right leg back
  162.     // Head (circle)
  163.     display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
  164.    
  165.     // Body (vertical line)
  166.     display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
  167.    
  168.     // Left arm (forward)
  169.     display.drawLine(xPos + 8, 28, xPos + 2, 25, SSD1306_WHITE);
  170.    
  171.     // Right arm (back)
  172.     display.drawLine(xPos + 8, 28, xPos + 14, 25, SSD1306_WHITE);
  173.    
  174.     // Left leg (forward)
  175.     display.drawLine(xPos + 8, 40, xPos + 4, 55, SSD1306_WHITE);
  176.    
  177.     // Right leg (back)
  178.     display.drawLine(xPos + 8, 40, xPos + 12, 55, SSD1306_WHITE);
  179. }
  180.  
  181. void drawWalkingFigureFrame2(int16_t xPos)
  182. {
  183.     // Frame 2: Both legs in middle position (stepping transition)
  184.     // Head (circle)
  185.     display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
  186.    
  187.     // Body (vertical line)
  188.     display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
  189.    
  190.     // Left arm (middle position)
  191.     display.drawLine(xPos + 8, 28, xPos + 3, 28, SSD1306_WHITE);
  192.    
  193.     // Right arm (middle position)
  194.     display.drawLine(xPos + 8, 28, xPos + 13, 28, SSD1306_WHITE);
  195.    
  196.     // Left leg (middle position)
  197.     display.drawLine(xPos + 8, 40, xPos + 6, 55, SSD1306_WHITE);
  198.    
  199.     // Right leg (middle position)
  200.     display.drawLine(xPos + 8, 40, xPos + 10, 55, SSD1306_WHITE);
  201. }
  202.  
  203. void drawWalkingFigureFrame3(int16_t xPos)
  204. {
  205.     // Frame 3: Right leg forward, left leg back
  206.     // Head (circle)
  207.     display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
  208.    
  209.     // Body (vertical line)
  210.     display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
  211.    
  212.     // Left arm (back)
  213.     display.drawLine(xPos + 8, 28, xPos + 14, 25, SSD1306_WHITE);
  214.    
  215.     // Right arm (forward)
  216.     display.drawLine(xPos + 8, 28, xPos + 2, 25, SSD1306_WHITE);
  217.    
  218.     // Left leg (back)
  219.     display.drawLine(xPos + 8, 40, xPos + 12, 55, SSD1306_WHITE);
  220.    
  221.     // Right leg (forward)
  222.     display.drawLine(xPos + 8, 40, xPos + 4, 55, SSD1306_WHITE);
  223. }
  224.  
  225. void drawWalkingFigureFrame4(int16_t xPos)
  226. {
  227.     // Frame 4: Both legs in middle position (stepping transition)
  228.     // Head (circle)
  229.     display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
  230.    
  231.     // Body (vertical line)
  232.     display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
  233.    
  234.     // Left arm (middle position)
  235.     display.drawLine(xPos + 8, 28, xPos + 13, 28, SSD1306_WHITE);
  236.    
  237.     // Right arm (middle position)
  238.     display.drawLine(xPos + 8, 28, xPos + 3, 28, SSD1306_WHITE);
  239.    
  240.     // Left leg (middle position)
  241.     display.drawLine(xPos + 8, 40, xPos + 10, 55, SSD1306_WHITE);
  242.    
  243.     // Right leg (middle position)
  244.     display.drawLine(xPos + 8, 40, xPos + 6, 55, SSD1306_WHITE);
  245. }
  246.  
  247. /* END CODE */
  248.  
Advertisement
Add Comment
Please, Sign In to add comment