0

I would like to know how to set the current position for the DC motors to zero and store the current position in an array. after that I would like it to loop in the array to run all steps that was stored in the array?

Am using Genuino UNO board, I have 4 DC motors and each 2 wheels are connected to an H-bridge.

I have an application that have a "save" button and a "Run" button. For the save button, I would like it to set the current position for each wheel to zero first then store the positions of each wheel in an array. For the run button, I would like it to loop in the array that was made by the save button and show the output on each wheels.

basically what am trying to achieve is to make the robotic car that can move any direction I tell it to move in the same time I can control the wheel speed. all that move are being sent via Bluetooth as numbers.

I hope it's clear now

here is my code

#include <SoftwareSerial.h>


SoftwareSerial Bluetooth(0, 1); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)

// Define the motors and the pins  
// Motor A1

int enA1 = 2;
int in11 = 3;
int in21 = 4;


// Motor B1

int enB1 = 5;
int in31 = 6;
int in41 = 7;

//other H-bridge 

// Motor A2

int enA2 = 8;
int in12 = 9;
int in22 = 10;


// Motor B2

int enB2 = 11;
int in32 = 12;
int in42 = 13;

int wheelSpeed = 125;
int dataIn, m;
//lbw left back wheel / lfw left fron wheel / rbw right back wheel / rfw right front wheel
int lbw[50], lfw[50], rbw[50], rfw[50]; // for storing positions/steps
int index = 0;

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

  Bluetooth.begin(9600); // Default baud rate of the Bluetooth module
  Bluetooth.setTimeout(1);
  Serial.print("connected");
  Serial.print("-------------------");
  delay(20);

  pinMode(enA1, OUTPUT);
  pinMode(enB1, OUTPUT);
  pinMode(in11, OUTPUT);
  pinMode(in21, OUTPUT);
  pinMode(in31, OUTPUT);
  pinMode(in41, OUTPUT);
  pinMode(enA2, OUTPUT);
  pinMode(enB2, OUTPUT);
  pinMode(in12, OUTPUT);
  pinMode(in22, OUTPUT);
  pinMode(in32, OUTPUT);
  pinMode(in42, OUTPUT);
}

void loop() {
  // Check for incoming data

 if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.read();  // Read the data

    if (dataIn == 0) {
      m = 0;
    }
    if (dataIn == 1) {
      m = 1;
    }
    if (dataIn == 2) {
      m = 2;
    }
    if (dataIn == 3) {
      m = 3;
    }
    if (dataIn == 4) {
      m = 4;
    }
    if (dataIn == 5) {
      m = 5;
    }
    if (dataIn == 6) {
      m = 6;
    }
    if (dataIn == 7) {
      m = 7;
    }
    if (dataIn == 8) {
      m = 8;

    }
    if (dataIn == 9) {
      m = 9;
    }
    if (dataIn == 10) {
      m = 10;
    }
    if (dataIn == 11) {
      m = 11;
    }

    if (dataIn == 12) {
      m = 12;
    }
    if (dataIn == 14) {
      m = 14;
    }
    // Set speed
    if (dataIn >= 16) {
      wheelSpeed = dataIn * 10;
      Serial.println(wheelSpeed);
    }
  }
  if (m == 4) {
    moveSidewaysLeft();
  }
  if (m == 5) {
    moveSidewaysRight();
  }
  if (m == 2) {
    moveForward();
  }
  if (m == 7) {
    moveBackward();
  }
  if (m == 3) {
    moveRightForward();
  }
  if (m == 1) {
    moveLeftForward();
  }
  if (m == 8) {
    moveRightBackward();
  }
  if (m == 6) {
    moveLeftBackward();
  }
  if (m == 9) {
    rotateLeft();
  }
  if (m == 10) {
    rotateRight();
  }

  if (m == 0) {
    stopMoving();
  }
  //Serial.println(dataIn);
  // If button "SAVE" is pressed
  if (m == 12) {
    if (index == 0) {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, LOW);  
        digitalWrite(in31, LOW);  
        digitalWrite(in41, LOW); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, LOW);  
        digitalWrite(in32, LOW);  
        digitalWrite(in42, LOW); 

    }


    // save position into the array 
  }
  //if (m == 14) {
   // runSteps();
    //if (dataIn != 14) {
      //stopMoving();
      // Clear the array data to 0
   // }
  //}

}

//method to read the array from what was saved by the save button and run it step by step
//void runSteps() {}

void moveForward() {
        digitalWrite(in11, HIGH); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, HIGH); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, HIGH); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, HIGH); 
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Forward");
}

void moveBackward() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, HIGH); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, HIGH); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, HIGH); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, HIGH);
        Serial.println("Motor: move Backward");
}

void moveSidewaysLeft() {
        digitalWrite(in11, HIGH); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, HIGH); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, HIGH); 
        digitalWrite(in32, HIGH); 
        digitalWrite(in42, LOW);
        Serial.println("Motor: move Sideways Left");
}
void moveSidewaysRight() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, HIGH); 
        digitalWrite(in31, HIGH); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, HIGH); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, HIGH);   
        Serial.println("Motor: move Sideways right");
}

void moveRightForward() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, HIGH); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, HIGH); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, LOW);   
        Serial.println("Motor: move Right Forward");
}


void moveLeftBackward() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, HIGH); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, HIGH); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, LOW);   
        Serial.println("Motor: move left Back ward");
}

void moveLeftForward() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, HIGH); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, HIGH); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, LOW);   
        Serial.println("Motor: move Left Forward");
}

void moveRightBackward() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, HIGH); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, HIGH);   
        Serial.println("Motor: move Right Backward");
}

void rotateLeft() {
        digitalWrite(in11, HIGH); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, HIGH); 
        digitalWrite(in12, HIGH); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, HIGH); 
        Serial.println("Motor: rotate Left");
  }

void rotateRight() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, HIGH); 
        digitalWrite(in31, HIGH); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, HIGH); 
        digitalWrite(in32, HIGH); 
        digitalWrite(in42, LOW); 
        Serial.println("Motor: rotate Right");
  }

void stopMoving() {
        digitalWrite(in11, LOW); 
        digitalWrite(in21, LOW); 
        digitalWrite(in31, LOW); 
        digitalWrite(in41, LOW); 
        digitalWrite(in12, LOW); 
        digitalWrite(in22, LOW); 
        digitalWrite(in32, LOW); 
        digitalWrite(in42, LOW); 
        Serial.println("Motor: off");
  }
  • 1
    A DC motor does not know steps, unless you introduce them by using something like a rotary encoder – chrisl Nov 16 '19 at 12:05
  • Could it be, that you mean the current state (i.e. the contents of the variables inXX and enXX of the DC motors instead of the "position"? I guess so because you said, you want to reset the position to zero on a save event, and what you do then is setting the inXX all to LOW. That would be easy to achieve. But saving the current position of the wheels is not that easy. Then you do define what position means. Do your wheels turn constantly, like car wheels, or are they more like servos and have a well defined position within 360°? (I guess they are like car wheels when I see the prog.) – Peter Paul Kiefer Nov 16 '19 at 14:07
  • This is the type DC motor am using: amazon.com/Electric-Magnetic-Gearbox-Plastic-Yeeco/dp/… What I mean by positions are that if I press the buttons to make car move forward ,side way left then backward --> all these I want it to be save in the array so when I press the run button it will redo all these position manually by looping in the array. I hope you understood me – Miss Nature Nov 16 '19 at 14:26
  • that does not explain what positions means ... you basically repeated what you already said in your post – jsotola Nov 16 '19 at 19:28
0

As I understood it from your comments, your aim is not really to store the actual positions of the wheels or your robot, but to save and repeat the actions, that the robot did. (Meaning you don't have a "position" here, but only a measurement of wheel speed)

If that is true, then you can save this in a 2-dimensional array with the element form {speed_wheel_1, speed_wheel2, duration}. So each action is defined by the speeds of both wheels and the duration of the action, for example:

Driving forward at full speed for 3s, turn on the spot to the left with half speed for 1.5s, drive a curve with motor left full speed and motor right half speed for 5s

This would translate to the following elements:

{100, 100, 3000}
{-50, 50, 1500}
{100, 50, 5000}

The speeds have to be read in percent here and the durations in milliseconds.


Obviously you are controlling the robot over serial. I don't completely understand your code (also as you didn't show us the full code), so I will give some hints, how you can go on from here:

You are obtaining the speeds from serial into the variable wheelSpeed. We cannot see, how you are using this variable later, but you can use it in some way to set the speed fields of a new array element.

Then you need the duration. The Arduino framework has a function for that: millis(). It will return the number of milliseconds since the startup, much like a clock. When an action starts (when the corresponding serial command is received), you have to set a timestamp:

unsigned long timestamp;
...
timestamp = millis();

When the action ends (either because it actually only had a limited duration, or because another command is received), you calculate the difference between the current millis value and the timestamp. This is the duration.

So the corresponding code parts will look like this:

unsigned long timestamp = 0; // Define timestamp at global scope
unsigned int current_element = 0; // A variable to keep track of the current array element
int actions[10][3]; // Array to hold up to 10 actions

void start_action(byte speed_left, byte speed_right){
    actions[current_element][0] = speed_left;
    actions[current_element][1] = speed_right;
    timestamp = millis();
    // Start the action with the provided speeds here
}

void end_action(unsigned int element_index){
    actions[current_element][2] = millis() - timestamp;
    current_element++;
    if(current_element >= 10){
        // Do something to prevent more actions. Maybe just disable recording.
}

For repeating the actions, you just have to cycle over the array and set the corresponding speeds for the corresponding time.

If you want to go fancy, you can ditch the 2-dimensional array and instead define a struct for an action:

struct Action{
    int speed_left;
    int speed_right;
    unsigned long duration;
};

Then you can create a simple array of the Action objects and reference the fields by name:

Action actions[10];
...
actions[1].duration = millis() - timestamp;
| improve this answer | |
  • thank you so much I've updated my question + the code. but I have another problem if you can help me with which is: when my phone send data(any number 1-14) via Bluetooth it doesn't show the output on the wheels!! example: when I pressed button move forward which send number 2 to the Bluetooth module, the wheels doesn't move. – Miss Nature Nov 17 '19 at 15:03
  • I don't know what exactly you are sending, but I guess you are sending an ASCII "2" over bluetooth. In your code you only check for the decimal values of the read data. An ASCII-encoded "2" equals a decimal 50. Now you can either change the sending code to send raw decimal values instead of ASCII-encoded data, or you an change the receiving code to react to ASCII encoded data. The last can be done by changing the numbers in the if statements to the appropriate numbers of the ASCII characters. Or just simply writing them as characters: if(dataIn == '2') – chrisl Nov 17 '19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.