Created
June 23, 2015 20:23
-
-
Save smashedagainst/cdefacaf4221498513c8 to your computer and use it in GitHub Desktop.
60 degrees stepper motor arduino sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char version[8]= {"v0.1"}; | |
const char today[15] = {"2015 MAY 26"}; | |
#define BTN_INPUT 1 | |
//////////////////////////////////////////////////////////////////////////////// | |
// Stepper motor set-up | |
// | |
#include <Stepper.h> | |
// Define how many steps there are in 1 revolution of your motor | |
#define STEPS_PER_REVOLUTION 200 | |
// Enable (PWM) outputs | |
#define EN1_PIN 3 | |
#define EN2_PIN 9 | |
// Direction outputs | |
#define DIR1_PIN 5 | |
#define DIR2_PIN 6 | |
// Fault inputs, active low | |
//ckck #define FAULT1_PIN 5 | |
#define FAULT2_PIN 8 | |
// General-purpose LED's, active high | |
//ckck #define LED0_PIN 9 // For motor A | |
#define LED1_PIN 10 // For motor A | |
#define LED2_PIN 16 // For motor B | |
#define LED3_PIN 17 // For motor B | |
Stepper stepper(STEPS_PER_REVOLUTION, DIR1_PIN, DIR2_PIN); | |
// Set initial default values | |
unsigned RPM = 30; | |
unsigned PWM = 25; | |
unsigned DIR = 1; | |
// 200 steps per revolution means 60 degrees is 33-1/3 steps | |
unsigned STEPS = STEPS_PER_REVOLUTION/6; | |
unsigned remainder = 3; // every Nth iteration, add a step | |
unsigned compensate=1; | |
unsigned count = 0; | |
unsigned _steps = 0; | |
void rest(){ | |
// Configure all outputs off for now | |
pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, LOW); | |
pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, LOW); | |
pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, LOW); | |
pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, LOW); | |
} | |
void setup() { | |
rest(); | |
// pinMode(LED0_PIN, OUTPUT); digitalWrite(LED0_PIN, LOW); | |
pinMode(LED1_PIN, OUTPUT); digitalWrite(LED1_PIN, LOW); | |
pinMode(LED2_PIN, OUTPUT); digitalWrite(LED2_PIN, LOW); | |
pinMode(LED3_PIN, OUTPUT); digitalWrite(LED3_PIN, LOW); | |
// Configure fault inputs with pullups | |
// pinMode(FAULT1_PIN, INPUT); digitalWrite(FAULT1_PIN, HIGH); | |
pinMode(FAULT2_PIN, INPUT); digitalWrite(FAULT2_PIN, HIGH); | |
Serial.begin(9600); | |
Serial.println("Started"); | |
// Change from divide-by-64 prescale on Timer 2 to divide by 8 to get | |
// 8-times faster PWM frequency (976 Hz --> 7.8 kHz). This should prevent | |
// overcurrent conditions for steppers with high voltages and low inductance. | |
// TCCR2B = _BV(CS21); | |
// Now enable PWM and start motion | |
analogWrite(EN1_PIN, PWM); | |
analogWrite(EN2_PIN, PWM); | |
stepper.setSpeed(RPM); | |
} | |
void button(){ | |
digitalWrite(LED2_PIN,LOW); | |
// Now enable PWM and start motion | |
analogWrite(EN1_PIN, PWM); | |
analogWrite(EN2_PIN, PWM); | |
count++; | |
// if( count == compensate ) | |
// STEPS++; | |
if( compensate && (count % remainder == 0) ) { | |
STEPS++; | |
digitalWrite(LED2_PIN,HIGH); | |
} | |
Serial.print("count "); | |
Serial.print(count); | |
Serial.print(", rotating "); | |
Serial.print(STEPS); | |
Serial.print(" steps "); | |
stepper.step(STEPS); | |
_steps+=STEPS; | |
Serial.print(" total: "); | |
Serial.println(_steps); | |
if( count == 60 ) { | |
count=0; | |
_steps=0; | |
} | |
// put STEPS back | |
STEPS = STEPS_PER_REVOLUTION/6; | |
// Serial.println("Sleeping..."); | |
delay(100); // catch when button held | |
} | |
//////////////////////////////////////////////////////////////////////////////// | |
void loop() { | |
int input; | |
int idx; | |
input = analogRead(BTN_INPUT); | |
if( input < 100 ) { | |
// for(int i=6; i>0; i--) { | |
Serial.print("Button read: "); | |
Serial.println(input); | |
button(); | |
Serial.println("Done."); | |
idx=0; | |
do { | |
delay(100); | |
input = analogRead(BTN_INPUT); | |
if( idx++ ) | |
Serial.println("Button still held down?"); | |
} while( input < 100 ); | |
// } | |
} | |
// send data only when you receive data: | |
if (Serial.available() > 0) { | |
Serial.println("-------------------------"); | |
char incomingByte; | |
// read the incoming byte: | |
incomingByte = Serial.read(); | |
if( incomingByte == '+' ) | |
STEPS += 1; | |
if( incomingByte == '-' ) | |
STEPS -= 1; | |
if( incomingByte == '+' || incomingByte == '-' ){ | |
Serial.print("# steps changed: "); | |
Serial.println(STEPS); | |
} ///////////////////////////////////////////////// | |
if( incomingByte == 'f' ) | |
RPM += 1; | |
if( incomingByte == 's' && RPM > 5 ) | |
RPM -= 1; | |
if( incomingByte == 's' || incomingByte == 'f' ){ | |
stepper.setSpeed(RPM); | |
Serial.print("RPM changed: "); | |
Serial.println(RPM); | |
} ///////////////////////////////////////////////// | |
if( incomingByte == 'p' ) | |
PWM += 5; | |
if( incomingByte == 'o' && PWM > 5 ) | |
PWM -= 5; | |
if( incomingByte == 'p' || incomingByte == 'o' ){ | |
rest(); | |
Serial.print("PWM changed: "); | |
Serial.println(PWM); | |
} ///////////////////////////////////////////////// | |
if( incomingByte == 'a' ) | |
button(); | |
if( incomingByte == 'r' ) | |
rest(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment