If Statement problem

I am new to coding, and could use some help. The problem is the if statement, and I just don't get whats wrong. Any help would be appreciated.

// defines pins
#define dirPin 10
#define stepPin 11
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}

void loop() {
  if (int i = 0; i <= 5; i++){
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 400 pulses for making one full cycle rotation
  for(int x = 0; x < 1600; x++) {
    digitalWrite(stepPin,HIGH);
    // by changing the time delay between the steps below
    // we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
    delayMicroseconds(100);   
    digitalWrite(stepPin,LOW);
    // Do not mess with this delay as it will throw things off. 
    delayMicroseconds(200); 
  }
 exit(0);
  }

}

It sure is. That looks a lot more like a for statement.

for (int i = 0; i <= 5; i++){

Review basic statements and syntax.

a7

1 Like
  • Where did you find this syntax ? :roll_eyes:

I changed the if to a for and it solves the syntax problem, but after running a test the code does not work as I wanted it too. I wanted to make code inside repeat 5 times and it only does it once.

I can't be sure it's you problem, but what do you hope this statement will do?

exit(0);

Follow the code with your finger…

a7

yep that's it! I needed to move the exit down one bracket. - Thanks All!

Brace.

Use the IDE Autoformat tool on your code: it can make some errors like this and others easier to spot.

a7

1 Like

Do you know that if you click on an opening brace (not a bracket) the the matching brace, if there is one will be shown with a thin box surround it?

You need to remove it. Before you moved it it was causing a problem. Now it's doing absolutely nothing.

I've never used exit(0) in 10 years of writing Arduino code!

               void setup()
Opening brace: {
                  //code here
Closing brace: }

If @cyberman42 wants the code to run once, perhaps every time he hits reset, that statement just makes it stop when it has.

Of course usually stuff to be done once gets put in setup(), so @cyberman42 could move all those lines in loop() on up there.

If he is using the reset button as a "do that thing (again)" button, he can look forward to learning how to program so a button press would make the sequence repeat in a sketch that just ran and ran, that is to say not rely on the relatively crude technique of exploiting a full reset on the board.

I had to refresh my memory of what it does in this context!

a7

I have removed the exit part as this appears not how you write a script like this - forgive my ignorance.

Below is the current code I am using and it still does not work the way I want. When I use this on the motor is keeps going around and never stops. I want to use the first "for" loop to make the motor rotate 5 full revolutions and stop. So how do I do this?

// defines pins
#define dirPin 10
#define stepPin 11

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  for (int i = 1; i <= 5; i++) {

    digitalWrite(dirPin, HIGH);  // Enables the motor to move in a particular direction
                                 // Makes 400 pulses for making one full cycle rotation
    for (int x = 0; x < 1600; x++) {
      digitalWrite(stepPin, HIGH);
      // by changing the time delay between the steps below
      // we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
      delayMicroseconds(100);
      digitalWrite(stepPin, LOW);
      // Do not mess with this delay as it will throw things off.
      delayMicroseconds(200);
    }
  }
}

This does what you wrote in the previous post, but perhaps you will need to redefine your goal when you want to do more than stop the motor.

// defines pins
#define dirPin 10
#define stepPin 11

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  for (int i = 1; i <= 5; i++) {

    digitalWrite(dirPin, HIGH);  // Enables the motor to move in a particular direction
                                 // Makes 400 pulses for making one full cycle rotation
    for (int x = 0; x < 1600; x++) {
      digitalWrite(stepPin, HIGH);
      // by changing the time delay between the steps below
      // we can change the rotation speed. 100 is the fastest and 2000 is around the slowest
      delayMicroseconds(100);
      digitalWrite(stepPin, LOW);
      // Do not mess with this delay as it will throw things off.
      delayMicroseconds(200);
    }
  }
  while(1); // this halts the code here, forevAr
}

Because, you have kept the codes in the loop function. Place the codes in the setup() function.
or
Connect a Button with the UNO, place the codes under a user-defined function, keep polling the Button in the loop() function for close condition and then call the function to rotate the motor for five full revolutions.

void loop()
{
    if(Button is closed)
    {
          rotateMotor();
    }
}

void rotateMotor()
{
    for (int i = 1; i <= 5; i++) 
    {
        digitalWrite(dirPin, HIGH);  // Enables the motor to move in a par
        for (int x = 0; x < 1600; x++) 
        {
           digitalWrite(stepPin, HIGH);
           // by changing the time delay between the steps below
           delayMicroseconds(100);
           digitalWrite(stepPin, LOW);
           // Do not mess with this delay as it will throw things off.
           delayMicroseconds(200);
        }
    }

}

Check my maths. The process will take 2.4 seconds, so triggering it when the button is low will work, but @cyberman42 is advised to get his fat finger off the button or another 2.4 seconds worth of action will ensue.

The full solution would involve initiating the process only once when then button went down, not to be repeated until the button is again seen to be going from not-pressed to pressed.

a7

You could use something like this for simple code:

void setup(){
  delay(digitalRead(buttonPin));
  //turn the motor
}

void loop(){
  delay(1);
}

I always put a delay(1); in my void loop() as this makes the code (if you add interrupts or something) more reliable.

  • Say what ? :roll_eyes:

Ok, thanks. I assumed it would do nothing!

So it exits/stops the execution of main(), which called loop()?

What happens then, on a microcontroller? It's not like there's an O/S to return to. Does some kind of halt op-code get executed, or does it go into an infinite loop?

Or in my experience at least. Ok, I know that delay(); isn't the recommended thing to use, but I really meant just some kind of waiting period, so the void loop() doesn't start over again immediately after a cycle.

1 Like

It's like while(1);, or my favorite for(; ; );, except interrupts are disabled. TBH I am only quite sure, I want to see the code before being certain. It def stops the normal regular execution of the loop function.

a7

1 Like