Advertisement
pleasedontcode

Stepper Control rev_03

Apr 28th, 2024
122
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: Stepper Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-04-28 12:46:38
  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. /****** SYSTEM REQUIREMENT 2 *****/
  27.     /* keep distance distance 100mm from sensor  to */
  28.     /* object in real time with tolerance 5% to objects */
  29.     /* use for that VL53L0X sensor and stepper motor */
  30.     /* driver with max treveling distance 50mm from set */
  31.     /* distance, distance  displaying on OLED LCD 0.96 */
  32.     /* i2c 4 pin */
  33. /****** END SYSTEM REQUIREMENTS *****/
  34.  
  35. /****** DEFINITION OF LIBRARIES *****/
  36. #include <Wire.h>
  37. #include <VL53L0X.h>
  38. #include <AccelStepper.h>
  39. #include <U8g2lib.h>
  40. #include <EasyButton.h>
  41.  
  42. /****** DEFINITION OF CONSTANTS *****/
  43. const uint8_t button_PushButton_PIN_D2 = 2;
  44. const uint8_t sensor_I2C_ADDRESS = 0x29;
  45. const uint8_t motor_STEP_PIN = 3;
  46. const uint8_t motor_DIR_PIN = 4;
  47. const uint8_t OLED_SDA_PIN = A4;
  48. const uint8_t OLED_SCL_PIN = A5;
  49. const uint16_t set_distance = 100; // Set distance in mm
  50. const float tolerance = 0.05; // Tolerance of 5%
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. // Instance of the button.
  54. EasyButton button(button_PushButton_PIN_D2);
  55.  
  56. // Instance of the VL53L0X sensor.
  57. VL53L0X sensor;
  58.  
  59. // Instance of the stepper motor driver.
  60. AccelStepper stepper(AccelStepper::DRIVER, motor_STEP_PIN, motor_DIR_PIN);
  61.  
  62. // Instance of the OLED LCD display.
  63. U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, OLED_SCL_PIN, OLED_SDA_PIN);
  64.  
  65. void setup(void)
  66. {
  67.     // Initialize serial communication
  68.     Serial.begin(9600);
  69.  
  70.     // Initialize the button
  71.     pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  72.  
  73.     // Initialize the VL53L0X sensor
  74.     Wire.begin();
  75.     sensor.init();
  76.     sensor.setAddress(sensor_I2C_ADDRESS);
  77.     sensor.setTimeout(500);
  78.  
  79.     // Initialize the stepper motor driver
  80.     stepper.setMaxSpeed(1000);
  81.     stepper.setSpeed(200);
  82.  
  83.     // Initialize the OLED LCD display
  84.     u8g2.begin();
  85.     u8g2.setFont(u8g2_font_ncenB14_tr);
  86. }
  87.  
  88. void loop(void)
  89. {
  90.     // Read the button state
  91.     button.read();
  92.  
  93.     // Main code here
  94.     // Keep distance set at 100mm with tolerance 5%
  95.     // Display distance on OLED LCD in real-time
  96.  
  97.     // Example code for displaying distance on OLED LCD
  98.     u8g2.firstPage();
  99.     do {
  100.         u8g2.setCursor(0, 20);
  101.         u8g2.print("Distance: ");
  102.         u8g2.print(sensor.readRangeSingleMillimeters());
  103.         u8g2.print(" mm");
  104.     } while (u8g2.nextPage());
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement