Advertisement
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: Library Applications
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-04-28 12:34:22
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* arduino uno based code that keep distance set */
- /* distance in 100mm with tolerance 5% to objects use */
- /* for that VL53L0X sensor and stepper motor driver */
- /* with max treveling distance 50mm from set */
- /* distance, distance displaying on OLED LCD 0.96 */
- /* i2c 4 pin */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <VL53L0X.h>
- #include <AccelStepper.h>
- #include <U8g2lib.h>
- #include <EasyButton.h>
- /****** DEFINITION OF CONSTANTS *****/
- const uint8_t button_PushButton_PIN_D2 = 2; // Arduino pin where the button is connected to
- const uint16_t distanceSet = 100; // Distance set in 100mm
- const float tolerance = 0.05; // Tolerance of 5%
- const uint8_t maxTravelDistance = 50; // Maximum traveling distance from the set distance
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- EasyButton button(button_PushButton_PIN_D2); // Initialize EasyButton object with the button pin
- VL53L0X sensor;
- AccelStepper stepper(AccelStepper::DRIVER, 9, 8); // Example pins for stepper motor driver
- U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
- // Initialize the EasyButton library
- button.begin();
- // Initialize the sensor
- sensor.init();
- sensor.setTimeout(500);
- // Initialize the stepper motor
- stepper.setMaxSpeed(200);
- stepper.setAcceleration(100);
- // Initialize the OLED display
- u8g2.begin();
- u8g2.clearBuffer();
- u8g2.setFont(u8g2_font_ncenB14_tr);
- u8g2.setCursor(0, 20);
- u8g2.print("Distance: ");
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- button.read(); // Continuously read the status of the button
- // Read distance from sensor
- uint16_t distance = sensor.readRangeSingleMillimeters();
- // Calculate the difference from the set distance
- float difference = abs(distanceSet - distance);
- // Check if the difference is within tolerance
- if (difference <= distanceSet * tolerance) {
- // Display distance on OLED LCD
- u8g2.clearBuffer();
- u8g2.setCursor(0, 20);
- u8g2.print("Distance: ");
- u8g2.print(distance);
- u8g2.sendBuffer();
- // Move stepper motor based on distance difference
- int steps = map(difference, 0, distanceSet * tolerance, 0, maxTravelDistance);
- stepper.moveTo(steps);
- stepper.run();
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement