Expected initializer before void

//void clockwise()
{
  for (int i = 0; i < 18; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void anticlockwise()
{
  for (int i = 18; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

If I remove // from void clockwise(), i get the error expected initializer before void?

but the void anticlockwise passes through.

Basically I am trying to make 2 subroutines that I can goto either by button press or by pins going high/low

Thanks

Head on over to http://snippets-r-us.com for help with your snippet.

The problem is NOT in the part you posted.

Interesting, but the error is probably in the part of the program that you did not show.

The code should be in code tags (as you did), and so should the error messages.

Is this supposed to be an array?

setOutput(i);

Perhaps this instead?

setOutput[i];

Thanks. I am trying to make 2 sections in the code that I can point to to make a motor run clockwise or anticlockwise.

At the moment I thought the existing code would go through both procedures, making the motor change direction as passing through, something as I say could be split later to give control direction of the motor either from pushbuttons or other pin control.

int motorPin1 = A0;    // Blue   - 28BYJ48 pin 1
int motorPin2 = A1;    // Pink   - 28BYJ48 pin 2
int motorPin3 = A2;    // Yellow - 28BYJ48 pin 3
int motorPin4 = A3;   // Orange - 28BYJ48 pin 4

int motorSpeed = 14000;
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}
void loop(){

//void clockwise()

{
  for (int i = 0; i < 18; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void anticlockwise()
{
  for (int i = 18; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

You can not define a function inside another function. One of your closing '}' is in the wrong place.

int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

The largest value in that array is 12. I'm pretty sure you need to use long, or long long, as the type to hold values that large.

byte motorPin1 = A0;    // Blue   - 28BYJ48 pin 1
byte motorPin2 = A1;    // Pink   - 28BYJ48 pin 2
byte motorPin3 = A2;    // Yellow - 28BYJ48 pin 3
byte motorPin4 = A3;   // Orange - 28BYJ48 pin 4

int motorSpeed = 14000; // >>> This needs to be static or volatile for the functions to see it?
byte lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}
void loop(){
// add code here that calls clockwise()
// and anticlockwise()
}

void clockwise(motorspeed) // need motorspeed passed in somehow?
{
  for (int i = 0; i < 18; i++)
  {
    setOutput(i);
    delayMicroseconds();
  }
}

void anticlockwise()
{
  for (int i = 18; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds();
  }
}

void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

I made some syntax changes for you, see if that helps.

Not sure what you're attempting here:

digitalWrite(motorPin1, bitRead(lookup[out], 0));

Normal syntax is

digitalWrite (pin#, HIGH or LOW);

So you've got a pin#, is

bitRead(lookup[#], 0)

supposed to return a 0 or 1 from the array with B01011 (etc.) in it for bit position 0 for this example?

Perhaps you want something more like this:

void loop() {
 // Put code here that determines which way to turn based on user inputs
 //     .
 //     .
 //     .
 if (turnDirection == turnClockwise) {
   clockwise(); 
 } else if ((turnDirection == turnAnticlockwise) {
   anticlockwise();
 }
}

void clockwise()
{
  for (int i = 0; i < 18; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void anticlockwise()
{
  for (int i = 18; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

Many thanks everybody for taking time to help.

I have been playing with gfvalvo’s code as it was a bit easier for me to follow.

I sorted out a few errors on way ie missing ). I put some variables in to test direction change however the motor won’t run anticlockwise (coil switching order looks wrong, even in clockwise) although clockwise runs.

//declare variables for the motor pins
int motorPin1 = A0;    // Blue   - 28BYJ48 pin 1
int motorPin2 = A1;    // Pink   - 28BYJ48 pin 2
int motorPin3 = A2;    // Yellow - 28BYJ48 pin 3
int motorPin4 = A3;   // Orange - 28BYJ48 pin 4

int motorSpeed = 7000; //variable to set stepper speed
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

// TEST VARIABLES - THESE ARE GIVEN VALUES JUST AS TEST
int turnDirection = 1;
int turnClockwise = 0;
int turnAnticlockwise = 1;

void setup() {
  //declare the motor pins as outputs
  Serial.begin(9600);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}

void loop() {
  // Put code here that determines which way to turn based on user inputs
  //     .
  //     .
  //     .

  if (turnDirection == turnClockwise) {
    clockwise();
  } else if ((turnDirection == turnAnticlockwise)) {
    anticlockwise();
  }
}

void clockwise()
{
  for (int i = 0; i < 18; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
    
  }
}

void anticlockwise()
{
  for (int i = 18; i >= 0; i--)

  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
    
  }
}
void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

Any ideas, thanks

Rather odd to be accessing 18 elements of an 8 element array.

Thanks Paul, however code taken from other sources in part, snd it does work when steered there?

I started the thread trying to sort out this section of code, remmed out /void clockwise.

If I alter the for (int loop to change direction, the motor coils are energised correctly and by choice will go in both directions perfectly. So I know my stepper control code is perfectly okay.

I just struggle to get these 2 functions working and individually selectable.

Just wish there was a step through in the editor to show how the code flows.

//void clockwise()
 {
 // for (int i = 0; i < 8; i++) // clockwise
  for (int i = 7; i >= 0; i--) // anticlockwise
    {
      setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}

void anticlockwise()
{
  for (int i = 7; i >= 0; i--)
  {
     setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}
void setOutput(int out)
{
    digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

ZOR2:
Thanks Paul, however code taken from other sources in part, snd it does work when steered there?

Only for some definitions of "work"

Thanks AWOL,

Paul, you were right, I don't know how things got changed to 18? Changed back to 8 and everything works fine. So a big thankyou to everyone who came in to help. Fantastic :slight_smile: :slight_smile:

ZOR2:
Thanks Paul, however code taken from other sources in part, snd it does work when steered there?

I have no idea what that means, but if you are happy running code with obvious flaws, go for it, Just do NOT bring it back here.

Thank you Paul for your comments.

What makes you feel the code is flawed when pulses to the stepper are all correct, no motion slipping, no abnormal currents.

If I want to come back with my code I do not see why you have the position to stop me when clearly all the other guys helped me sort it out. You came back at me on an earlier thread andanother member made remarks back to you.

You don't have to answer my posts. Thank you