0% found this document useful (0 votes)
19 views4 pages

Completed For Upload Onto Lathe Module

Two buttons have been added to pins 5 and 7 for holding to increment or decrement the number of motor steps. The document describes an Arduino code for controlling two stepper motors and displaying motor position and status on an OLED display. The code uses encoder inputs and buttons to control motor position and switching between motors.

Uploaded by

Kurdlost Suly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Completed For Upload Onto Lathe Module

Two buttons have been added to pins 5 and 7 for holding to increment or decrement the number of motor steps. The document describes an Arduino code for controlling two stepper motors and displaying motor position and status on an OLED display. The code uses encoder inputs and buttons to control motor position and switching between motors.

Uploaded by

Kurdlost Suly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Despite all the functions, two buttons on pin 5 & 7 has been added for holding to

move motors.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 32
#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int pinA = 2; // Pin A of the encoder


int pinB = 3; // Pin B of the encoder
int buttonPin = 6; // Pin for the push button change motor and reset
pulse count
int resetButtonPin = 4; // Pin for the reset pulse count only
int button5Pin = 5; // Pin for button5 hold for numberofclick ++
int button7Pin = 7; // Pin for button7 hold for numberofclick --
int numberofclicks = 0; // Stores the number of clicks done by the encoder. 1
turn = 100 clicks
int previous_numberofclicks = 0; // Stores the "previous" number of clicks. Helps
us see if the encoder was moved
float OLEDTimer = 0; // Timer for the screen refresh

#include <AccelStepper.h>
AccelStepper stepper1(1, 9, 8); // Stepper motor 1: pulses/steps 9;
Direction 8
AccelStepper stepper2(1, 10, 11); // Stepper motor 2: pulses/steps 10;
Direction 11
AccelStepper* currentStepper = &stepper1; // Pointer to the currently active
stepper motor
const int stepperEnablePin = 12; // enable/disable pin for the stepper
motor driver

int previousButtonState = HIGH; // Previous state of the button


int previousResetButtonState = HIGH; // Previous state of the reset button
int previousButton5State = HIGH; // Previous state of button5
int previousButton7State = HIGH; // Previous state of button7
bool motorChanged = false; // Flag to track if the motor has been
changed

void setup()
{
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP);
pinMode(button5Pin, INPUT_PULLUP); // Set button5 pin as input
pinMode(button7Pin, INPUT_PULLUP); // Set button7 pin as input

attachInterrupt(digitalPinToInterrupt(pinA), pinAInterrupt, RISING); // pin A is


an interrupt

Serial.begin(115200);
Serial.println("CNC encoder test.");
// OLED part
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
display.clearDisplay();
OLEDTimer = millis(); // start the timer

// Stepper setup
stepper1.setSpeed(4000); // SPEED = Steps / second
stepper1.setMaxSpeed(38500); // SPEED = Steps / second
stepper1.setAcceleration(28500); // ACCELERATION = Steps /(second)^2

stepper2.setSpeed(4000); // SPEED = Steps / second


stepper2.setMaxSpeed(38500); // SPEED = Steps / second
stepper2.setAcceleration(28500); // ACCELERATION = Steps /(second)^2

pinMode(stepperEnablePin, OUTPUT); // enable/disable pin is defined as an


output
digitalWrite(stepperEnablePin, LOW); // enable motor current
}

void loop()
{
checkButton(); // Check the button state and update the current motor if
necessary

showLCD(); // if all conditions are fulfilled, the OLED is updated

currentStepper->moveTo(numberofclicks * 4); // Updates the "go to" position of


the current stepper motor - absolute value

while (currentStepper->distanceToGo() != 0) // This blocks the rest of the


code!
{
currentStepper->runSpeedToPosition(); // Runs to the target position
defined by the moveTo() function
// does not use accelerations
}
}

void pinAInterrupt()
{
// When pin A's wave is detected...

if (digitalRead(pinB) == 0) // if B is LOW, it means that pin A's wave


occurred first -> CW rotation occurred
{
numberofclicks++;
}
else // if B is HIGH, it means that pin B's wave
occurred first. So, when pin A has a rising edge, pin B is already high -> CCW
rotation
{
numberofclicks--;
}
}

void showLCD()
{
if (millis() - OLEDTimer > 200) // update every 200 ms
{
if (previous_numberofclicks != numberofclicks) // if the encoder was moved...
{
display.clearDisplay(); // delete the content of the display
display.setTextSize(1); // Text size for "Pulse"
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Pulse: ");

display.setTextSize(1.2); // Text size for the value


display.println(numberofclicks / 1); // print the new position. you can
use / or * delta to change the pulse count print on display.

float distance_mm = numberofclicks * 0.03; // Calculate the distance in mm


display.setTextSize(1); // Text size for "Distance"
display.print("Distance: ");
display.setTextSize(1); // Text size for the value
display.println(distance_mm, 1); // Print the distance value with 1 decimal
place

display.setTextSize(1); // Text size for "Active motor"


display.print("Active M: ");
display.setTextSize(1); // Text size for the value
if (currentStepper == &stepper1) {
display.println("1");
}
else if (currentStepper == &stepper2) {
display.println("2");
}

display.display();

OLEDTimer = millis(); // reset timer


previous_numberofclicks = numberofclicks; // update current position
}
}
}

void checkButton()
{
int buttonState = digitalRead(buttonPin); // Read the current state of the
button
int resetButtonState = digitalRead(resetButtonPin); // Read the current state of
the reset button
int button5State = digitalRead(button5Pin); // Read the current state of button5
int button7State = digitalRead(button7Pin); // Read the current state of button7

if (buttonState != previousButtonState) // If the button state has changed


{
if (buttonState == LOW) // If the button is pressed
{
// Reset pulse count and move motor to current position
numberofclicks = 0;
currentStepper->setCurrentPosition(0);

motorChanged = true; // Set the motorChanged flag to


true
}
}
if (resetButtonState == LOW) {
// Reset pulse count
numberofclicks = 0;
currentStepper->setCurrentPosition(0);

motorChanged = false; // Reset the motorChanged flag


}

if (button5State == LOW) {
// Increase number of clicks with clickrate 8 pulses per 250ms
unsigned long currentMillis = millis();
static unsigned long previousMillis = 0;
if (currentMillis - previousMillis >= 25) {
numberofclicks += 8;
previousMillis = currentMillis;
}
}

if (button7State == LOW) {
// Decrease number of clicks with clickrate 8 pulses per 250ms
unsigned long currentMillis = millis();
static unsigned long previousMillis = 0;
if (currentMillis - previousMillis >= 25) {
numberofclicks -= 8;
previousMillis = currentMillis;
}
}

previousButtonState = buttonState; // Update the previous button state


previousResetButtonState = resetButtonState; // Update the previous reset button
state

if (motorChanged) {
digitalWrite(stepperEnablePin, HIGH); // Disable motor current
currentStepper->stop(); // Stop the current stepper motor
currentStepper->disableOutputs(); // Disable the outputs of the
current stepper motor
delay(500); // Wait for the motor to stop
completely
digitalWrite(stepperEnablePin, LOW); // Enable motor current
currentStepper->enableOutputs(); // Enable the outputs of the new
current stepper motor

// Switch to the next motor


if (currentStepper == &stepper1) {
currentStepper = &stepper2;
}
else {
currentStepper = &stepper1;
}

display.display();
OLEDTimer = millis(); // reset timer
previous_numberofclicks = -1; // reset previous number of clicks to force
display update

motorChanged = false; // Reset the motorChanged flag


}
}

You might also like