Hello i need help with my code

I am making a welding system with a Linear Rail Motion and I need the forward speed to be slower than the reverse speed. and aome limit swich the code I have does not do this to me . can any one help and fi this costs maney pls tell me how much it cost.

// defines pins numbers

const int stepPin = 5;

const int dirPin = 2;

const int enPin = 8;

const int LimitSwitch_LEFT_Pin  = 10;

const int LimitSwitch_RIGHT_Pin = 11;



void setup() {



  Serial.begin(9600);



  pinMode(LimitSwitch_LEFT_Pin , INPUT);

  pinMode(LimitSwitch_RIGHT_Pin , INPUT);



  // Sets the two pins as Outputs

  pinMode(stepPin,OUTPUT);

  pinMode(dirPin,OUTPUT);



  pinMode(enPin,OUTPUT);

  digitalWrite(enPin,LOW);



  // Set Dir to Home switch

  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction





}

void loop() {



    int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);

    int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);

   

    if( (leftSw  == HIGH && (digitalRead(dirPin) == HIGH)) ||

        (rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){

   

       motorStep(1);

    }
   
    else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
          digitalWrite(dirPin,LOW);
          delay(50);
    }
    else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
          digitalWrite(dirPin,HIGH);
          delay(50);
    }

   
 
}
void motorStep( int MAX){

   for(int x = 0; x < MAX; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(70);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(70);
      }
     
}

Hello
take a look here.
The usage of a FSM for your project is strongly recommended.

hello im using TB6560 stepper driver and nema 23 stepper amd i need slove problem with code pls canu help me

Just read your dirPin and set the delay time accordingly....

void motorStep( int MAX) {
  int delayTime;
  if( digitalRead(dirPin) == HIGH) {
    delayTime = 70;
  } else {
    delayTime = 140;
  }
  for (int x = 0; x < MAX; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(delayTime);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(delayTime);
  }
}

$1,000,000 USD please :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.