I am creating a robot and want to implement record and replay functionality

I am creating a robot according to the code provided below and it is controlled by an app created by mit app inventor .i wanted to add record and replay functionality in it such that each actions performed by the robot the first time is recorded and is automatically replayed from the next time from a click of a button .please help there should also be a button for erasing recorded actions so that there is space for recording the next set of recordings .

#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial BT(A0, A1);

Servo servo_1;
Servo servo_2;

int motor_r2 = 6;
int motor_r1 = 7;

int motor_l2 = 8;
int motor_l1 = 9;

int state;
int speed = 130;

int pos1 = 90;
int pos2 = 90;

int pump = 4;
int pwm = 5;

void setup(){
servo_1.attach(2);
servo_2.attach(3);
servo_1.write(pos1); 
servo_2.write(pos2); 

pinMode(motor_l1, OUTPUT);
pinMode(motor_l2, OUTPUT); 
pinMode(motor_r1, OUTPUT);
pinMode(motor_r2, OUTPUT);

pinMode(pump, OUTPUT);

pinMode(pwm, OUTPUT);

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
BT.begin(9600); // Setting the baud rate of Software Serial Library 
 
 delay(1000);
 }

void loop(){
//if some date is sent, reads it and saves in state
if(BT.available() > 0){     
state = BT.read(); 
Serial.println(state);
if(state > 15){speed = state;}      
}
  
   // if the state is '1' the DC motor will go forward
     if (state == 1){forword();Serial.println("Forward!");}

// if the state is '2' the motor will Backword
else if (state == 2){backword();Serial.println("Backword!");}
    
// if the state is '3' the motor will turn left
else if (state == 3){turnLeft();Serial.println("Turn LEFT");}
    
// if the state is '4' the motor will turn right
else if (state == 4){turnRight();Serial.println("Turn RIGHT");}
    
// if the state is '5' the motor will Stop
else if (state == 5) {stop();Serial.println("STOP!");}    

else if (state == 6) {Serial.println("lift");  if(pos1<180){pos1 = pos1+1;}} 
else if (state == 7) {Serial.println("right"); if(pos1>0){pos1 = pos1-1;}} 
else if (state == 8) {Serial.println("up");    if(pos2>0){pos2 = pos2-1;}}  
else if (state == 9) {Serial.println("down");  if(pos2<180){pos2 = pos2+1;}} 

else if (state == 10){Serial.println("pump on");digitalWrite(pump, HIGH);} 
else if (state == 11){Serial.println("pump off");digitalWrite(pump, LOW);} 

servo_1.write(pos1); 
servo_2.write(pos2); 

analogWrite(pwm, speed);
delay(30);
}


void stop(){
    digitalWrite(motor_l1, LOW);
    digitalWrite(motor_l2, LOW); 
    digitalWrite(motor_r1, LOW);
    digitalWrite(motor_r2, LOW);
}

void forword(){
    digitalWrite(motor_l1, LOW);
    digitalWrite(motor_l2, HIGH); 
    digitalWrite(motor_r1, HIGH);
    digitalWrite(motor_r2, LOW);  
}

void backword(){
    digitalWrite(motor_l1, HIGH);
    digitalWrite(motor_l2, LOW); 
    digitalWrite(motor_r1, LOW);
    digitalWrite(motor_r2, HIGH);   
}


void turnRight(){
    digitalWrite(motor_l1, LOW);
    digitalWrite(motor_l2, HIGH); 
    digitalWrite(motor_r1, LOW);
    digitalWrite(motor_r2, HIGH);  
}

void turnLeft(){
    digitalWrite(motor_l1, HIGH);
    digitalWrite(motor_l2, LOW); 
    digitalWrite(motor_r1, HIGH);
    digitalWrite(motor_r2, LOW); 
}

please help me with the code

presumably you can capture commands along with a timestamp to maintain their timing. you need to decide what that capture buffer is and how buttons are processes to begin capturing, execute and reset

but there are several problems with the code you posted

  • Serial.read() returns an ASCII character. subtracting an ascii zero '0' results in an integer from 0-9.

  • reading just a single char prevents having commands > 9. perhaps use Serial.readBytesUntil() to read multiple characters and used atoi() to translate the string to an integer

  • the block of code that processes commands, state does so regardless of receiving a new command. so that last received command is repeatedly executed. perhaps put this block into a sub-function that is called when a new command is received and eventually to process "captured" commands

I will correct the mistakes but can you please give me a record and replay code for an experiment like this

why don't you make an attempt to write it yourself.
forum members can provide advice

Ok then, tell me the procedure please

???

Plenty of resources on the internet. The problem becomes, which is your favorite? I like DroneBotWorkshop for record/playback of servo movement... (find "record" on page)

This is fully servo record and replay i want to record and replay gear motor and pump functions. I used a driver shield gor motors. By the way my code has changed.

//Arduino Bluetooth Controlled Car
//Before uploading the code you have to install the necessary library
//Note - Disconnect the Bluetooth Module before hiting the upload button otherwise you'll get compilation error message.
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
//After downloading the library open Arduino IDE >> go to sketch >> Include Libray >> ADD. ZIP Libray >> Select the downloaded 
//ZIP File >> Open it >> Done
//Now You Can Upload the Code without any problem but make sure the bt module isn't connected with Arduino while uploading code

#include <AFMotor.h>   
#include <Servo.h>
#define pum  15

//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);

char command; 
Servo servo1;
Servo servo2;



void setup() 
{       
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
  servo1.attach(10);
  servo2.attach(9);
  pinMode(pum, OUTPUT);
}

void loop(){
  if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); //initialize with motors stoped
    //Change pin mode only if new command is different from previous.   
    //Serial.println(command);
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    case 'W':
      pumpon();
      break;
    case 'w':
      pumpoff();
      break;
    case 'U':
      servoup();
      break;
    case 'u':
      servodown();
      break;
    case 'V':
      servoleft();
      break;
    case 'v':
      servoright();
      break;
   
    }
  } 
}

void forward()
{
  motor1.setSpeed(500); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(500); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(500);//Define maximum velocity
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(500);//Define maximum velocity
  motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(500); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(500); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(500); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(500); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(500); //Define maximum velocity
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(500); //Define maximum velocity
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(500); //Define maximum velocity
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(500); //Define maximum velocity
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(500); //Define maximum velocity
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(500); //Define maximum velocity
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(500); //Define maximum velocity
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(500); //Define maximum velocity
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
} 

void Stop()
{
  motor1.setSpeed(0); //Define minimum velocity
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum velocity
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum velocity
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum velocity
  motor4.run(RELEASE); //stop the motor when release the button
}
void pumpon(){
  digitalWrite (pum, HIGH);

}
void pumpoff(){
  digitalWrite (pum, LOW);

}
void servoup(){
  servo2.write(0);
}
void servodown(){
  servo2.write(100);
}
void servoleft(){
  servo1.write(0);
}
void servoright(){
  servo1.write(140);
}

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