Skip to content

Instantly share code, notes, and snippets.

@vishnumaiea
Last active April 26, 2017 13:26
Show Gist options
  • Save vishnumaiea/e44d8d80caaaaf84605ddc3944f0f30a to your computer and use it in GitHub Desktop.
Save vishnumaiea/e44d8d80caaaaf84605ddc3944f0f30a to your computer and use it in GitHub Desktop.
Code for controlling a CD stepper motor from the serial monitor with Arduino UNO and L293D driver.
//-----------------------------------------------------------------//
// CD-Stepper-Serial-Drive //
// //
// This is a modified version of the example from the Arduino //
// Stepper library. A CD stepper motor can be controlled via //
// serial commands. You can use either L293D or L298 motor //
// drivers. The schematic an be foound in the Stepper library //
// documentation. This code is compatible with 4-pin and //
// 2-pin configuration. Both works the same way, but you can //
// save 2 Arduino pins with 2-pin configuration but with the //
// addition of two transistors. //
// //
// Use "Stepper myStepper(stepsPerRevolution,8,9);" for 2-pin //
// config and Stepper myStepper(stepsPerRevolution,8,9, 10, 11); //
// for 4-pin config. //
// //
// Once you upload the code, open the serail monitor. Enter //
// and send "s" to start the motor and "h" to stop the motor. //
// //
// Author : Vishnu M Aiea //
// Wesite : www.vishnumaiea.in //
// Date : IST 1:14 PM 12-04-2017, Wednesday //
#include <Stepper.h>
const int stepsPerRevolution = 20; //standard step count for CD steppers
Stepper myStepper(stepsPerRevolution,8,9); //create a Stepper object
int motorSpeed = 1200;
int stepCount = 256; //step count to rotate. Equal to full swing of a CD stepper drive.
int timeDelay = 500;
int incomingByte = 0;
int latch = 1; //to latch and store the current operation
int forCount = 0; //forward slide count
int revCount = 0; //reverse slide count
void setup() {
myStepper.setSpeed(motorSpeed);
Serial.begin(9600);
}
void loop() {
if(Serial.available()>0)
{
incomingByte = Serial.read();
if(incomingByte == 'h')
{
forCount = 0;
revCount = 0;
latch = 1; //stop rotation
}
else if(incomingByte == 's')
{
latch = 0; //start rotation
}
else latch = 1; //default
}
if(latch == 0)
{
// perform a given revolutions in forward
forCount++;
Serial.print("Forward- ");
Serial.println(forCount);
myStepper.step(stepCount);
delay(timeDelay);
// perform a given revolutions in reverse
revCount++;
Serial.print("Reverse- ");
Serial.println(revCount);
myStepper.step(-stepCount);
delay(timeDelay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment