Hi Folks, Totally new to any programming and have been searching and experimenting for 3 days now to try to get a stepper motor to work with limit switches and a start switch.
I have had the motor running and reversing but since adding the switches to the mix I have not been able to upload the program. It gets to Void Loop() { and gives message.
Exit status 1
A function definition is not allowed here before '{' token
I have checked that all brackets are closed and have searched the error term to no avail.
I am assuming I have written something that the processor does not recognise somewhere above the loop function but am at a loss as what I need to change. Below is where I am at, I would really appreciate some help.
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 12; // the number of the pushbutton1 pin slideforward micro switch
const int buttonPin3 = 11; // the number of the pushbutton3 pin start motor push button
const int buttonPin2 = 8; // the number of the pushbutton2 pin slideback micro switch
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
// variables will change:
boolean buttonState = 0; // variable for reading the pushbutton status
boolean buttonState2 = 0; // variable for reading the pushbutton status
boolean buttonState3 = 0; // variable for reading the pushbutton status
int slidePosition = 2; // Tells progam what position the slide is in
/**
setup inputs for switches and outs for motor pins
serial begin to read the switches to test for errors
*/
void setup() {
// initialize the pin as an inputs:
pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
}
/**
this function turns motor foward
*/
void slideForward() {
// turn motor foward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/*turns the motor 160000 steps*/
for (int i = 0; i < 160000; i++)
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
/**
this function turns motor backwards
*/
void slideBackward() {
digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
/*Turns the motor fast 160000 steps*/
for (int i = 0; i < 160000; i++)
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
/**
Stop the motor form moving
*/
void stopSlideFromMoving() {
digitalWrite(smStepPin, LOW);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
slidePosition = 2;
Serial.println(F("Front button state is high slide is forward"));
stopSlideFromMoving();
} else if (buttonState2 == HIGH) {
slidePosition = 1;
stopSlideFromMoving();
}
if (buttonState3 == HIGH) {
if ( slidePosition == 1 ) {
slideBackward();
delay(100);
slidePosition = 2;
delay(100);
} else if (slidePosition == 2 ) {
delay(100);
slideForward();
delay(100);
slidePosition = 1;
}
}
}
void slideForward()
{
// turn motor forward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/*turns the motor 160000 steps*/
for (int i = 0; i < 160000; i++)
{
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
/**
* this function turns motor backwards
*/
void slideBackward()
{
etc, etc
Thanks for that, I looked and looked but missed that one.... I will try again, fingers crossed......
...minutes later, well the programme uploaded but the only thing it seems to do is to lock the motor.
Switches do nothing, I will check that they are connected correctly ( I know the motor is as it has been running). I may be back to ask if anyone can see what is wrong with my code.
Note how the code looks in post #2. It has been Auto formatted in the IDE and the slideBackward() is plainly not on the left margin where it should be, which is a big clue
Note how the code looks in post #2. It has been Auto formatted in the IDE and the slideBackward() is plainly not on the left margin
That is the problem with being new to this, I did not notice the difference, a newbie question then, once I have written code should I always finish with auto format before trying to upload?
once I have written code should I always finish with auto format before trying to upload?
Don't wait until you finish. Do it often
Other tips. The code block for a function starts with { and ends with }. The IDE will add the closing } to the editor window when you type the { once you press Return. If you place the mouse pointer next to the opening { of a function and click then the IDE will put a marker round the corresponding closing } which makes it easy to see the extent of the function. I also find that putting each { and } on its own line makes the code easier to read (Auto format can be customised to do that automatically) and putting a blank line between the end of one function and the nest is also helpful.
If you place the mouse pointer next to the opening { of a function and click then the IDE will put a marker round the corresponding closing } which makes it easy to see the extent of the function.
I had noticed that one and had used it to check open/closed brackets, what I missed was that the error was above the highlighted code line, I stupidly assumed that the error was at or below that point.... lessons learned.
I think customising auto format is a step too far at the moment I will just make sure I use it.
I have read that pin 13 can be awkward to use (don't know why) so have moved switch to pin 8.
I have uploaded the corrected code in my first post.
What the code is meant to do when initialised is with one of the micro-switches high:
Pressing the push button starts the motor, releasing micro-switch
Motor runs, until the other micro switch goes high then stops
Next touch of the push button sends the motor back the other way stopping once other micro-switch is high-
I decided that maybe the delays were too short on the button states so upped them by a factor of 10 to no avail.
There are no mid points on this slide, it stops at one end or the other.
I know that you meant well but by changing the code in the original post you have made nonsense of the observations made by MarkT and I.
As to your code, the first thing that I would do is to give the inputs sensible names. For instance you mention in your explanation of what the program should do that there is a pushbutton that should start the motor and microswitches that act as limit switches. Rather than having to read the comments to discover which input is which it would be preferable to give the inputs meaningful names so that their use is clear throughout the code.
I see that the pinMode()s set the pins as INPUT and from the code it seems that they are taken HIGH when pressed/activated. Do you have pulldown resistors in place to keep the inputs LOW at all other times or are they floating at an unknown voltage ?
You're missing curlies( { & } ) around your for loop statements.
void slideForward() {
// turn motor foward:
digitalWrite(smDirectionPin, HIGH); //Writes the direction to the EasyDriver DIR pin. (HIGH is clockwise).
/*turns the motor 160000 steps*/
for (int i = 0; i < 160000; i++)
{ // <- here
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
} //<- here
}
/**
this function turns motor backwards
*/
void slideBackward() {
digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
/*Turns the motor fast 160000 steps*/
for (int i = 0; i < 160000; i++)
{ //<- here
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
} //<- here
}
Still, jumping from standstill to 7000 + steps per second might miss many steps or just stall.