Hello, i am a total newb. So apologies if i am not listing all the info needed.
I got a project of running 2 stepper motors with a Mega2560
One is a rotating platform and the other is a screw drive.
I was told that the program i was given worked. It doesnt
It is supposed to run the screw drive to a home switch to set it to a known position at every restart.
The turntable is supposed to rotate to a proximity sensor to set known position.
Once i got all the connections made the screw drive moves 2.5" and stops. Even if i move it closer to the home switch nothing happens.
The turn table just twitches and then nothing...
Ive been reading as much as i can about programming. Not going to lie, its not my thing. I think ill eventually get it, but right now what i see in this program doesnt compare to what i read.
I have connections made to pins 2,3, 8,9,10 & 12 for controls and then to 5v and GND
For the Turn Table
Pin 2 is DIR-
Pin 3 is STP-
5v to both DIR+ & STP+
Pin 12 is to the Prox Sensor
For the Screw Drive
Pin 8 is for STP-
Pin 9 is for DIR-
5v out to both STP+ & DIR+
Pin 10 is to the Home Switch
When i read the code i can only see that pin 10 is assigned for sure.
I am not sure where all the other pins are being defined. Some of the code on top appears to match my pin ID, but then other code seems to be doing something different.
example:
int ttsteppin = 3;
int ttdirectionpin = 2;
and then
int cr = 13;
int buttonright = 6;
int buttonleft =5;
It looks like the proximity sensor code is disabled. I think that is because it was written before it was installed.
Any help is appreciated.
Thanks!
int steppin = 8;
int directionpin = 9;
const int buttonPin = 10;
int Distance = 0;
int Speed = 1;
long NumSteps =(0);
int i=2;
int j=2;
long jogsteps = 200000;
long systemsteps = 787500;
long homesteps = 9000;
int buttonState = 0;
int buttonStateright = 3;
int buttonStateleft = 3;
long setmovesteps = 275000;
int done = 0;
//int relayPin = 12;
int relayState = 0;
int cr = 13;
int buttonright = 6;
int buttonleft =5;
int ttsteppin = 3;
int ttdirectionpin = 2;
long ttsystemsteps = 12500;
long rightsystemsteps = -1*systemsteps;
int ttSpeed = 80;
int ttbutton = 12;
int ttbuttonState = 3;
int prox = 13;
int proxState = 3;
int proxdone = 0;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode (steppin, OUTPUT);
pinMode (directionpin, OUTPUT);
digitalWrite (steppin, LOW);
pinMode (buttonPin, INPUT_PULLUP);
//pinMode (relayPin, INPUT_PULLUP);
pinMode(buttonright,INPUT_PULLUP);
pinMode(buttonleft, INPUT_PULLUP);
pinMode (ttsteppin, OUTPUT);
pinMode (ttdirectionpin, OUTPUT);
digitalWrite (ttsteppin, LOW);
pinMode (ttbutton, INPUT_PULLUP);
pinMode (prox, INPUT_PULLUP);
}
void ttleft (){
for( long NumSteps=0; NumSteps <= ttsystemsteps; NumSteps = NumSteps + 1){
digitalWrite (ttdirectionpin, LOW);
digitalWrite (ttsteppin,HIGH);
delayMicroseconds (ttSpeed);
digitalWrite(ttsteppin, LOW);
delayMicroseconds (ttSpeed);
}}
void ttright (){
for( long NumSteps=0; NumSteps <= ttsystemsteps; NumSteps = NumSteps + 1){
digitalWrite (ttdirectionpin, HIGH);
digitalWrite (ttsteppin,HIGH);
delayMicroseconds (ttSpeed);
digitalWrite(ttsteppin, LOW);
delayMicroseconds (ttSpeed);
}}
void left (){
for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){
digitalWrite (directionpin, LOW);
digitalWrite (8,HIGH);
delayMicroseconds (Speed);
digitalWrite(8, LOW);
delayMicroseconds (Speed);
}
}
void right (){
for( long NumSteps=0; NumSteps <= systemsteps; NumSteps = NumSteps + 1){
digitalWrite (directionpin, HIGH);
digitalWrite (8,HIGH);
delayMicroseconds (Speed);
digitalWrite(8, LOW);
delayMicroseconds (Speed);
}
}
void movehome (){
buttonState = digitalRead (buttonPin);
while(buttonState != 0)
{
digitalWrite (directionpin, HIGH);
digitalWrite (8,HIGH);
delayMicroseconds (Speed);
digitalWrite(8, LOW);
delayMicroseconds (Speed);
buttonState = digitalRead (buttonPin);
}
done = 1;
}
void tthome (){
proxState = digitalRead (prox);
while(proxState != 1)
{
digitalWrite (ttdirectionpin, HIGH);
digitalWrite (ttsteppin,HIGH);
delayMicroseconds (ttSpeed);
digitalWrite(ttsteppin, LOW);
delayMicroseconds (ttSpeed);
proxState = digitalRead (prox);
}
proxdone=1;
}
void setmove (){
for( long NumSteps=0; NumSteps < setmovesteps; NumSteps = NumSteps + 1){
digitalWrite (directionpin, LOW);
digitalWrite (8,HIGH);
delayMicroseconds (Speed);
digitalWrite(8, LOW);
delayMicroseconds (Speed);
buttonState = digitalRead (buttonPin);
}
done = 2;
}
void loop() {
proxState = digitalRead (prox);
Serial.println(proxState);
if (proxdone ==0){
tthome();
}
buttonState = digitalRead (buttonPin);
// relayState = digitalRead (relayPin);
if (buttonState == 1 && done == 0){
movehome();
}else if (buttonState == 0 && done == 1){
setmove();
}
//Serial.println(digitalRead(button));
if (Serial.available() >= 0) {
buttonStateleft = digitalRead (buttonleft);
ttbuttonState = digitalRead(ttbutton);
Serial.println(ttbuttonState);
// Serial.println(buttonStateleft);
if (buttonStateleft==0 && i == 2){
i = 1;
left();
buttonStateleft = digitalRead (buttonleft);
ttbuttonState = digitalRead(ttbutton);
}else if (buttonStateleft ==0 && i == 1){
i = 2;
right();
buttonStateleft = digitalRead (buttonleft);
ttbuttonState = digitalRead(ttbutton);
}else if (ttbuttonState ==0 && j==2){
ttleft();
j=1;
ttbuttonState = digitalRead(ttbutton);
}else if(ttbuttonState==0 && j==1){
ttright();
j=2;
ttbuttonState = digitalRead(ttbutton);
}
//Jog to the Left
// }else if ( letter == '4'){
// for( long NumSteps=0; NumSteps <= jogsteps; NumSteps = NumSteps + 1){
// digitalWrite (directionpin, LOW);
//digitalWrite (8,HIGH);
//delayMicroseconds (Speed);
//digitalWrite(8, LOW);
//delayMicroseconds (Speed);
// }
//Jog to the Right
// } else if ( letter == '5'){
//for( long NumSteps=0; NumSteps <= jogsteps; NumSteps = NumSteps + 1){
//digitalWrite (directionpin, HIGH);
//digitalWrite (8,HIGH);
//delayMicroseconds (Speed);
//digitalWrite(8, LOW);
//delayMicroseconds (Speed);
// }
//}
}//void Serial bracket
}
//}//void loop bracket
StepperControlFinal.ino (5.16 KB)