hello helpful people this is my third attempt at making my steppers do what i need them to do.now i know i must make my own controller.because thats the way it is, and ps3 controllers are to tricky for me. I just want my steppers to move 200 steps one way and 200 steps the other way when either right or left button is pushed. im using momentary pushbuttons from radio shack and a 2 g251x gecko drivers. the pushbuttons are the never off style so the stepper will stay at 200 steps when it is pushed and go back to zero when the button is released. i tried to combine the 2 most simple codes i could find that i could get working. one moves a stepper 200 steps forward and back the other makes an led light shine nice and bright when a button is pushed and goes cold and dark when i let it go. when i ran this code it gave me an error of expected ' } ' before 'else' and expected ' } ' at end of input . my simple code looks like this #define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
else{
// Step backward 200 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(speedDelay);
}
i am trying to learn this stuff, but i am lazy and would love to have someone else write some code for me. i will be happy with help for making this simple one work though. thanks everyone
One does not just sprinkle { and } wherever one feels like it. Some are required. Some are optional. The number of each used must be identical. You have more { than }.
i appreciate your help and patience, i see your helping others on here. i found the extra } and now it says expected initializer before 'buttonstate' and expected unqualified-id before 'if'
i looked at the all the curly braces and found one that had no end so i removed it. when i hit the verify button there is error on line 20 expected initializer before buttonstate and another error unqualified id before if on line 24 this is what the code looks like now
#define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
{
// Step backward 200 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
i am looking at this thing and i just cant figure out how to make the code happy
#define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
{
// Step backward 200 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
im sure its obvious to code people what im doing wrong but i just dont see it
im sure its obvious to code people what im doing wrong but i just dont see it
Is your shift key broken? If not, I think that you will appear to be more than 10 years old if you actually use it.
If you put each { on a new line, and properly indent code between the { and the }, I think that you would see exactly what the problem is.
Your loop() function looks like this:
void loop()
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
}
Nothing that follows this is inside a function, so the compiler rejects it.
What you need to understand about the { and } is that they are delimiters. They mark the start and end of blocks. They are NOT optional in some cases, like after a function declaration.
They are optional in some cases, like after an if statement if there is only one statement to execute. Though optional, they are recommended here.
They can be used anywhere to define a block, but, frankly blocks that do not follow an if, for, or while statement just look stupid. The ONLY exception is after a case statement, where a local variable is needed.
getting closer now i get an error on line 38 that says expected ' } ' at end of input. line 38 is the last one. the new code now looks like this
#define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
{
// Step backward 200 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<200; i++)
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
of course when this code compiles my stepper probably wont move when i push the button. at least thats what im expecting
hmm i just removed most of the curly braces and it compiles. and i did that while entertaining my 2 yr old after i got off of a long day of work.it can be tuff to look at lines and watch a child. the new code looks like this #define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++)
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
ok i put this down for a couple of days, looked at it and saw missing things. I replaced some "{ and }" and put a delay in and it compiles. I dont know if it is going to work when i wire the push button to the driver and stepper. this is a learning process for me and still dont know the proper terms for anything, but i will get there. here is the updated code.
#define directionPin 8 #define stepPin 9 #define speedDelay 1 // change this to change the speed, the lower it is the faster it will go
const int buttonPin = 2;
int buttonState = 0;
void setup() {
// Initialize the Serial port:
Serial.begin(9600);
// set up the driver pins:
pinMode(stepPin, OUTPUT);
pinMode(directionPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
// Step forward 200 steps:
Serial.println("Forward");
digitalWrite(directionPin, HIGH);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(speedDelay);
}
delay(500);
// Step backward 200 steps:
digitalWrite(directionPin, LOW);
for (int i=0; i<200; i++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}
}
I replaced some "{ and }" and put a delay in and it compiles.
I really think that you need to read ALL of the comments.
There is no reason for
your code to be wandering
all over the page like that.
Nail the stuff down by putting each {
one a new line WHERE IT F**KING BELONGS
and use Tools + Auto Format.
There is no excuse for you STILL failing to post code correctly. Read the damned sticky at the top of the forum and do it right.
It will be orders of magnitude easier to wire the switch(es) if you use the internal pullup resistors. One leg of the switch goes to ground. The other leg goes to a digital pin.
I'll leave it as homework for you to find BOTH ways of using the internal pullup resistors.
Finally, there is a library for use with stepper motors. stepper.step(200) and stepper.step(-200) are much easier than what you are doing.
im going to look into the easier way of using the internal pull up for the button i think i found the code. but the stepper library wants to use pin 8 thru 12 to work the stepper and my driver needs a step pin and a direction pin. the code i am currently using for the stepper uses the step and direction pin.