CCCPaste

Stepper Control rev_03

/********* 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: Stepper Control
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-04-28 12:46:38

********* 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 */
/****** SYSTEM REQUIREMENT 2 *****/
	/* keep distance distance 100mm from sensor  to */
	/* object in real time 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 
#include 
#include 
#include 
#include 

/****** DEFINITION OF CONSTANTS *****/
const uint8_t button_PushButton_PIN_D2 = 2;
const uint8_t sensor_I2C_ADDRESS = 0x29;
const uint8_t motor_STEP_PIN = 3;
const uint8_t motor_DIR_PIN = 4;
const uint8_t OLED_SDA_PIN = A4;
const uint8_t OLED_SCL_PIN = A5;
const uint16_t set_distance = 100; // Set distance in mm
const float tolerance = 0.05; // Tolerance of 5%

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Instance of the button.
EasyButton button(button_PushButton_PIN_D2);

// Instance of the VL53L0X sensor.
VL53L0X sensor;

// Instance of the stepper motor driver.
AccelStepper stepper(AccelStepper::DRIVER, motor_STEP_PIN, motor_DIR_PIN);

// Instance of the OLED LCD display.
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, OLED_SCL_PIN, OLED_SDA_PIN);

void setup(void)
{
    // Initialize serial communication
    Serial.begin(9600);

    // Initialize the button
    pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);

    // Initialize the VL53L0X sensor
    Wire.begin();
    sensor.init();
    sensor.setAddress(sensor_I2C_ADDRESS);
    sensor.setTimeout(500);

    // Initialize the stepper motor driver
    stepper.setMaxSpeed(1000);
    stepper.setSpeed(200);

    // Initialize the OLED LCD display
    u8g2.begin();
    u8g2.setFont(u8g2_font_ncenB14_tr);
}

void loop(void)
{
    // Read the button state
    button.read();

    // Main code here
    // Keep distance set at 100mm with tolerance 5%
    // Display distance on OLED LCD in real-time

    // Example code for displaying distance on OLED LCD
    u8g2.firstPage();
    do {
        u8g2.setCursor(0, 20);
        u8g2.print("Distance: ");
        u8g2.print(sensor.readRangeSingleMillimeters());
        u8g2.print(" mm");
    } while (u8g2.nextPage());
}