1

I am trying to call a function that will turn a stepper motor a number of steps depending on the value submitted through the serial console. For an example, if I submit 50, the motor should turn 50 steps clockwise (and counter clockwise for a negative value).

#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 8, 9, 10, 11);

int serial_input = Serial.read();

void setup() {
Serial.begin(9600) ;
stepper.setSpeed(30);
}

void loop() {
}

void turnMotor(serial_input){
  stepper.step ( serial_input ) ;
}  

And here are the errors I get:

Motor4.ino:12:28: error: variable or field ‘turnMotor’ declared void
Motor4.ino:12:16: error: ‘serial_input’ was not declared in this scope
Motor4.ino:117:28: error: variable or field ‘turnMotor’ declared void

Thank you

  • you can't call Serial.read() outside function – Juraj Jan 27 '19 at 6:43
  • I see my error as you explained – Danny Jan 29 '19 at 6:39
0

As Juraj already explained, you cannot use Serial.read() outside a function.

What you probably want is to call it within the loop, or a function called from loop (via other functions). Than you can call also turnMotor from Loop or a function called from loop. The value (int serial_input) probably doesn't have to be global anymore, but that depends on the time where you assign the value and where you call turnMotor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.