Completed For Upload Onto Lathe Module
Completed For Upload Onto Lathe Module
move motors.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#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
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
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
void loop()
{
checkButton(); // Check the button state and update the current motor if
necessary
void pinAInterrupt()
{
// When pin A's wave is detected...
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.display();
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 (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;
}
}
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
display.display();
OLEDTimer = millis(); // reset timer
previous_numberofclicks = -1; // reset previous number of clicks to force
display update