help with programming

I am trying to write a program and I don't know if what I want to do is possible. What I want to do is I have to make a stepper move a certain amount of steps but I need to vary that amount of steps. My arduino will stand alone and not have a computer attached to where I could just change it in the program. So I want to know is it possible to assign a number of pulses to a pin when its high and when it is low the number of pulses will be different. I have been trying to do it by doing a digital read and assigning 4400 pulses to the high and 700 pulses to the low but it just moves the 700 pulses whether the pin is high or low I don't have my code with me to post it now. I would greatly appreciate if anyone can give me hope that this will work or know of another way that I can achieve the same thing any help is greatly appreciated. Thank you

Sorry, until we see your code, we'd just be guessing.

Yes, if I understand what you are trying to do it will work. The arduino can operate without being attached to a computer, but it must have a power supply. The arduino can detect if a digital input is high or low and, based upon that, can make a stepper motor take a differing number of steps.

To get fuller answers you will need to provide more information on what you are trying to do and post your code, but what you have said so far sounds practical.

ok so what I am attempting to do is to make a stepper motor pull fiber off a spool and I need two different lengths. I have tried a lot of things I thought would work but nothing seems to work for me. It will always pick one and stay with it whether the pin is high or low any help is appreciated
I am new to programming so you have to explain things to me like Im a kid. I know the code is complex I didn’t write it I found it here on the forum.
I liked it because it would allow me to control the speed and the number of pulses I wanted it to move. I was trying to introduce a button to start the code and it took me while to get that to work but I finally did this I just can’t seem to get to recognize when the switch is turned on or off. I am using an on/off switch cause I only need two different lengths I figured I could assign one value of steps to a pin when it was high and another value when it was low. Like Thank you for taking the time to help me out it is really appreciated.

// Givens
long ta = 3e6;     // acceleration time (microsec)
long td = 3e6;     // decelleration time (microsec)
long Vm = 1000;    // steady state velocity (pulse/sec)


// Other variables
long Pt =0;    // total number of pulses for move (7000 steps per rev)
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;         // val will be used to store the state of the input pin
int old_val=0;     // this variable stores the previous value of "val"
int state=0;       // 0 =off 1=on
int val2=0;
// Arduino pins
#define dirPin 3
#define stepPin 12
#define checkPin 7
#define pulsePin 5

void setup() {
  Serial.begin(9600);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(checkPin, INPUT);
  pinMode(pulsePin,INPUT);
  Serial.println(); 
  Serial.println();

  Serial.println("Setup Done");
}
void loop()
{
   val2=digitalRead(pulsePin);// read input and store it 
    if (val2==HIGH){
    long Pt=14400;
    
    // Calculate the time at constant velocity
    t12 = (Pt/(Vm/1e6))-0.5*(ta+td);


  val=digitalRead(checkPin); //read input val and store it
  if((val==HIGH)&&(old_val==LOW)){ //check if there is a transition
    state=1-state;
    delay(10);
  }
  old_val=val;  // val is now old, let's store it
  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);
      val==LOW;
      old_val=val;
      state=0;
      t = td/9;


      delay (1000);
    }
  }
    }
    
    else{
      if(val2==LOW){
      // Calculate the time at constant velocity
      t12 = (Pt/(Vm/1e6))-0.5*(ta+td);

      val=digitalRead(checkPin); //read input val and store it
      if((val==HIGH)&&(old_val==LOW)){ //check if there is a transition
        state=1-state;
        delay(10);
      }
      old_val=val;  // val is now old, let's store it
      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);
          val==LOW;
          old_val=val;
          state=0;
          t = td/9;


          delay (1000);

        }
      }
    }
  }
}

Your code looks good, and you've got a useful set of debug prints - do they tell you (and us) anything?

Hi. Complete newbie, but what does val==LOW; do?

I thought == was a compare.

Sorry if misunderstanding.

Carl

doing a digital read and assigning 4400 pulses to the high and 700 pulses to the low but it just moves the 700 pulses

How have you got your input switch wired up? Do you have a pull up or pull down resistor? See:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

I have my switch wired up with a pull down resistor. the debug prints were in the original code I have used them they tell when the setup is done and how many steps were completed

oh sorry I just realized that the code is missing a line in the else part after if(val2==LOW){ long Pt= 7000 should be there I copied it in the middle of me trying to figure out what going on

Have you got a response to CarlJP's question?

sorry carljp yeas the == is a comparison operator and the two == means equal to so (val==LOW) means val is equal to LOW

Yes but you have code like this:-

Serial.println (count);
          val==LOW;
          old_val=val;

So what is it doing?

it is reseting the val to low and the old_val to low cause that was the only way I could get the code to work otherwise when you pushed the button it would just keep on looping which isn't what I wanted it to do. I wanted it to start when I pushed the button move the specified steps then stop and wait for the button to be pushed again

Hi. Thanks for the explanation but I'm missing something here. Sorry!

// The move is finished if (t>(ta+t12+td)){ Serial.println ("Move Complete"); Serial.print ("Total steps indexed: "); Serial.println (count); val==LOW; old_val=val; state=0; t = td/9;

delay (1000); }

I still don't see what val==LOW; is doing in the above fragment. If it is testing for equality how is the result actioned? I would understand it if it was the test within an if statement, but this just seems an isolated statement. Sorry for asking basic question with what I'm sure has an obvious answer.

Carl

No, it is testing a relationship, and discarding the result. Normally, this is simply a waste of time, unless the compiler just decides to optimize it out.

Thanks for the explanation but I’m missing something here. Sorry!

CarlJP it is newbie101 that is missing something, he is missing his mistake, well done for spotting it.

Thanks AWOL (and Grumpy_Mike). Just to finally clarify. The statement is valid and will compile, but in this case actually does nothing productive, whereas val=LOW; would be valid and would do something. Is that right? Thanks again.

That's correct. You were absolutely right in picking that up.

wow thanks guys I cant believe I missed that I feel retarded. you dont know how may times I looked at the code trying to find something wrong unfortunately i will be a way all weekend and will not have time to try it out again and see how it works when I change it thanks again like said I am new to this so you have to talk to me like I know nothing I know very little and like I said all the help is greatly appreciated