Skip to content
Browse files

new smoother motor control - also updated to new arduino ino file

  • Loading branch information...
1 parent 9e70f7f commit b27b15e2c2e31e88d6d587c70ef67eaf9778c753 @tbuser committed Apr 30, 2012
Showing with 84 additions and 72 deletions.
  1. +84 −0 firmware/firmware.ino
  2. +0 −72 firmware/firmware.pde
View
84 firmware/firmware.ino
@@ -0,0 +1,84 @@
+int ledPin = 13;
+int laserPin = 2;
+int dirPin = 3;
+int stepPin = 4;
+float gearRatio = 61.0/10.0;
+
+void setup() {
+ pinMode(laserPin, OUTPUT);
+ pinMode(dirPin, OUTPUT);
+ pinMode(stepPin, OUTPUT);
+ pinMode(ledPin, OUTPUT);
+
+ Serial.begin(9600);
+}
+
+void rotate(int steps, float speed){
+ //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
+ //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
+ int dir = (steps > 0)? HIGH:LOW;
+ steps = abs(steps);
+
+ digitalWrite(dirPin,dir);
+
+ float usDelay = (1/speed) * 70;
+
+ for(int i=0; i < steps; i++){
+ digitalWrite(stepPin, HIGH);
+ delayMicroseconds(usDelay);
+
+ digitalWrite(stepPin, LOW);
+ delayMicroseconds(usDelay);
+ }
+}
+
+void rotateDeg(float deg, float speed){
+ //rotate a specific number of degrees (negitive for reverse movement)
+ //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
+ int dir = (deg > 0)? HIGH:LOW;
+ digitalWrite(dirPin,dir);
+
+ int steps = abs(deg)*(1/0.225)*gearRatio;
+ float usDelay = (1/speed) * 70;
+
+ for(int i=0; i < steps; i++){
+ digitalWrite(stepPin, HIGH);
+ delayMicroseconds(usDelay);
+
+ digitalWrite(stepPin, LOW);
+ delayMicroseconds(usDelay);
+ }
+}
+
+void laser(boolean state) {
+ digitalWrite(ledPin, state);
+ digitalWrite(laserPin, state);
+}
+
+void loop() {
+ while (Serial.available() == 0);
+
+ int val = Serial.read();
+
+ if (val == '0') {
+ laser(false);
+ Serial.println("OK");
+ } else if (val == '1') {
+ laser(true);
+ Serial.println("OK");
+ } else if (val == '2') {
+ rotateDeg(360, 0.03);
+ Serial.println("OK");
+ } else if (val == '3') {
+ laser(true);
+ rotateDeg(360, 0.03);
+ laser(false);
+ Serial.println("OK");
+ } else {
+ Serial.println("Unknown Command");
+ }
+
+ delay(50);
+
+ Serial.flush();
+}
View
72 firmware/firmware.pde
@@ -1,72 +0,0 @@
-int ledPin = 13;
-int laserPin = 2;
-int dirPin = 3;
-int stepPin = 4;
-int stepSpeed = 4000;
-float gearRatio = 61.0/10.0;
-
-void setup() {
- pinMode(laserPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- pinMode(stepPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
-
- Serial.begin(9600);
-}
-
-void stepper(float turnDegrees, int stepSpeed) {
- boolean dir;
- float steps;
-
- if (turnDegrees > 0) {
- dir = true;
- } else {
- dir = false;
- }
-
- digitalWrite(dirPin, dir);
-
-// delay(50);
-
- steps = turnDegrees/360.0 * 1600.0 * gearRatio;
-
- for (int i = 0; i < steps; i++) {
- digitalWrite(stepPin, LOW);
- delayMicroseconds(stepSpeed/2);
- digitalWrite(stepPin, HIGH);
- delayMicroseconds(stepSpeed/2);
- }
-}
-
-void laser(boolean state) {
- digitalWrite(ledPin, state);
- digitalWrite(laserPin, state);
-}
-
-void loop() {
- while (Serial.available() == 0);
-
- int val = Serial.read();
-
- if (val == '0') {
- laser(false);
- Serial.println("OK");
- } else if (val == '1') {
- laser(true);
- Serial.println("OK");
- } else if (val == '2') {
- stepper(360.0, stepSpeed);
- Serial.println("OK");
- } else if (val == '3') {
- laser(true);
- stepper(360.0, stepSpeed);
- laser(false);
- Serial.println("OK");
- } else {
- Serial.println("Unknown Command");
- }
-
- delay(50);
-
- Serial.flush();
-}

0 comments on commit b27b15e

Please sign in to comment.
Something went wrong with that request. Please try again.