Advertisement
pleasedontcode

Spinning Arrow rev_01

Oct 11th, 2024
105
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: Spinning Arrow
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-10-12 00:32:16
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Draw an animation like a spinning arrow */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <Wire.h>
  25. #include <Adafruit_SSD1306.h>    // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  26. #include <U8g2_for_Adafruit_GFX.h> // https://github.com/olikraus/U8g2_for_Adafruit_GFX
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. void drawArrow(int16_t x, int16_t y, int16_t angle); // Function to draw the arrow
  32.  
  33. /***** DEFINITION OF I2C PINS *****/
  34. const uint8_t ga_SSD1306OledDisplay_I2C_PIN_SDA_A4      = A4;
  35. const uint8_t ga_SSD1306OledDisplay_I2C_PIN_SCL_A5      = A5;
  36. const uint8_t ga_SSD1306OledDisplay_I2C_SLAVE_ADDRESS    = 60;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. // Create an instance of the display with the reset pin
  40. Adafruit_SSD1306 display(ga_SSD1306OledDisplay_I2C_PIN_SDA_A4, ga_SSD1306OledDisplay_I2C_PIN_SCL_A5, -1, -1, ga_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  41. // Initialize U8g2_for_Adafruit_GFX with the display instance
  42. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Instance for U8g2 support
  43.  
  44. void setup(void)
  45. {
  46.     // Initialize the display
  47.     display.begin(SSD1306_SWITCHCAPVCC, ga_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
  48.     u8g2_for_adafruit_gfx.begin(display); // Connect U8g2 to Adafruit GFX
  49.  
  50.     // Clear the display buffer
  51.     display.clearDisplay();
  52.     display.display(); // Make the buffer visible
  53. }
  54.  
  55. void loop(void)
  56. {
  57.     // Animation loop for spinning arrow
  58.     for (int angle = 0; angle < 360; angle += 10) { // Increment angle for animation
  59.         display.clearDisplay(); // Clear the display buffer
  60.         drawArrow(display.width() / 2, display.height() / 2, angle); // Draw the arrow at the center
  61.         display.display(); // Update the display
  62.         delay(100); // Delay for animation effect
  63.     }
  64. }
  65.  
  66. // Function to draw an arrow at a given angle
  67. void drawArrow(int16_t x, int16_t y, int16_t angle) {
  68.     // Calculate the arrow points based on the angle
  69.     int16_t arrowLength = 20; // Length of the arrow
  70.     int16_t arrowWidth = 5;   // Width of the arrow head
  71.  
  72.     // Calculate the end point of the arrow
  73.     int16_t endX = x + arrowLength * cos(radians(angle));
  74.     int16_t endY = y + arrowLength * sin(radians(angle));
  75.  
  76.     // Draw the arrow shaft
  77.     display.drawLine(x, y, endX, endY, WHITE); // Draw line from center to end point
  78.  
  79.     // Draw the arrow head
  80.     display.drawLine(endX, endY, endX - arrowWidth * cos(radians(angle - 45)), endY - arrowWidth * sin(radians(angle - 45)), WHITE); // Left head
  81.     display.drawLine(endX, endY, endX - arrowWidth * cos(radians(angle + 45)), endY - arrowWidth * sin(radians(angle + 45)), WHITE); // Right head
  82. }
  83.  
  84. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement