Stop dc engine from loop and print Finish "once"

Hy guys.
Finally i reached to stop the DC engine from with a while statement.
Now i want to Print something that tells me that the look has gone through but i dont know where tu put it.

Now its inside the loop so it writes not only once. Putting after the loop bracket i get an error :-/

Any hint please?

int motorPin1=2;
int motorPin2=3;
int x = 2;


void setup(){
  pinMode(motorPin1,OUTPUT);
  pinMode(motorPin2,OUTPUT);
  Serial.begin(9600);
}

void motorStop(){
  digitalWrite(motorPin1,LOW);
  digitalWrite(motorPin2,LOW);
  delay(500);
}


void loop(){
    while(x <= 3){
    motorStop();                    // Motor Stop  
    digitalWrite(motorPin1,HIGH);   // Motor Vor
    digitalWrite(motorPin2,LOW);
    delay(1000);

    motorStop();                    // Motor Stop  
    digitalWrite(motorPin1,LOW);   // Motor Vor
    digitalWrite(motorPin2,HIGH);
    delay(1000);
    
    Serial.print(x);
    x++;
  }
  motorStop();
  Serial.print("Finish Loop");
}

Just take the parts of loop() that you don't want to repeat forever, and put them in the end of setup().

int motorPin1 = 2;
int motorPin2 = 3;
int x = 2;

void setup()
{
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  Serial.begin(9600);

  while (x <= 3)
  {
    motorStop();                    // Motor Stop
    digitalWrite(motorPin1, HIGH);  // Motor Vor
    digitalWrite(motorPin2, LOW);
    delay(1000);

    motorStop();                    // Motor Stop
    digitalWrite(motorPin1, LOW);  // Motor Vor
    digitalWrite(motorPin2, HIGH);
    delay(1000);

    Serial.print(x);
    x++;
  }
  
  motorStop();
  Serial.print("Finish Loop");
}

void motorStop()
{
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
  delay(500);
}

void loop() { }
1 Like

I don't understand your description what kind of functionality you want to have.

What your code is doing is

stop motor
run motor clockwise for one second
stop motor
run motor counterclockwise for one second

loop does what its name says loop = looping = repeating infinetely
repeat all code
only as x contains value 4 the condition is false

if this has repeated two time because x is initialised with value 2

repeat

while(x <= 3){

the condition is false
(4 <= 3) evaluates to false
=> code inside the while-loop is not executed

the code below the while-loop will be executed
This means make the execution of the serial-message conditional

As a recommendation for future postings describe the wanted functionality in this way
no programming-terms
just everyday-words
but describe each single step of what shall happen

best regards Stefan

Thanks :slight_smile:
1
2
3
Finish Loop with: 3 loops.

It's fully documented in the Bare Example sketch that ships with the IDE:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.