0% found this document useful (0 votes)
14 views8 pages

Arduino Stepper Motor Control Code

The document is a code for controlling a stepper motor and a motor using an Arduino with an LCD display. It includes functionality for reading inputs from buttons and a potentiometer, managing motor speed, and displaying status information on the LCD. The code also handles serial communication for machine status updates and includes interrupt routines for detecting motor rotations.

Uploaded by

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

Arduino Stepper Motor Control Code

The document is a code for controlling a stepper motor and a motor using an Arduino with an LCD display. It includes functionality for reading inputs from buttons and a potentiometer, managing motor speed, and displaying status information on the LCD. The code also handles serial communication for machine status updates and includes interrupt routines for detecting motor rotations.

Uploaded by

Dwi Sabda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <LiquidCrystal_I2C.

h>

#include <Stepper.h>

//===== STEPPER =====

#define pinSwitchMode A0

#define pinButtonCW A1

#define pinButtonCCW A2

#define pinPot A3

#define stepPerRevolution 200

#define pinStep 4

#define pinDir 3

#define pinEnable 7

Stepper motor(stepPerRevolution, pinDir, pinStep); //8 direction, 9 step

int stepperSpeed, mnSpeed = 5, mxSpeed = 1500;

bool stepperMode = false,firstAuto=true;

unsigned long startAuto, durAuto;

int ml;

//===== MOTOR & FLYBACK =====

#define pinMotor 6

#define pinPwm 5

#define pinOpto 2

#define pinLed 13

int motorSpeed = 0, pwmSpeed = 0;

unsigned long startTime, stopTime, rpm;

bool firstStart = true;

//===== DISPLAY & UPDATE =====

LiquidCrystal_I2C lcd(0x27, 20, 4);


unsigned long lastDisplay;

int delayDisplay = 2000;

char text[50];

bool machineStatus = false, connectStatus = false;

char chrTemp;

String strTemp, receiveTime = "00:00:00";

void detect(){

// digitalWrite(pinLed, HIGH);

// unsigned long nowTime = micros();

detachInterrupt(digitalPinToInterrupt(2));

// [Link](".");

// stopTime = nowTime;

unsigned long delta = stopTime-startTime;

//seperempat rotasi = sekian detik, berarti 1 menit nantinya berapa rotasi ?

// misal 0.25, rotasi = 0.1 menit, maka 1 menit, = 2.5 rotasi

rpm++;

//rpm = 60000/delta;

attachInterrupt(digitalPinToInterrupt(2), detect, RISING);

// startTime=nowTime;

// digitalWrite(pinLed, LOW);

void setup() {

//===== INIT INPUT OUTPUT =====

pinMode(pinButtonCW, INPUT_PULLUP);

pinMode(pinButtonCCW, INPUT_PULLUP);

pinMode(pinSwitchMode, INPUT_PULLUP);

// pinMode(pinOpto,INPUT_PULLUP);

pinMode(pinEnable, OUTPUT); digitalWrite(pinEnable, HIGH);


pinMode(pinMotor, OUTPUT); digitalWrite(pinMotor, HIGH);

pinMode(pinPwm, OUTPUT); digitalWrite(pinPwm, HIGH);

pinMode(pinLed, OUTPUT); digitalWrite(pinLed, HIGH);

//===== INIT DISPLAY & SERIAL =====

[Link](); [Link]();

[Link](0, 0); [Link]("=INJECTIONS MACHINE=");

[Link](0, 1); [Link](" ");

[Link](0, 2); [Link](" OPELIOT SOLUTION ");

[Link](0, 3); [Link](" [085222818623] ");

[Link](9600);

lcdBlink(100, 200, 5);

attachInterrupt(digitalPinToInterrupt(2), detect, RISING);

void loop() {

cekSerial();

controlStepper();

controlMotor();

controlPwm();

setDisplay();

delay(50);

void cekSerial() {

//FORMAT *MACHINESTATUS,SPEEDMOTOR,SPEEDPWM,CON,TIME#

while ([Link]()) {

chrTemp = [Link]();

if (chrTemp == '#') {

// [Link](strTemp);

int len = [Link]();


strTemp = [Link](len - 21, len);

// [Link](0, 0); [Link](strTemp);

machineStatus = [Link](1, 2).toInt();

motorSpeed = [Link](3, 6).toInt();

pwmSpeed = [Link](7, 10).toInt();

connectStatus = [Link](11, 12).toInt();

receiveTime = [Link](13, len);

// [Link](receiveTime);

strTemp = "";

[Link](); //delay(500);

[Link](9600); delay(200);

//FORMAT *MACHINESTATUS,SPEEDSTEPPER,MODESTEPPER#

sprintf(text, "*%d,%d,%05d,%03d#", machineStatus, 1 - stepperMode,ml, map(stepperSpeed,


mnSpeed, mxSpeed, 0, 100));

[Link](text);

[Link](0, 0); [Link](strTemp);

else {

strTemp += chrTemp;

void controlStepper() {

int analogValue = analogRead(pinPot);

stepperSpeed = map(analogValue, 0, 1023, mnSpeed, mxSpeed);

stepperMode = digitalRead(pinSwitchMode);

if (stepperSpeed > 0) {

[Link](stepperSpeed);
digitalWrite(pinEnable, HIGH);

if (stepperMode == false && machineStatus == true) {

if(firstAuto)startAuto = millis();

durAuto = millis()-startAuto;

//===== KONVERSI DURASI STEPPER =====

ml = durAuto/100;

digitalWrite(pinEnable, LOW);

[Link](5);

firstAuto=false;

} else {

durAuto = 0;

ml=0;

firstAuto=true;

if (digitalRead(pinButtonCW) == LOW) {

digitalWrite(pinEnable, LOW);

[Link]("+");

[Link](stepPerRevolution / 100);

} else if (digitalRead(pinButtonCCW) == LOW) {

digitalWrite(pinEnable, LOW);

[Link]("-");

[Link](-(stepPerRevolution / 100));

} else {

digitalWrite(pinEnable, HIGH);

} else {

digitalWrite(pinEnable, HIGH);

}
void controlMotor() {

if (machineStatus == true) {

setMotor(map(motorSpeed, 0, 240, 0, 255));

// if(firstStart)attachInterrupt(digitalPinToInterrupt(2), detect, FALLING);

firstStart = false;

else {

// rpm = 0;

setMotor(0);

firstStart = true;

// detachInterrupt(digitalPinToInterrupt(2));

void controlPwm() {

if (machineStatus == true) {

setPwm(map(pwmSpeed, 0, 100, 0, 255));

else {

setPwm(0);

void setDisplay() {

if (millis() > lastDisplay + delayDisplay) {

//===== LCD =====

//status, speedStepper, speedMotor, speedPwm

//statusStepper(manual/ Auto), status Koneksi

[Link](0, 0); [Link](machineStatus ? "===[ MACHINE ON ]===" : "===[MACHINE OFF!]===");

float pwmF = pwmSpeed*0.9989+0.21;

int pwmI = int(pwmF*10);


sprintf(text, "CON :%3s PWM:%3d.%1dkV", connectStatus ? " OK" : "---", pwmI/10,pwmI%10);

[Link](0, 1); [Link](text);

sprintf(text, "STE :%3d RPM: %6lu", stepperSpeed / 15, rpm);

// sprintf(text, "STE :%3d DRU: %2d.%1dV", stepperSpeed / 15, motorSpeed/10,motorSpeed%10);

[Link](0, 2); [Link](text);

sprintf(text, "%10s %7dml", stepperMode ? "[ MANUAL ]" : "[ AUTO ]",ml);

[Link](0, 3); [Link](text);

lastDisplay = millis();

void setPwm(int pwmValue) {

analogWrite(pinPwm, 255 - pwmValue);

void setMotor(int motorValue) {

analogWrite(pinMotor, 255 - motorValue);

void lcdBlink(int timeOff, int timeOn, int perulangan) {

for (int i = 0; i < perulangan; i++) {

[Link](); delay(timeOff);

[Link](); delay(timeOff);

You might also like