Switch case array for stepper motor. Full steps questions

Hi, I am new to writing codes.
I tried to do this code without importing library and it wont compile. Especially stuck on case default.
Any help is to be welcome.

const int buttonPin =8; //Forward / Reverse switch
int buttonState=0; //Pin goes to zero
int Pin0 = 3;
int Pin1 = 4;
int Pin2 = 5;
int Pin3 = 6;
int _step =0 ; //sets initial step to zero
boolean dir = true; // sets clockwise ++ or anticlockwise -- (DIR)
const byte stepperPin[4]={3,4,5,6};
void setup()
{
pinMode(stepperPin[4], OUTPUT);

pinMode(buttonPin, INPUT);
}
void loop() {
buttonState= digitalRead(buttonPin); // read the button pin == is condition

const byte stepSequence [4][4];

switch(_step){
case 0:
digitalWrite(stepperPin[], HIGH, HIGH, LOW, LOW);
break;
case 1:
digitalWrite(stepperPin[], LOW,HIGH,HIGH,LOW);
break;
case 2:
digitalWrite(stepperPin[], LOW,LOW,HIGH,HIGH);
break;
case 3:
digitalWrite(stepperPin[], HIGH,LOW,LOW,HIGH);
break;
default:
digitalWrite(stepperPin[], LOW,LOW,LOW,LOW);
break;
}

if(dir){
_step--;
}else{
_step++;
}
if(_step>3){
_step=0;
}
if(_step<0){
_step=3;
}
delay(5);
}

what PIN number do you think tou touch with that code?

     digitalWrite([color=red]stepperPin[][/color], HIGH, HIGH, LOW, LOW);

have you read the digitalWrite() documentation?


Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

const int buttonPin = 8; //Forward / Reverse switch
int buttonState = 0; //Pin goes to zero
int Pin0 = 3;
int Pin1 = 4;
int Pin2 = 5;
int Pin3 = 6;
int _step = 0 ; //sets initial step to zero
boolean dir = true; // sets clockwise ++ or anticlockwise -- (DIR)
const byte stepperPin[4] = {3, 4, 5, 6};
void setup() {
for (int i = 1, i <= 4, i++) {
pinMode(stepperPin*, OUTPUT);*

  • }*
  • pinMode(buttonPin, INPUT);*
    }
    void loop() {
  • buttonState = digitalRead(buttonPin); // read the button pin == is condition*
  • switch (_step) {*
  • case 0:*
    _ digitalWrite(stepperPin*, HIGH, HIGH, LOW, LOW);_
    _
    break;_
    _
    case 1:_
    _ digitalWrite(stepperPin, LOW, HIGH, HIGH, LOW);
    break;
    case 2:
    digitalWrite(stepperPin, LOW, LOW, HIGH, HIGH);
    break;
    case 3:
    digitalWrite(stepperPin, HIGH, LOW, LOW, HIGH);
    break;
    default:
    digitalWrite(stepperPin, LOW, LOW, LOW, LOW);
    break;
    }
    if (dir) {_

    step--;*
    * } else {_
    step++;*
    * }_
    if (step > 3) {
    step = 0;*
    * }*

    * if (step < 0) {
    step = 3;*
    * }*

    * delay(5);*
    }
    for (int i = 1, i <= 4, i++) 
    {
        pinMode(stepperPin, OUTPUT);
    }

First, kudos for using an array to store pin numbers this way.

You've declared stepperPin as an array so in order to access its members as an array, you need to put '[', an index and ']' after the name; the index value ranges from 0 to the size of the array minus one. So for a 4-element array the valid indexes are 0 to 3.

On that last point, your for loop runs from 1 to 4; valid indices for your array would be 0 to 3. So the init here would look like:

    for( int i = 0; i < 3; i++ ) 
    {
        pinMode( stepperPin[i], OUTPUT );
    }

Notice the use of ';' separating items in the for( ... ) statement.

Your use of digitalWrite is incorrect. This function sets a single digital pin to a HIGH or LOW level. For example:

digitalWrite( stepperPin[0], LOW );   //set pin 3 low
digitalWrite( stepperPin[0], HIGH );   //set pin HIGH

Perhaps what you want is something like:

    switch (_step) 
    {
        case 0:
            digitalWrite(stepperPin[0], HIGH );
            digitalWrite(stepperPin[1], HIGH );
            digitalWrite(stepperPin[2], LOW );
            digitalWrite(stepperPin[3], LOW );
        break;
.
.
.

You can repeat this for the other cases. If you fix these, the code should compile. There may be other problems but you should see some measure of success.

@cubico - what don’t you understand in using code tags. Read forum rules please, your 2nd post is formatted as crap just like the first one. I’m out of here.

Kudos to @blackfin for your patience with a member who is willingly ignoring simple basic politeness rules of a community.

@BlackFin You are a Genius. Codes has now compiled without problem. I will see if it will run the Stepper motor . Thanks a Million.
Forgive me if I do some things wrong. I am still trying to figure out how to make a post here.
I am a newbie to coding as well.

This is the result

const int buttonPin = 8; //Forward / Reverse switch
int buttonState = 0; //Pin goes to zero
int Pin0 = 3;
int Pin1 = 4;
int Pin2 = 5;
int Pin3 = 6;
int _step = 0 ; //sets initial step to zero
boolean dir = true; // sets clockwise ++ or anticlockwise -- (DIR)
const byte stepperPin[4] = {3, 4, 5, 6};
void setup() {
for (int i = 0; i < 3; i++) {
pinMode(stepperPin*, OUTPUT);*

  • }*
  • pinMode(buttonPin, INPUT);*
    }
    void loop() {
  • buttonState = digitalRead(buttonPin); // read the button pin == is condition*
  • switch (_step) {*
  • case 0:*
  • digitalWrite(stepperPin[0], HIGH);*
  • digitalWrite(stepperPin[1], HIGH);*
  • digitalWrite(stepperPin[2], LOW);*
  • digitalWrite(stepperPin[3], LOW);*
  • break;*
  • case 1:*
  • digitalWrite(stepperPin[0], LOW);*
  • digitalWrite(stepperPin[1], HIGH);*
  • digitalWrite(stepperPin[2], HIGH);*
  • digitalWrite(stepperPin[3], LOW);*
  • break;*
  • case 2:*
  • digitalWrite(stepperPin[0], LOW);*
  • digitalWrite(stepperPin[1], LOW);*
  • digitalWrite(stepperPin[2], HIGH);*
  • digitalWrite(stepperPin[3], HIGH);*
  • break;*
  • case 3:*
  • digitalWrite(stepperPin[0], HIGH);*
  • digitalWrite(stepperPin[1], LOW);*
  • digitalWrite(stepperPin[2], LOW);*
  • digitalWrite(stepperPin[3], HIGH);*
  • break;*
  • default:*
  • digitalWrite(stepperPin[0], LOW);*
  • digitalWrite(stepperPin[1], LOW);*
  • digitalWrite(stepperPin[2], LOW);*
  • digitalWrite(stepperPin[3], LOW);*
  • break;*
  • }*
  • if (dir) {*
  • _step--;*
  • } else {*
  • _step++;*
  • }*
  • if (_step > 3) {*
  • _step = 0;*
  • }*
  • if (_step < 0) {*
  • _step = 3;*
  • }*
  • delay(5);*
    }

I wanted to make the codes more compact. By sweeping through the stepperPin[] and doing the digitalWrite in a single line.
However, It seems that there has to be one digitalWrite for each pin. Am I correct?

Thank you!!

Forgive me if I do some things wrong. I am still trying to figure out how to make a post here

why don't you start by reading the forum rules ? they are pinned at the top of the forum...

Can't you see your code is all garbled?