'Better' Logic for stepper motor - Didn't Work

/*Code to run a stepper motor

  • TRIED WITH “BETTER” (shortcut) LOGIC
  • And no compiling errors.
  • But this didn’t work when tested with LEDs*/

int dele=500;
int pin[4]={8,9,10,11};

void setup(){
for (int i=0;i<4;i++){
pinMode(pin*,OUTPUT);*

  • }*
    }
    void loop()
    {

  • for(int j=0;j<4;j++)*

  • {*

  • digitalWrite(pin[j],HIGH);*

  • for(int k=0;k<=3;k++)*

  • {*

  • while(k!=j)*

  • digitalWrite(pin[k],LOW);*

  • }*

  • delay(dele); *

  • }*

}
/*Same Code to run a stepper motor
- TRIED WITH straight-forward LOGIC
- And this did work well when tested with LEDs*/
int motorPin1 = 8;
int motorPin2 = 9;
int motorPin3 = 10;
int motorPin4 = 11;
int delayTime = 100;
void setup() {

  • pinMode(motorPin1, OUTPUT);*
  • pinMode(motorPin2, OUTPUT);*
  • pinMode(motorPin3, OUTPUT);*
  • pinMode(motorPin4, OUTPUT);*
    }
    void loop() {
  • digitalWrite(motorPin1, HIGH);*
  • digitalWrite(motorPin2, LOW);*
  • digitalWrite(motorPin3, LOW);*
  • digitalWrite(motorPin4, LOW);*
  • delay(delayTime);*
  • digitalWrite(motorPin1, LOW);*
  • digitalWrite(motorPin2, HIGH);*
  • digitalWrite(motorPin3, LOW);*
  • digitalWrite(motorPin4, LOW);*
  • delay(delayTime);*
  • digitalWrite(motorPin1, LOW);*
  • digitalWrite(motorPin2, LOW);*
  • digitalWrite(motorPin3, HIGH);*
  • digitalWrite(motorPin4, LOW);*
  • delay(delayTime);*
  • digitalWrite(motorPin1, LOW);*
  • digitalWrite(motorPin2, LOW);*
  • digitalWrite(motorPin3, LOW);*
  • digitalWrite(motorPin4, HIGH);*
  • delay(delayTime);*
    }
    /* Where could be my mistake in the previous code?
    Please help.*/

/* Where could be my mistake in the previous code?

All those italics in your code?
Read the stickies at the top of the forum and post your code CORRECTLY.

   while(k!=j)
        digitalWrite(pin[k],LOW);

Why are you using while? The values of k and j do not change because digitalWrite() is called, so it k and j are not equal, this code will simply keep setting the kth pin LOW until hell freezes over.

Welcome to the Forum. Please read the two posts

How to use this forum - please read.
and
Read this before posting a programming question …

at the top of this Forum on guidelines for posting here, especially the use of code tags which make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.

Wrong syntax here ?

 pinMode(pin,OUTPUT);

You need to tell us what stepper motor driver you are using.
You need to tell us what power supply (volts and amps) you are using for your stepper motor.
You need to post a link to the datasheet for the stepper motor.

...R
Stepper Motor Basics