# can someone help me with a school project

i want to have 3 options left mid and right im using the IF command but i cant get it to work my stepper is just spinning and when i press the button it gows the other way

here is the code for now

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(backpin,INPUT);
pinMode(leftpin,INPUT);
pinMode(rightpin,INPUT);
}
void loop() {{
if (valL == HIGH) // check if the input is HIGH
{
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
}
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600);

}

if (valR == HIGH) // check if the input is HIGH
{

digitalWrite(dirPin,LOW); // richting omdraaien

for(int x = 0; x < 400; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600); }}

The answer is very simple. I’ll give you a hint. The left if statement is different than the the right.

OK a second hint. Look here.

``````void loop() { {
if (valL == HIGH)          // check if the input is HIGH
{
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
}
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600);

}
``````

And a tip. Under Tools, there is an Auto Format command (CTRL+T). You could try that to see if the solution presents itself more clearly.

P.S. Also not my use of code tags to make the post more readable.

First, welcome to the forum.
Second, use code tags as noted by adwsystems and covered in the How to use this forum post at the top of this and every forum.
Third, Auto Format. (also as noted by adwsystems)

``````   // Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
``````

I am not an expert on stepper motors, but I am going to go out on a limb here and say this probably does not do what you expect it to do.
The first time you set the pin high, something probably happens. But does it happen 199 more times?
Not in my understanding. But if you see the motors turning, then I am probably wrong and you can ignore my point.

I bet a set of curly brackets making all 4 lines part of the for() block is what you intended.

thx for the CTRL T tip. but i still don't understand your other tips. when i run the program the steppermotor just spins contiues. and when i press the button it does go the other way.
but it spins on and on. i want it to go 400! so 2 revolutions and then stop.

basicly my program

press button left.(stepper gows 2 revolutions left and stops.

press button back(to the middle) 2 revolutions the other way than the previous one.

press button right ( gows 2 revolutions right and stops

im doing this on my own for 2 weeks now in my late hours. so i tought it was time to get help

im from the netherlands so sorry for my english. and thx for helping me

Compare this

``````if (valR == HIGH)         // check if the input is HIGH
{

digitalWrite(dirPin,LOW); // richting omdraaien

for(int x = 0; x < 400; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600); }
``````

to this

``````{
if (valL == HIGH)          // check if the input is HIGH
{
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
}
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++)
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600);

}
``````

Notice any difference?

the { marks??

Yep.

A. You have an extra set.
B. It keep turning left because the code to step the motor is outside the {} of the if statement.

In my left post above, the { in the first line and the } in the sixth line are extra. Even If that isn't the final answer, it will make left and right the same.

I have never run into an instance where there are two { back to back (ie., {{ ) so that is an immediate red flag.

Be sure to post your new code if you make any changes, so we can keep helping you move forward.

i think i don’t really understand the {} marks then they are loops?

i’ve changed it

*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(backpin, INPUT);
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
}
void loop() {{

if (valL == HIGH) // check if the input is HIGH

digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);

}
{
if (valR == HIGH) // check if the input is HIGH

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
}}

and how can i program the middle button? so it always gows back to the middle?

i’ve asked my teacher but he’s not into arduino

Too many (wrong) changes.

Please each brace { and } on its own line and the program will become clearer.

I mentioned there is no case to see {{ so

``````void loop() {{
``````

is wrong. Delete one and place the other on its own line.

At the end

``````   delayMicroseconds(600); }}
``````

is correct, but place each } on its own line.

You changed the Right but it was correct

``````valR = digitalRead(rightpin);  // read input value
if (valR == HIGH)         // check if the input is HIGH
{

digitalWrite(dirPin,LOW); // richting omdraaien

for(int x = 0; x < 400; x++)
{ // also missing
digitalWrite(stepPin,HIGH);
delayMicroseconds(600);
digitalWrite(stepPin,LOW);
delayMicroseconds(600);
} // also missing
}
``````

Left should be

`````` valL = digitalRead(leftpin);  // read input value
if (valL == HIGH)          // check if the input is HIGH
{
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
{ // also missing
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} // also missing
}
``````

I think you were also missing the braces for the for statements (I added them with the comment “also missing”

Don’t worry about the middle button until you have these two working properly.

hi im back had to go sleep yesterday.

i’ve changed it to this but at the end i had to do alot of }

*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(backpin, INPUT);
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
}
void loop() {
{

if (valL == HIGH) // check if the input is HIGH
{
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
}
}
{
if (valR == HIGH) // check if the input is HIGH
{

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
}
}
}
}
}

THX for helping

Try labelling your braces (which is easiest done if they are all on their own line) then you will find you are adding complexity to reading the program because you have extras.

void loop()
{ // loop start
{

if (valL == HIGH) // check if the input is HIGH
{ // if valL HIGH
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
{ // for loop to run motor left
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} //end for loop
} // end if left
{
if (valR == HIGH) // check if the input is HIGH
{ // if valR HIGH

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
{// for loop to run motor right
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} // end for
} // end if right
}
}
} // loop end

See the extra braces now (they would be the ones that aren’t labelled) and how hard it is to read because of them.

thx i think i starting to understand it ive changed it

the questions marks are mine. is it right what i do?

the code is wrong when i verify it cuz of the } at the end .

but i want to know if the one’s i added are right.

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(backpin, INPUT);
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
}
void loop()
{ // loop start
{ // start loop left ???

if (valL == HIGH) // check if the input is HIGH
{ // if valL HIGH
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
{ // for loop to run motor left
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} //end for loop
} // end if left
} // end loop left ???
{
if (valR == HIGH) // check if the input is HIGH
{ // if valR HIGH

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
{// for loop to run motor right
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} // end for
} // end if right
}
}
} // loop end

thx again

There were 4 extra, now there are 5. The two you marked plus the three unmarked.

Braces always correspond to an instruction that requires them

if ()
{
}

for ()
{
}

subroutine()
{
}

If you have {{ there is a problem.

If you have
{
}

without a function, then you have extra braces (which is what you have marked)

OP - USE CODE TAGS!
... and the structure may be clearer if you put the steps into a separate function, but atm, I think that is beyond OP.

Remember this is a school project. OP should be collaborating, or asking his teacher, although the help thus far has been benign.

*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(backpin, INPUT);
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
}
void loop()
{ // loop start

if (valL == HIGH) // check if the input is HIGH
{ // if valL HIGH
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 200; x++)
{ // for loop to run motor left
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} //end for loop
} // end if left

{
if (valR == HIGH) // check if the input is HIGH
{ // if valR HIGH

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
{// for loop to run motor right
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} // end for
} // end if right
}

} // loop end

so this is right?

yeaah i tried it the buttons work!!! nicee

now for the middle one i need someting like this.

if middle is HIGH

take the previous dirPIN status HIGH or LOW

reverse the status so LOW or HIGH and use that as next command for the dirPIN

and then the pulse program

am i right?

Getting there. You still have one pair of braces with no comment, thus they are extra.

Now add the code you think works for the middle button.

P.S. you code is getting long enough to become troublesome to read when posted directly into the message. There is a way to post a program to make it readable on the forum. Please goto How to use this forum and read #7

``````/

*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int leftpin = 8;
const int backpin = 9;
const int rightpin = 10;
int valL = 0;
int val = 0;
int valR = 0; // variable for reading the pin status
int LR = 0;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(backpin, INPUT);
pinMode(leftpin, INPUT);
pinMode(rightpin, INPUT);
}
void loop()
{ // loop start

if (valL == HIGH)          // check if the input is HIGH
{ // if valL HIGH
digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

// Makes 200 pulses for making one full cycle rotation
for (int x = 0; x < 400; x++)
{ // for loop to run motor left
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} //end for loop
} // end if left

if (valR == HIGH)         // check if the input is HIGH
{ // if valR HIGH

digitalWrite(dirPin, LOW); // richting omdraaien

for (int x = 0; x < 400; x++)
{// for loop to run motor right
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} // end for
} // end if right

if (val == HIGH)              // when HIGH do things below
{ //val HIGH
digitalWrite(dirPin,[LR,reverse]; // this one i don't understand

for (int x = 0; x < 400; x++)
{// for loop to run motor
digitalWrite(stepPin, HIGH);
delayMicroseconds(600);
digitalWrite(stepPin, LOW);
delayMicroseconds(600);
} //end loop to run motor
}

} // loop end
``````

this is what i think is good.

i hope you understand my comments after the //

thx.

Looking good, but does it do what you want?

no because i don’t know how to reverse the command it reads.

it does not verify.

and i don’t know how i find that information on internet.

btw it does work!! only needs to turn the other way so i need to reverse the HIGH to LOW or other way

PS: for the guy about that i need to ask my teacher! i can’t really i’m mechanical engineer and we don’t use this for now, they want to teach it to the next year of student’s.

Comments are valuable when they tell the reader something that they can't glean from the code itself. Think why, not what. Most of yours just echo the code, which makes them of little use. And then there's the need to ensure that they keep up with changes you have made. This one has got behind:

``````  // Sets the two pins as Outputs
``````