Last active
August 29, 2015 14:00
-
-
Save andysmithfal/11398596 to your computer and use it in GitHub Desktop.
Arduino, two stepper motors with serial input driven like 360º servos. Processing interface.
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
//Code for Arduino | |
// | |
#include <Stepper.h> | |
//may need to change this depending on manufacturing variences.. | |
const int stepsPerRevolution = 2050; | |
//placeholders to keep track of both motors' positions | |
unsigned int currentPos1 = 0; | |
unsigned int currentPos2 = 0; | |
//placeholder for numerical value received over serial | |
int value; | |
//stepper motor objects | |
Stepper stepper1(stepsPerRevolution, 2, 3, 4, 5); | |
Stepper stepper2(stepsPerRevolution, 10, 11, 12, 13); | |
void setup() { | |
//max speed for motor (28BYJ-48) | |
stepper1.setSpeed(5); | |
stepper2.setSpeed(5); | |
//start serial comms | |
Serial.begin(9600); | |
} | |
void loop() { | |
int i; | |
if(Serial.available()){ | |
i = Serial.read(); | |
if ((i>='0') && (i<='9')) { | |
value = 10*value + i - '0'; | |
} else { | |
//sending ###q or ###w over serial will reset where the arduino | |
//currently thinks the motor is - e.g. 0q will reset m1 to 0 degrees | |
if (i=='q') { | |
Serial.print("Setting current pos of m1 to "); | |
Serial.println(value); | |
currentPos1 = value; | |
} | |
if (i=='w') { | |
Serial.print("Setting current pos of m2 to "); | |
Serial.println(value); | |
currentPos2 = value; | |
} | |
//main input commands - 90l will move m1 to 90 degees | |
if(i=='l'){ | |
moveToAngle(value, currentPos1, stepper1); | |
Serial.print('k'); | |
} | |
if(i=='r'){ | |
moveToAngle(value, currentPos2, stepper2); | |
Serial.print('k'); | |
} | |
value = 0; | |
} | |
} | |
} | |
void moveToAngle( int value, unsigned int ¤tPos, Stepper &motor){ | |
//this function calculates the difference in angles | |
//and moves the motor accordingly | |
int moveValue = 0; | |
if(value < currentPos){ | |
moveValue = (360-currentPos)+value; | |
} | |
if(value > currentPos){ | |
moveValue = value-currentPos; | |
} | |
float factor = (float)stepsPerRevolution/360; | |
float stepsToMove = factor*moveValue; | |
motor.step((int)stepsToMove); | |
currentPos = value; | |
} |
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
//Code for Processing | |
// | |
import processing.serial.*; | |
Serial arduino; | |
String typedText = "abc"; | |
PFont font; | |
void setup() { | |
size(700, 200); | |
arduino = new Serial(this, Serial.list()[5], 9600); | |
font = createFont("Helvetica", 18); | |
} | |
void draw() { | |
background(255); | |
fill(255,0,0); | |
textFont(font,18); | |
text(typedText+(frameCount/10 % 2 == 0 ? "_" : ""), 35, 45); | |
} | |
void keyReleased() { | |
if (key != CODED) { | |
switch(key) { | |
case BACKSPACE: | |
typedText = typedText.substring(0,max(0,typedText.length()-1)); | |
break; | |
case TAB: | |
typedText += " "; | |
break; | |
case ENTER: | |
case RETURN: | |
processText(typedText); | |
typedText = ""; | |
break; | |
case ESC: | |
case DELETE: | |
break; | |
default: | |
typedText += key; | |
} | |
} | |
} | |
void processText(String t){ | |
//loop through input string | |
char[] allChars = new char[t.length()]; | |
for (int i = 0; i < t.length(); i++) { | |
allChars[i] = t.charAt(i); | |
int[] values = getValue(allChars[i]); | |
sendChar(values[0], 'l'); | |
sendChar(values[1], 'r'); | |
println("sleep"); | |
delay(4000); | |
println("nosleep"); | |
} | |
} | |
int[] getValue(char letter){ | |
//define a placeholder for our values | |
int[] selectedLetter = new int[2]; | |
//match up the input letter to a set of angles | |
switch(letter){ | |
case 'a': | |
selectedLetter[0] = 120; | |
selectedLetter[1] = 80; | |
break; | |
case 'b': | |
selectedLetter[0] = 40; | |
selectedLetter[1] = 130; | |
break; | |
case 'c': | |
selectedLetter[0] = 280; | |
selectedLetter[1] = 340; | |
break; | |
case 'd': | |
selectedLetter[0] = 40; | |
selectedLetter[1] = 90; | |
break; | |
//and so on and so on... | |
default: | |
selectedLetter[0] = 0; | |
selectedLetter[1] = 0; | |
} | |
//pass back the matched up letter | |
return selectedLetter; | |
} | |
void sendChar(int deg, char motor){ | |
println("senChar routine"); | |
println(deg+""+motor); | |
arduino.write(deg+""+motor); | |
int co = 0; | |
while (arduino.available() == 0){ | |
//do nothing whilst we wait for a response | |
if(co == 0){ | |
println("waiting for arduino"); | |
} | |
co++; | |
} | |
while (arduino.available() > 0) { | |
//there is an incoming byte in the serial buffer | |
//this will be a confirmation that the motor has | |
//reached its position | |
int inByte = arduino.read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment