arduino function

hi, guys, my Arduino wan't to execute the second function anybody can help me it's stopped in the first function
this is my code

#include <NewPing.h>
int trig = 12 ;
int echo = 13 ;
int max_dis = 200 ;
#define in1 3
#define in2 5
#define in3 6
#define in4 9
NewPing sonic (trig , echo , max_dis);

//#define enA;
//#define enB;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
// pinMode(enA,OUTPUT);
// pinMode(enB,OUTPUT);

}

void forward(){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);

}
void reverse(){

digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);

}
void toright(){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);

}

void toleft(){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);

}
void off(){
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);

}
void loop() {

forward();
delay(5000);
reverse();
off();
}

What do you mean by "first"?
What do you mean by "second"?

The loop function loops, that's its job - it's called again and again.

When you call forward, you then delay for five seconds, then you call reverse, micoseconds later you call off and microseconds (if that) afterwards, you call forward again. Reverse and off are over so quickly that you will not see them do anything.

Add more delays and perhaps some Serial.prints to see what's happening.

in the loop i have two function forward() and reverse and off()
when i transfered the code in my arduino my dc motor stopped in in the first function forawrd

hicham147:
i my dc motor stopped in in the first function forawrd

How do you know this?

Did you read reply #2?

i just add it to code in every function Serial.print("forward") for the fisrt function forward and and the same for reverse and off() when i run the code it work fine in the monitor iall it's ok but in my dc motor topped in the fisrt function forward ()

Post the code that you are describing, otherwise we're wasting time.

#include <NewPing.h>
int trig = 12 ;
int echo = 13 ;
int max_dis = 200 ;
#define in1 3
#define in2 5
#define in3 6
#define in4 9
NewPing sonic (trig , echo , max_dis);

//#define enA;
//#define enB;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
// pinMode(enA,OUTPUT);
// pinMode(enB,OUTPUT);

}

void forward(){
digitalWrite(3,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(9,HIGH);
Serial.println("forward");

}
void reverse(){

digitalWrite(3,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(9,LOW);
Serial.println("reverse");
}
void toright(){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);

}

void toleft(){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);

}
void off(){
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
Serial.println("off");

}
void loop() {

forward();
delay(5000);
reverse();
off();
}

...and the serial output is...?

Full reverse to off - lots of kickback.

Try this:

void loop() {
       
 forward();
        delay(5000);
       reverse();
        delay(5000);
       off();
        delay(5000);
}

thank you it work it bro :slight_smile:

Sometimes I think that my posts must be invisible. This is one of those times.

i'm so sorry i'm new in this forum thank you for your help don't stop helping people

hicham147:
thank you it work it bro :slight_smile:

Thre important question is do you know understand why it did not appear to work before?

wildbill:
Sometimes I think that my posts must be invisible. This is one of those times.

A perfectly normal feeling on the forum, we all get that sometimes.