Go Down

Topic: Need help with custom Function... (Read 85 times) previous topic - next topic

This is the error that I get:
Code: [Select]
CODE_V4_-_motor_drivers.ino: In function 'void loop()':
CODE_V4_-_motor_drivers.ino:77: error: 'stepMotor' was not declared in this scope
CODE_V4_-_motor_drivers.ino:82: error: a function-definition is not allowed here before '{' token


This is the code I am having trouble with, when the button is pressed the customfunction is supposed to run the code for the stepper motor
 

Code: [Select]
twixstate = digitalRead(btntwix); // read the state of the twix button value 
  if (twixstate == HIGH) { // Check if the button is pressed (High)
    digitalWrite(M1enable, HIGH); // Enable Motor Driver 1
    stepMotor(2);
    digitalWrite(M1enable, LOW); // Disable Motor Drive 1
  }
   }
       
void stepMotor(int stepMotor){
 
   digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
 
  digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
}
  }


Im a total beginner at arduino so this may be a silly mistake but can someone point me in the correct direction

Thanks

jremington


michinyon

You probably have a problem with the number of } and { in your program.   These have to match.

Also,  if you need your loop( ) function to call some other function,   the function normally needs to be defined,  or at least declared,  before the loop function.


Sorry here is the full code

Code: [Select]
//Stepper motor wires:
int brown = 26;
int yellow = 24;
int black = 22;
int orange = 28;
const int btntwix = 5; // Constants won't change values - Twix Chocolate input button

int twixstate = 0;

//Motor Driver board enables:
int M1enable = 2;
int M2enable = 4;
int M3enable = 7;


void setup() {               
     pinMode(brown, OUTPUT);
     pinMode(yellow, OUTPUT);
     pinMode(black, OUTPUT);
     pinMode(orange, OUTPUT);
     pinMode(M1enable, OUTPUT);
     pinMode(M2enable, OUTPUT);
     pinMode(M3enable, OUTPUT);   
}

void loop() {
  twixstate = digitalRead(btntwix); // Read the state of the twix button value 
  if (twixstate == HIGH) { // Check if the button is pressed (High)
    digitalWrite(M1enable, HIGH);
    stepMotor(2); // Run the function
    digitalWrite(M1enable, LOW);
}

void stepMotor() {
 
  digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
 
  digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
}
}

Code: [Select]

sketch_feb21e.ino: In function 'void loop()':
sketch_feb21e:30: error: 'stepMotor' was not declared in this scope
sketch_feb21e:34: error: a function-definition is not allowed here before '{' token


and now this is the error that I receive, I dont want the function to return any values I just want it to run the code

Thanks 

wildbill

You have tried to declare your stepMotor function inside loop. There's a closing curly bracket at the end of your sketch that should be before the stepMotor function starts.

You've tried to call stepMotor with a parameter - 2. The function doesn't take any parameters - get rid of it.

BillHo

Try this out, is this what you want?

Code: [Select]
//Stepper motor wires:
int brown = 26;
int yellow = 24;
int black = 22;
int orange = 28;
const int btntwix = 5; // Constants won't change values - Twix Chocolate input button

int twixstate = 0;

//Motor Driver board enables:
int M1enable = 2;
int M2enable = 4;
int M3enable = 7;


void setup() {               
     pinMode(brown, OUTPUT);
     pinMode(yellow, OUTPUT);
     pinMode(black, OUTPUT);
     pinMode(orange, OUTPUT);
     pinMode(M1enable, OUTPUT);
     pinMode(M2enable, OUTPUT);
     pinMode(M3enable, OUTPUT);   
}

void stepMotor() {
 
  digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
 
  digitalWrite(black, LOW);
  digitalWrite(brown, HIGH);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, LOW);
  digitalWrite(yellow, HIGH);
  delay(100);
 
  digitalWrite(black, HIGH);
  digitalWrite(brown, LOW);
  digitalWrite(orange, HIGH);
  digitalWrite(yellow, LOW);
  delay(100);
}


void loop() {
  twixstate = digitalRead(btntwix); // Read the state of the twix button value 
  if (twixstate == HIGH) { // Check if the button is pressed (High)
    digitalWrite(M1enable, HIGH);
    stepMotor(); // Run the function
    digitalWrite(M1enable, LOW);
  }
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy