Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: # Walking Figure
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-09 03:42:24
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* I have a I2C Oled Display and I want to code it to */
- /* show The animation of walking in wokwi */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_SSD1306.h> //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
- #include <U8g2_for_Adafruit_GFX.h> //https://github.com/olikraus/U8g2_for_Adafruit_GFX
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void initializeDisplay(void);
- void drawWalkingAnimation(void);
- void drawWalkingFigureFrame1(int16_t xPos);
- void drawWalkingFigureFrame2(int16_t xPos);
- void drawWalkingFigureFrame3(int16_t xPos);
- void drawWalkingFigureFrame4(int16_t xPos);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t oledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
- const uint8_t oledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
- const uint8_t oledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
- /****** DEFINITION OF DISPLAY PARAMETERS *****/
- const uint16_t SCREEN_WIDTH = 128;
- const uint16_t SCREEN_HEIGHT = 64;
- const uint8_t OLED_RESET_PIN = -1;
- const uint16_t I2C_ADDRESS = 0x3C;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET_PIN);
- U8G2_FOR_ADAFRUIT_GFX u8g2Display;
- /****** ANIMATION PARAMETERS *****/
- const uint16_t ANIMATION_DELAY_MS = 500;
- const uint16_t FIGURE_X_START = 0;
- const uint16_t FIGURE_X_SPEED = 5;
- int16_t figureCurrentXPos = FIGURE_X_START;
- uint8_t animationFrameIndex = 0;
- void setup(void)
- {
- // Initialize Serial communication for debugging (optional)
- Serial.begin(115200);
- delay(100);
- // Initialize I2C communication with custom pins
- Wire.begin(oledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21, oledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
- delay(100);
- // Initialize the OLED display
- initializeDisplay();
- }
- void loop(void)
- {
- // Clear the display buffer
- display.clearDisplay();
- // Draw the walking animation
- drawWalkingAnimation();
- // Update the display with the buffer contents
- display.display();
- // Delay between animation frames
- delay(ANIMATION_DELAY_MS);
- }
- void initializeDisplay(void)
- {
- // Initialize display with I2C address
- if (!display.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS))
- {
- Serial.println(F("SSD1306 allocation failed"));
- while (1);
- }
- // Initialize U8g2 for Adafruit GFX to enable advanced font rendering
- u8g2Display.begin(display);
- // Clear the display
- display.clearDisplay();
- display.display();
- Serial.println(F("Display initialized successfully"));
- }
- void drawWalkingAnimation(void)
- {
- // Select the appropriate frame based on animation index
- switch (animationFrameIndex)
- {
- case 0:
- // Frame 1: Left leg forward
- drawWalkingFigureFrame1(figureCurrentXPos);
- break;
- case 1:
- // Frame 2: Both legs together
- drawWalkingFigureFrame2(figureCurrentXPos);
- break;
- case 2:
- // Frame 3: Right leg forward
- drawWalkingFigureFrame3(figureCurrentXPos);
- break;
- case 3:
- // Frame 4: Both legs together (stepping phase)
- drawWalkingFigureFrame4(figureCurrentXPos);
- break;
- default:
- animationFrameIndex = 0;
- break;
- }
- // Update animation frame index
- animationFrameIndex++;
- if (animationFrameIndex >= 4)
- {
- animationFrameIndex = 0;
- }
- // Update figure position for walking movement
- figureCurrentXPos += FIGURE_X_SPEED;
- // Reset position when figure reaches the right edge of the screen
- if (figureCurrentXPos > SCREEN_WIDTH)
- {
- figureCurrentXPos = FIGURE_X_START;
- }
- }
- void drawWalkingFigureFrame1(int16_t xPos)
- {
- // Frame 1: Left leg forward, right leg back
- // Head (circle)
- display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
- // Body (vertical line)
- display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
- // Left arm (forward)
- display.drawLine(xPos + 8, 28, xPos + 2, 25, SSD1306_WHITE);
- // Right arm (back)
- display.drawLine(xPos + 8, 28, xPos + 14, 25, SSD1306_WHITE);
- // Left leg (forward)
- display.drawLine(xPos + 8, 40, xPos + 4, 55, SSD1306_WHITE);
- // Right leg (back)
- display.drawLine(xPos + 8, 40, xPos + 12, 55, SSD1306_WHITE);
- }
- void drawWalkingFigureFrame2(int16_t xPos)
- {
- // Frame 2: Both legs in middle position (stepping transition)
- // Head (circle)
- display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
- // Body (vertical line)
- display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
- // Left arm (middle position)
- display.drawLine(xPos + 8, 28, xPos + 3, 28, SSD1306_WHITE);
- // Right arm (middle position)
- display.drawLine(xPos + 8, 28, xPos + 13, 28, SSD1306_WHITE);
- // Left leg (middle position)
- display.drawLine(xPos + 8, 40, xPos + 6, 55, SSD1306_WHITE);
- // Right leg (middle position)
- display.drawLine(xPos + 8, 40, xPos + 10, 55, SSD1306_WHITE);
- }
- void drawWalkingFigureFrame3(int16_t xPos)
- {
- // Frame 3: Right leg forward, left leg back
- // Head (circle)
- display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
- // Body (vertical line)
- display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
- // Left arm (back)
- display.drawLine(xPos + 8, 28, xPos + 14, 25, SSD1306_WHITE);
- // Right arm (forward)
- display.drawLine(xPos + 8, 28, xPos + 2, 25, SSD1306_WHITE);
- // Left leg (back)
- display.drawLine(xPos + 8, 40, xPos + 12, 55, SSD1306_WHITE);
- // Right leg (forward)
- display.drawLine(xPos + 8, 40, xPos + 4, 55, SSD1306_WHITE);
- }
- void drawWalkingFigureFrame4(int16_t xPos)
- {
- // Frame 4: Both legs in middle position (stepping transition)
- // Head (circle)
- display.fillCircle(xPos + 8, 20, 5, SSD1306_WHITE);
- // Body (vertical line)
- display.drawLine(xPos + 8, 25, xPos + 8, 40, SSD1306_WHITE);
- // Left arm (middle position)
- display.drawLine(xPos + 8, 28, xPos + 13, 28, SSD1306_WHITE);
- // Right arm (middle position)
- display.drawLine(xPos + 8, 28, xPos + 3, 28, SSD1306_WHITE);
- // Left leg (middle position)
- display.drawLine(xPos + 8, 40, xPos + 10, 55, SSD1306_WHITE);
- // Right leg (middle position)
- display.drawLine(xPos + 8, 40, xPos + 6, 55, SSD1306_WHITE);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment