Beruflich Dokumente
Kultur Dokumente
html
//SETUP
void setup() {
//Motor is is controled by a direction and speed input
pinMode(12,OUTPUT); //Direction
pinMode(3,OUTPUT); //Voltage
pinMode(9,OUTPUT); //Brake
pinMode(7,INPUT); //already a pulldown switch
//USE Pin #2 to count the interrupt.everytime there is a rising interrupt fin the function count
attachInterrupt(digitalPinToInterrupt(2),count,RISING); //RISING, FALLING, or CHANGE
//Define Variables
int v=0, current_mmps=0;
float vel_curr=0, err=0, tStart=0;
volatile long int pulses=0, ref_pulses=0; //helps it run quickly
volatile int initial_pulses=0;
//STEP #1
//Retract stage to the homing switch at 35 mm/s
digitalWrite(Dir,HIGH); //HIGH = CW = Retracting
v=100; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed Input
while(digitalRead(Button)){ //UNTIL it hits button
vel_curr= getSpeed(20000); //Get current speed
err=(35+vel_curr); //Calculate error between actual and goal; want (+) value
v += K1*err; //Change Voltage input
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
ref_pulses = pulses; //Establishes 0 point for distance
stop1(); //stop for 1 second
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Stop 1");
//STEP #2
//Advance 10 mm from the switch at 20 mm/s
digitalWrite(Dir,LOW); //LOW = CCW = Advance
v=50; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed
//STEP #3
//Retract to homing switch at lowest possible speed
digitalWrite(Dir,HIGH); //HIGH = CW = Retract
v=50; //Minimum initial speed
analogWrite(Voltage,v); //Slowest possible speed
while(digitalRead(Button)){ //UNTIL it hits button
vel_curr=getSpeed(50000); //Get current speed
err=(6+vel_curr); //Calculate error - SET AT LOWEST POSSIBLE SPEED FROM TESTS
v += K3*err; //Change Voltage input - DECREASE TO GO SLOWER
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
ref_pulses=pulses; //Re-estableshes 0 point immediately when switch is hit
stop1(); //Stops motion for 1 second
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Stop 3");
//STEP #4
//Advance 30 mm from the switch at 10 mm/s
digitalWrite(Dir,LOW); //LOW = CCW = Advance
v=50; //Set initial voltage
analogWrite(Voltage,v); //Inital Speed
while(pulses-ref_pulses < CF*(30*374/8)){ //move 30 mm =1402 pulses
vel_curr=getSpeed(20000); //Get current speed
err=(10-vel_curr); //Calculate error
v += K4*err; //Change Voltage input
if (v>255) //Limits Voltage to possible inputs
v=255;
else if (v<0)
v=0;
analogWrite(Voltage,v); //Rewrite voltage
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+String(v)+"\t"+String(err));
}
stop1();
vel_curr= getSpeed(20000); //Get current speed
Serial.println(String(millis()-tStart)+"\t"+String(vel_curr)+"\t"+String(pulses)+"\t"+String(pulses-ref_pulses)+"\t"+"Final Stop");
//Turn off
shutdown();
while(true); //sinkhole
}
return(mmps);
}