Also,
Here is my code as a whole, without using millis(). I figure i should try to debig this one first, as it is the simpler of the two codes. Additionally, i really appreciate it the responses and help from you guys.
// "Select" button allows user to select which profile to punch.
// "Start" button starts punching the profile (or pauses the machine if it is already punching).
// "Reset" button lets user tell machine that the workpiece has been removed.
#include "AccelStepper.h" //use "" and no <>, b/c accelstepper is a librabry that i defined
class pneumaticClamp
{
int myPin;
// Constructor
public:
pneumaticClamp( int pin )
{
myPin = pin;
}
void clamp()
{
digitalWrite( myPin, HIGH );
}
void unClamp()
{
digitalWrite( myPin, LOW );
}
};
class punch
{
int myPin;
// Constructor
public:
punch( int pin )
{
myPin = pin;
}
void punchHole()
{
digitalWrite( myPin, HIGH );
delay( 1000 ); // Give it time to punch through the material
digitalWrite( myPin, LOW );
}
};
//board stuff
const int serialBaudRate = 9600; // [bps]
const int oneTimeStep = 33; // Smallest timestep in milliseconds
//pin assignments
const int selectButtonPin = 4; // Number of pin connected to "select" button
const int startButtonPin = 5; // Number of pin connected to "start" button
const int resetButtonPin = 6; // Number of pin connected to "start" button
const int mechStopPins[] = { 10, 11, 12, 13 };
const int mobileClampPin = 8;
const int staticClampPin = 9;
const int alertPin = 23;
const int punchPin = 7;
const int profileSwitchPin = 27;
//constants for motor movement
//float stepsPerInch = 2272.727;
float stepsPerInch = 1408.45;
//profile checking
bool punchingAProfile = false;
bool workpieceNeedsToBeRemoved = false;
bool selectIsDown = false;
bool selectWasDown = false;
bool selectFreshlyUp = false;
bool startIsDown = false;
bool startWasDown = false;
bool startFreshlyUp = false;
bool resetIsDown = false;
bool resetWasDown = false;
bool resetFreshlyUp = false;
const int profileSetALength = 21;
const int profileSetBLength = 29;
//hole patterns, in inches
float profileSetA[ 4 ][ profileSetALength ] = {
{ 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0,0 }, //IS-8-bottom rail
{ 4.000, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 5.333, 0, 0, 0, 0, 0, 0, 0,0 }, //IS-6-bottom rail
{ 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
{ 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0} //IL-6-bottom rail
};
// a list of the intervals between consecutive holes for each profile. If 0, that means there is no next hole.
float profileSetB[ 4 ] [ profileSetBLength ] = {
{ 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333,2.67, 5.333, 0, 0, 0,0 }, //IS-8-bottom rail
{ 4.000, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 5.333, 2.67, 5.333, 0, 0, 0, 0, 0, 0, 0,0 }, //IS-6-bottom rail
{ 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364}, //IL-8-botoom rail
{ 3.273, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 4.364, 0, 0, 0, 0, 0} //IL-6-bottom rail
};
int chosenProfile = 0; // 0: 6S, 1: 6L, 2: 8S, 3: 8L
pneumaticClamp mobileClamp( mobileClampPin );
pneumaticClamp staticClamp( staticClampPin );
punch unicyl( punchPin );
//stepper motor intilization, for accel stepper
AccelStepper stepper(1,3,2); //pin 3 =pluse, pin 2=direction
void setup()
{
pinMode( mechStopPins[0], OUTPUT );
pinMode( mechStopPins[1], OUTPUT );
pinMode( mechStopPins[2], OUTPUT );
pinMode( mechStopPins[3], OUTPUT );
pinMode( mobileClampPin, OUTPUT );
pinMode( punchPin, OUTPUT);
pinMode( staticClampPin, OUTPUT );
pinMode( alertPin, OUTPUT );
pinMode( selectButtonPin, INPUT );
pinMode( startButtonPin, INPUT );
pinMode( resetButtonPin, INPUT );
pinMode( profileSwitchPin, INPUT );
Serial.begin( serialBaudRate );
//for stepper motor control
stepper.setMaxSpeed(1500); //in steps per second
stepper.setAcceleration(800); //in steps per second
}
void loop()
{
selectIsDown = digitalRead( selectButtonPin );
startIsDown = digitalRead( startButtonPin );
resetIsDown = digitalRead( resetButtonPin );
bool profileSwitch = digitalRead( profileSwitchPin );
if ( selectWasDown && !selectIsDown ){ selectFreshlyUp = true; }
if ( startWasDown && !startIsDown ){ startFreshlyUp = true; }
if ( resetWasDown && !resetIsDown ){ resetFreshlyUp = true; }
digitalWrite( mechStopPins[0], LOW );
digitalWrite( mechStopPins[1], LOW );
digitalWrite( mechStopPins[2], LOW );
digitalWrite( mechStopPins[3], LOW );
if ( punchingAProfile ){
// if punching a profile
punchingAProfile = false;
if (profileSwitch) {
punchProfile(profileSetA[chosenProfile], profileSetALength);
} else {
punchProfile(profileSetB[chosenProfile], profileSetBLength);
}
workpieceNeedsToBeRemoved = true;
digitalWrite( alertPin, HIGH );
} else {
// if not punching a profile
if ( workpieceNeedsToBeRemoved ) {
//see if reset button was freshly pressed and if so, set this variable to false.
if ( resetFreshlyUp ) {
workpieceNeedsToBeRemoved = false;
digitalWrite( alertPin, LOW );
}
} else {
if ( selectFreshlyUp ) {
if ( chosenProfile == 3 ) {
chosenProfile = 0;
} else {
chosenProfile++;
}
}
if ( startFreshlyUp ) {
// Switch into "punching a profile" mode
punchingAProfile = true;
}
digitalWrite( mechStopPins[ chosenProfile ], HIGH );
}
}
selectWasDown = selectIsDown;
startWasDown = startIsDown;
resetWasDown = resetIsDown;
startFreshlyUp = false;
selectFreshlyUp = false;
resetFreshlyUp = false;
delay( oneTimeStep );
}
void punchProfile(float profile[], int profileLength)
{
mobileClamp.clamp();
staticClamp.clamp();
delay( 2000 ); // Give clamps time to clamp securely
for (int i=0; i < profileLength; i++){
float distanceToMove = profile[ i ];
if ( distanceToMove != 0 )
{
unicyl.punchHole();
moveWorkpieceForward( distanceToMove );
}
}
unicyl.punchHole();
mobileClamp.unClamp();
staticClamp.unClamp();
delay( 2000 );
}
void moveWorkpieceForward( float distanceToMove )
{
int xa=0;
int xb=0;
//Serial.print( "Moving workpiece " );
//Serial.print( distanceToMove );
staticClamp.clamp();
mobileClamp.unClamp();
Serial.print( "mClamp -> " );
Serial.print( distanceToMove );
xa = moveMobileCylinderForward(distanceToMove); //moving forward
Serial.print( ". " );
delay( 1000 );
//delay( 1000*distanceToMove ); // REMOVE
mobileClamp.clamp();
staticClamp.unClamp();
Serial.print( "mClamp <- " );
Serial.print( distanceToMove );
xb = moveMobileCylinderBackward(distanceToMove ); //moving backward
Serial.print( ". " );
delay( 1000 );
//delay( 1000*distanceToMove ); //REMOVE
staticClamp.clamp();
//delay(2000); //REMOVE
}
int moveMobileCylinderForward( float distanceInInches )
{
long stepsToMove = distanceInInches*stepsPerInch;
long deccel_step = stepsToMove-1000;
//move forward
stepper.moveTo(stepsToMove);
while (stepper.currentPosition() != stepsToMove) // Full speed up to 300
{
stepper.run();
}
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
// Now stopped after quickstop
}
int moveMobileCylinderBackward( float distanceInInches )
{
long stepsToMove = -1*distanceInInches*stepsPerInch;
long deccel_step = stepsToMove-1000;
//move forward
stepper.moveTo(stepsToMove); // negative because moving backwards
while (stepper.currentPosition() != 0) // Full speed back to 0
{
stepper.run();
}
stepper.stop(); // Stop as fast as possible: sets new target
stepper.runToPosition();
}