Help me to understand the code

``#include "EEPROM.h"

define D1 2 // direction of motor rotation 1

define M1 3 // PWM left motor

define D2 4 // direction of motor rotation 2

define M2 5 // PWM right motor

define HORN 13 // additional channel 1

//#define autoOFF 2500 // milliseconds after which the robot stops when the connection

define cmdL 'L' // UART-command for left motor

define cmdR 'R' // UART-command for right motor

define cmdH 'H' // UART-command for additional channel (for example Horn)

define cmdF 'F' // UART-command for EEPROM operation

define cmdr 'r' // UART-command for EEPROM operation (read)

define cmdw 'w'[/code] // UART-command for EEPROM operation (write)

That's not really code - just a bunch of defines. It doesn't actually do anything.

This: "#define D1 2 // direction of motor rotation 1" just means that you can use "D1" in the code instead of the value "2", to set the direction of rotation.

This: "#define cmdL 'L' // UART-command for left motor" is similar. When you use "cmdL" in your code, obviously being sent out a serial port, 'L' is actually sent.

'define's make code easier to read. Also, you can change a 'define' at the top of your code, and the change is reflected right throughout your code. Effectively, a 'define' creates a 'constant'.

Thank you. I want to ask one more question related to motor driving can you help me?

ShayanAhmad: Thank you. I want to ask one more question related to motor driving can you help me?

I won't know if I can help until you ask the question. :)

void loop() { //if some date is sent, reads it and saves in state if(Serial.available() > 0){ state = Serial.read(); flag=0; } // if the state is '1' the DC motor will go forward if (state == '1') { digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); if(flag == 0){ Serial.println("Go Forward!"); flag=1; } }

// if the state is '2' the motor will turn left else if (state == '2') { digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); if(flag == 0){ Serial.println("Turn LEFT"); flag=1; } delay(1500); state=3; stateStop=1; }

in order to move forward lagic to both motors goes high.

I want to do that if left command is recieved then right motor should move forward and left motor should move reverse so that the robot can turn left easily

I just answered this question in your other thread. Did you not see my answer?

Change this:-

else if (state == '2') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);

to this:-

else if (state == '2') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);  // ***I changed this from 'LOW' to 'HIGH'

And please use code tags in future, as you did in the other thread. :)

But if I will make it high,then instead of moving at left it will move forward as the 1st state.
Like this

if (state == ‘1’) {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
if(flag == 0){
Serial.println(“Go Forward!”);
flag=1;[/code]

ShayanAhmad: But if I will make it high,then instead of moving at left it will move forward as the 1st state. Like this

if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;

Ah, in that case just reverse the LOW and HIGH. I can't see your wiring, so had to guess. You have your motors wired differently to most people. You actually want this then:-

if (state == '2') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);

Remember, we can neither see your motor connections nor read your mind. Comments in the code make it much easier. Also, if you originally posted all of your code, rather than simple snippets, we would have seen the code for 'forward', and known exactly what was required for 'reverse' on the left motor.

And place the code tags around the code, as I've done, not your text. :)

May I sent you the complete code in order to understand the queries?

ShayanAhmad: May I sent you the complete code in order to understand the queries?

Don't send it to me in a PM - post it in this thread, between code tags, so that someone, not necessarily me, can answer.

Didn't my last reply answer your question? There are only two possibilities, and I've now posted both. I'm out of ideas if it didn't do it. Someone else can handle this from now on.

I got the answer from your last post.
I want to ask some more questions so I am posting my complete code.
Thank you so much you are helping me in my project

int motor1Pin1 = 3; // pin 2 on L293D IC int motor1Pin2 = 4; // pin 7 on L293D IC int enable1Pin = 6; // pin 1 on L293D IC int motor2Pin1 = 8; // pin 10 on L293D IC int motor2Pin2 = 9; // pin 15 on L293D IC int enable2Pin = 11; // pin 9 on L293D IC int state; int flag=0; //makes sure that the serial only prints once the state int stateStop=0; void setup() { // sets the pins as outputs: pinMode(motor1Pin1, OUTPUT); pinMode(motor1Pin2, OUTPUT); pinMode(enable1Pin, OUTPUT); pinMode(motor2Pin1, OUTPUT); pinMode(motor2Pin2, OUTPUT); pinMode(enable2Pin, OUTPUT); // sets enable1Pin and enable2Pin high so that motor can turn on: digitalWrite(enable1Pin, HIGH); digitalWrite(enable2Pin, HIGH); // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

void loop() { //if some date is sent, reads it and saves in state if(Serial.available() > 0){ state = Serial.read(); flag=0; } // if the state is '1' the DC motor will go forward if (state == '1') { digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); if(flag == 0){ Serial.println("Go Forward!"); flag=1; } }

// if the state is '2' the motor will turn left else if (state == '2') { digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); if(flag == 0){ Serial.println("Turn LEFT"); flag=1; } delay(1500); state=3; stateStop=1; } // if the state is '3' the motor will Stop else if (state == '3' || stateStop == 1) { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, LOW); if(flag == 0){ Serial.println("STOP!"); flag=1; } stateStop=0; } // if the state is '4' the motor will turn right else if (state == '4') { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, LOW); digitalWrite(motor2Pin1, LOW); digitalWrite(motor2Pin2, HIGH); if(flag == 0){ Serial.println("Turn RIGHT"); flag=1; } delay(1500); state=3; stateStop=1; } // if the state is '5' the motor will Reverse else if (state == '5') { digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin2, HIGH); digitalWrite(motor2Pin1, HIGH); digitalWrite(motor2Pin2, LOW); if(flag == 0){ Serial.println("Reverse!"); flag=1; } } //For debugging purpose //Serial.println(state); }[/code]

Have you tried reading this?

Ok can you let me khow what does the following lines means

int motor1Pin1 = 3; // pin 2 on L293D IC
int motor1Pin2 = 4; // pin 7 on L293D IC
int enable1Pin = 6; // pin 1 on L293D IC
int motor2Pin1 = 8; // pin 10 on L293D IC
int motor2Pin2 = 9; // pin 15 on L293D IC
int enable2Pin = 11; // pin 9 on L293D IC
[code]

and what is the working of flag commmang in the code 

[code]
int state;
int flag=0;        //makes sure that the serial only prints once the state
int stateStop=0;

Those lines declare variables with names that can be used in the program and give them initial values.

Note that they appear to be pin numbers and and as such will not change and are not greater than 255 so could be declared as const byte, like this

const byte motor1Pin1 = 3; // pin 2 on L293D IC

Note also that the comments do not match the code, which is not helpful.

flag is not a command, it is a variable that is given a value. The value is tested by the code later and actions are performed or not based on its value.

  if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;
        }

ShayanAhmad:
I want to ask some more questions so I am posting my complete code.

...

// if the state is ‘4’ the motor will turn right
   else if (state == ‘4’) {
//…
      state=3;
   }
//…

In the above snippet you have confused data types (highlighted in red and green). The code, almost certainly, will not work the way you expect it to.

In the C language. The figure ‘4’ inside single quotes, denotes an ASCII character literal, with the ordinal value 53. The figured 3 without the quotes, denotes the ordinal value 3. Hence, ‘4’ - 3 = 50

It is good practice to use separate variables for storing input and maintaining program state.

state = 0;
char chIn;

chIn = Serial.read();
if(chIn == '4') {
 state = 3)
}

UKHeliBob: Those lines declare variables with names that can be used in the program and give them initial values.

Note that they appear to be pin numbers and and as such will not change and are not greater than 255 so could be declared as const byte, like this

const byte motor1Pin1 = 3; // pin 2 on L293D IC

Note also that the comments do not match the code, which is not helpful.

flag is not a command, it is a variable that is given a value. The value is tested by the code later and actions are performed or not based on its value.

  if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;
        }

I want to ask that what is the working of flag in the following code lines?It is a code for motor driving.
.
int flag=0; // (it is decleard in the beginning of the code)

void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '1' the DC motor will go forward
    if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;
        }
    }

If "its [sic] urgent", don't you think you need to provide all your code?

See reply #13 The clue as to what the flag variable controls is in its declaration

int flag = 0;      //makes sure that the serial only prints once the state

I want to ask that what is the working to flag command in the following lines of the code?

and flag is declared in the begining of teh code int flag=0; //makes sure that the serial only prints once the state

if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;