Advertisement
pleasedontcode

Library Applications rev_01

Apr 28th, 2024
115
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: Library Applications
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-28 12:34:22
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* arduino uno based code that keep distance set */
  21.     /* distance in 100mm with tolerance 5% to objects use */
  22.     /* for that VL53L0X sensor and stepper motor driver */
  23.     /* with max treveling distance 50mm from set */
  24.     /* distance, distance  displaying on OLED LCD 0.96 */
  25.     /* i2c 4 pin */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <VL53L0X.h>
  31. #include <AccelStepper.h>
  32. #include <U8g2lib.h>
  33. #include <EasyButton.h>
  34.  
  35. /****** DEFINITION OF CONSTANTS *****/
  36. const uint8_t button_PushButton_PIN_D2 = 2; // Arduino pin where the button is connected to
  37. const uint16_t distanceSet = 100; // Distance set in 100mm
  38. const float tolerance = 0.05; // Tolerance of 5%
  39. const uint8_t maxTravelDistance = 50; // Maximum traveling distance from the set distance
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. EasyButton button(button_PushButton_PIN_D2); // Initialize EasyButton object with the button pin
  43. VL53L0X sensor;
  44. AccelStepper stepper(AccelStepper::DRIVER, 9, 8); // Example pins for stepper motor driver
  45. U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  46.  
  47. void setup(void)
  48. {
  49.     // put your setup code here, to run once:
  50.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  51.  
  52.     // Initialize the EasyButton library
  53.     button.begin();
  54.  
  55.     // Initialize the sensor
  56.     sensor.init();
  57.     sensor.setTimeout(500);
  58.  
  59.     // Initialize the stepper motor
  60.     stepper.setMaxSpeed(200);
  61.     stepper.setAcceleration(100);
  62.  
  63.     // Initialize the OLED display
  64.     u8g2.begin();
  65.     u8g2.clearBuffer();
  66.     u8g2.setFont(u8g2_font_ncenB14_tr);
  67.     u8g2.setCursor(0, 20);
  68.     u8g2.print("Distance: ");
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // put your main code here, to run repeatedly:
  74.     button.read(); // Continuously read the status of the button
  75.  
  76.     // Read distance from sensor
  77.     uint16_t distance = sensor.readRangeSingleMillimeters();
  78.  
  79.     // Calculate the difference from the set distance
  80.     float difference = abs(distanceSet - distance);
  81.  
  82.     // Check if the difference is within tolerance
  83.     if (difference <= distanceSet * tolerance) {
  84.         // Display distance on OLED LCD
  85.         u8g2.clearBuffer();
  86.         u8g2.setCursor(0, 20);
  87.         u8g2.print("Distance: ");
  88.         u8g2.print(distance);
  89.         u8g2.sendBuffer();
  90.  
  91.         // Move stepper motor based on distance difference
  92.         int steps = map(difference, 0, distanceSet * tolerance, 0, maxTravelDistance);
  93.         stepper.moveTo(steps);
  94.         stepper.run();
  95.     }
  96. }
  97.  
  98. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement