help on programming

I am new to this hope someone can help me understand if this is possible and guide me on what to do. I have code that someone has wrote and it works great but it loops over and over I want to put a button in so I can start the program with that and one it is done what it is supposed to do the program will stop until the button is pushed again. I have tried to do it myself but can’t seem to get it to work the program keeps running the loop.
I am not a programmer and don’t really know alot about it any help is greatly appreciated. I am including the code if you could a tell me how to put a button in this code to make it do what I want and to understand it better that is what I am looking for thank you again in advance

/*
=============================================================
=       Project: S curve
=      Language: Arduiino r12
=          Date: January 2008
=        Author: C. Eckert
=============================================================
*/

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 3500;    // steady state velocity (pulse/sec)
long Pt = 14400;    // total number of pulses for move (7000 steps per rev)

// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0
                   //                             so you don't calculate to long of a delay
long t12;           // time during constant velocity (microsec)

int count = 0;      // count the number of pulses


// Arduino pins
#define dirPin 3
#define stepPin 12

void setup() {
 Serial.begin(9600);
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 
 // Calculate the time at constant velocity
 t12 = (Pt/(Vm/1e6))-0.5*(ta+td);
 Serial.println(); Serial.println();

 Serial.println("Setup Done");
}
void loop()
{
 digitalWrite(dirPin, HIGH);  // Set the stepper direction

 // Decide which part of the velocity curve your at
 if (t<ta) {                                       // Acceleration
   //Serial.println ("Acceleration Curve");
   dly = (ta)/(2*(Vm/1e6)*t);
 }
 else if (t>=ta && t<(ta+t12)){                    // Constant velocity
   //Serial.println ("Constant Velocity");
   dly = 1/(2*(Vm/1e6));
 }
 else if (t>=(ta+t12) && t<(ta+t12+td)){          // Deceleration
   //Serial.println ("Deceleration Curve");
   dly = 1/(2*((Vm/1e6)-(Vm/(1e6*td))*(t-ta-t12)));
 }
 
 t = t+2*dly; // update the current time
 //Serial.print("dly: "); Serial.print (dly); Serial.println(" microsec");
 //Serial.print ("Current time: "); Serial.print(t); Serial.println(" microsec");
 
 // Move stepper one pulse using delay just calculated
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(dly);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(dly);
 count ++;
   
 // The move is finished
 if (t>(ta+t12+td)){
   Serial.println ("Move Complete");
   Serial.print ("Total steps indexed: "); Serial.println (count);
   

   t=td/9;
   
   delay (1000);
 }
 
}

I want to put a button in

You need permission or help? Permission granted! Help? http://arduino.cc/en/Tutorial/Button

but it loops over and over

That's what loop does.

start the program with that and one it is done what it is supposed to do the program will stop until the button is pushed again.

You need to detect when the switch transitions to pressed, and invoke the code when that happens, and only when that happens.

I have tried

Not in that code...

I am not a programmer and don't really know alot

Nothing to be ashamed of, unless you intend to stay that way.

If i’m getting you correctly so its very easy to put a push button (e-g on pin 10) in it, just add these few lines to your code…
like…

/*
=============================================================
=       Project: S curve
=      Language: Arduiino r12
=          Date: January 2008
=        Author: C. Eckert
=============================================================
*/

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 3500;    // steady state velocity (pulse/sec)
long Pt = 14400;    // total number of pulses for move (7000 steps per rev)

// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0
                   //                             so you don't calculate to long of a delay
long t12;           // time during constant velocity (microsec)

int count = 0;      // count the number of pulses


// Arduino pins
#define dirPin 3
#define stepPin 12
#define checkPin 10
void setup() {
 Serial.begin(9600);
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(checkPin, INPUT);
 // Calculate the time at constant velocity
 t12 = (Pt/(Vm/1e6))-0.5*(ta+td);
 Serial.println(); Serial.println();

 Serial.println("Setup Done");
}
void loop()
{
if(checkPin==HIGH)
 {
 digitalWrite(dirPin, HIGH);  // Set the stepper direction

 // Decide which part of the velocity curve your at
 if (t<ta) {                                       // Acceleration
   //Serial.println ("Acceleration Curve");
   dly = (ta)/(2*(Vm/1e6)*t);
 }
 else if (t>=ta && t<(ta+t12)){                    // Constant velocity
   //Serial.println ("Constant Velocity");
   dly = 1/(2*(Vm/1e6));
 }
 else if (t>=(ta+t12) && t<(ta+t12+td)){          // Deceleration
   //Serial.println ("Deceleration Curve");
   dly = 1/(2*((Vm/1e6)-(Vm/(1e6*td))*(t-ta-t12)));
 }
 
 t = t+2*dly; // update the current time
 //Serial.print("dly: "); Serial.print (dly); Serial.println(" microsec");
 //Serial.print ("Current time: "); Serial.print(t); Serial.println(" microsec");
 
 // Move stepper one pulse using delay just calculated
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(dly);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(dly);
 count ++;
   
 // The move is finished
 if (t>(ta+t12+td)){
   Serial.println ("Move Complete");
   Serial.print ("Total steps indexed: "); Serial.println (count);
   

   t=td/9;
   
   delay (1000);
  }
}
}

I have done the button tutorial but when I put it in the code it wouldn't wait for the button to be pushed the code would start whether you pushed the button or not and keep looping. So I am asking for help in understanding how to do it. I understand that the way the code is written the it will start an acceleration curve to get the stepper up to speed then remain at a constant velocity for determined amount of time then decelerate when it has reached the total number of steps it was suppose to move. I have tried doing it with easier code and it works with the button but I can only run the stepper really slow because it needs the acceleration curve in order for me to get it to move faster. I know it's not in that code but that is code I'm trying to do it with and I didnt save what I had did for the button but basically I defined the button did just what it says in the tutorial. No I want to learn more about this like I said this really new to me and have no clue really. I have done a program with a button before for another project where I had it turn on a lathe and rotate it in one direction for a minute then stop and change direction and rotate it again in the other direction and it would do this for an hour then stop and wait for the button to be pressed again . Thank you for being understanding I'm just trying to learn more

#define checkPin 10

if(checkPin==HIGH)

10 is not HIGH. Ever.

You missing a digitalRead() of checkpin?

wow thanks guys I will try that out and see if that works for me

hahaha.. Sorry i missed that :) yes before "if" condition you have to digitalRead(checkPin) and store it in a variable and then put that in "if" condition if that is HIGH so...

ok I just tried to do what you said and it's now waiting for the button be pushed but it is still looping constantly it doesn't wait for the button to be pushed again after it has completed its move once the button its pushed once then it runs for number of steps entered then stops and starts again how do I get it to stop and not keep looping. I need it to move a specified number of steps then stop and wait for the button to be pushed again before it starts to move again thanks again for all the help so far

sorry meant to put my code in so you can see what I have

/*
=============================================================
=       Project: S curve
=      Language: Arduiino r12
=          Date: January 2008
=        Author: C. Eckert
=============================================================
*/

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 3500;    // steady state velocity (pulse/sec)
long Pt = 14400;    // total number of pulses for move (7000 steps per rev)

// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0
                   //                             so you don't calculate to long of a delay
long t12;           // time during constant velocity (microsec)

int count = 0;      // count the number of pulses

int val =0;

int state =0;

// Arduino pins
#define dirPin 3
#define stepPin 12
#define checkPin 7

void setup() {
 Serial.begin(9600);
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(checkPin, INPUT);
 // Calculate the time at constant velocity
 t12 = (Pt/(Vm/1e6))-0.5*(ta+td);
 Serial.println(); Serial.println();

 Serial.println("Setup Done");
}
void loop()
{
  val=digitalRead(checkPin);
if(val==HIGH){
  state=1-state;
}
if (state==1){
 digitalWrite(dirPin, HIGH);  // Set the stepper direction

 // Decide which part of the velocity curve your at
   if (t<ta) {                                       // Acceleration
   //Serial.println ("Acceleration Curve");
   dly = (ta)/(2*(Vm/1e6)*t);
 }
 else if (t>=ta && t<(ta+t12)){                    // Constant velocity
   //Serial.println ("Constant Velocity");
   dly = 1/(2*(Vm/1e6));
 }
 else if (t>=(ta+t12) && t<(ta+t12+td)){          // Deceleration
   //Serial.println ("Deceleration Curve");
   dly = 1/(2*((Vm/1e6)-(Vm/(1e6*td))*(t-ta-t12)));
 }
 
 t = t+2*dly; // update the current time
 //Serial.print("dly: "); Serial.print (dly); Serial.println(" microsec");
 //Serial.print ("Current time: "); Serial.print(t); Serial.println(" microsec");
 
 // Move stepper one pulse using delay just calculated
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(dly);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(dly);
 count ++;
   
 // The move is finished
 if (t>(ta+t12+td)){
   Serial.println ("Move Complete");
   Serial.print ("Total steps indexed: "); Serial.println (count);
   

   t=td/9;
   
  
  }
  else{
    digitalWrite(stepPin,LOW);
    digitalWrite(dirPin,LOW);
  }
}
}

Put the { for each compound statement on a new line. Use Tools + Auto Format to fix the atrocious indentation.

You haven't enabled the internal pullup resistor, so you need an external one. You haven't told us how the switch is wired. So, the ball is back in your court.

If i’m not getting you wrong and if you are facing this problem that you push the button only once and during that, loop executes multiple times so its the solution for that, putting this code, loop will execute only once with one push…

/*
=============================================================
=       Project: S curve
=      Language: Arduiino r12
=          Date: January 2008
=        Author: C. Eckert
=============================================================
*/

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 3500;    // steady state velocity (pulse/sec)
long Pt = 14400;    // total number of pulses for move (7000 steps per rev)

// Other variables
long dly;           // stepper pulse delay (microsec)
long t = td/9;      // current time (microsec)  -  You need to seed the initial time with something > 0
                   //                             so you don't calculate to long of a delay
long t12;           // time during constant velocity (microsec)

int count = 0;      // count the number of pulses

int val =0;

//int state =0;
int previousValue = 0;

// Arduino pins
#define dirPin 3
#define stepPin 12
#define checkPin 7

void setup() {
 Serial.begin(9600);
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(checkPin, INPUT);
 // Calculate the time at constant velocity
 t12 = (Pt/(Vm/1e6))-0.5*(ta+td);
 Serial.println(); Serial.println();

 Serial.println("Setup Done");
}
void loop()
{
  val=digitalRead(checkPin);
if(previousValue != val)
if(val==HIGH)
{
 digitalWrite(dirPin, HIGH);  // Set the stepper direction

 // Decide which part of the velocity curve your at
   if (t<ta) {                                       // Acceleration
   //Serial.println ("Acceleration Curve");
   dly = (ta)/(2*(Vm/1e6)*t);
 }
 else if (t>=ta && t<(ta+t12)){                    // Constant velocity
   //Serial.println ("Constant Velocity");
   dly = 1/(2*(Vm/1e6));
 }
 else if (t>=(ta+t12) && t<(ta+t12+td)){          // Deceleration
   //Serial.println ("Deceleration Curve");
   dly = 1/(2*((Vm/1e6)-(Vm/(1e6*td))*(t-ta-t12)));
 }
 
 t = t+2*dly; // update the current time
 //Serial.print("dly: "); Serial.print (dly); Serial.println(" microsec");
 //Serial.print ("Current time: "); Serial.print(t); Serial.println(" microsec");
 
 // Move stepper one pulse using delay just calculated
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(dly);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(dly);
 count ++;
   
 // The move is finished
 if (t>(ta+t12+td)){
   Serial.println ("Move Complete");
   Serial.print ("Total steps indexed: "); Serial.println (count);
   

   t=td/9;
   
  
  }
  else{
    digitalWrite(stepPin,LOW);
    digitalWrite(dirPin,LOW);
  }
}
previousValue=val;
}

i think “state” variable was making a problem instead of solving the problem…

The switch is wired with a 10K pull down down resistor.

OK Saad I tried the latest code you put up and now it does nothing

oops… “nothing” :~
then switch back to previous program… :slight_smile: