Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- socdough
- Date:
- 2011-08-18
- Revision:
- 0:506d7846938a
- Child:
- 1:529333e22121
File content as of revision 0:506d7846938a:
/*
Project Stepper
Written by Tony Cacace & Brandon Ring
Date: 08-18-11
Rev: AB
This program will use the mbed micro-controller to step a stepper motor forward and reverse
based on the users input request.
*/
#include "mbed.h"
#include "TextLCD.h"
#define DELAYSEQ 3.0 // A wait used between the main while loop
TextLCD lcd(p24, p26, p27, p28, p29, p30);
DigitalOut enable1(p21);
DigitalOut enable2(p22);
DigitalOut STP1(p11);
DigitalOut STP2(p12);
DigitalOut STP3(p13);
DigitalOut STP4(p14);
Serial pc(USBTX, USBRX); // tx, rx
void moveSteps (int); // Fuction prototype: used to define how many steps to take
volatile int count = 1; //keeps track of counted steps 1-4
float delayStep = 0.5; //Speed of motor.....0.005
int main() {
enable1 = 1; //Turn on driver
enable2 = 1; //Turn on driver
// int inputdir; //Variable used to define step direction.
int inputsteps; //Variable used to define step amount.
do { //Print menu on PC to request what to do from the user.
pc.printf("Menu\n\n");
pc.printf("1. Home Stage.\n");
pc.printf("2. Move Stage Forward.\n");
pc.printf("3. Move Stage Backwards.\n");
pc.printf("4. Exit Program.\n\n");
pc.scanf("%d",&count);
pc.printf("Option %i\n\n",count);
switch (count) { //Switch function to move to loop requested by user.
case 1: // (HOME) Call function here to do the required operation
pc.printf("We're sorry, this function is not available at this time!!\n\n");
break;
case 2: // (Move Forward) Call function here to do the required operation
inputsteps=1;
while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
pc.printf(" Enter a number between 1 and 100.\n\n");
pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
lcd.cls(); //Clears LCD screen.
lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
int stepcount = 0; // keeps track of how many steps were taken
pc.printf("Move started....\n\n"); //Prints so user knows command was received.
while (stepcount<inputsteps) { // compares how many steps taken to the desired steps passed in
count++;
if (count == 5) // if the motor went around oncce..
count=1; // ...reset the counter
switch (count) {
case 1: // if count equals 1 do this
STP1 = 1;
STP2 = 0;
STP3 = 0;
STP4 = 0;
break; // this skips the code below until stepcount++
case 2:
STP1 = 0;
STP2 = 0;
STP3 = 1;
STP4 = 0;
break;
case 3:
STP1 = 0;
STP2 = 1;
STP3 = 0;
STP4 = 0;
break;
case 4:
STP1 = 0;
STP2 = 0;
STP3 = 0;
STP4 = 1;
break;
}
stepcount++; //Iterate loop to keep track of steps.
lcd.cls(); //Clear mbed LCD screen.
lcd.printf("%i", stepcount);//Print loop count on LCD.
wait(delayStep); //Defines how fast to step the motor.
}
inputsteps=0; //Reset inputsteps so that while loop becomes false.
pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
}
break;
case 3: // (Move Backward) Call function here to do the required operation
inputsteps=1;
while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
pc.printf(" Enter a number between 1 and 100.\n\n");
pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
lcd.cls(); //Clears LCD screen.
lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
int stepcount = 0; // keeps track of how many steps were taken
inputsteps=-1*inputsteps; // reverses sign so that motor steps backwards.
pc.printf("Move started....\n\n"); //Prints so user knows command was received.
while (stepcount>inputsteps) {
count--;
if (count == 0) // if the motor went around oncce..
count=4; // ...reset the counter
switch (count) {
case 1: // if count equals 1 do this
STP1 = 1;
STP2 = 0;
STP3 = 0;
STP4 = 0;
break;
case 2: // this skips the code below until stepcount--
STP1 = 0;
STP2 = 0;
STP3 = 1;
STP4 = 0;
break;
case 3:
STP1 = 0;
STP2 = 1;
STP3 = 0;
STP4 = 0;
break;
case 4:
STP1 = 0;
STP2 = 0;
STP3 = 0;
STP4 = 1;
break;
}
stepcount--; //Iterate loop to keep track of steps.
lcd.cls(); //Clear mbed LCD screen.
lcd.printf("%i", stepcount);//Print loop count on LCD.
wait(delayStep);
}
inputsteps=0; //Reset inputsteps so that while loop becomes false.
pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
}
break;
case 4: // (End Program) Call function here to do the required operation
printf("Goodbye\n\n");
wait(100000); //Wait forever!
break;
default: // (Invaild Input)
pc.printf("Invaild Choice. Try again...\n\n");
wait(1);
break;
}
}
while (1); //Program won't run without this while loop!
}