0% found this document useful (0 votes)
102 views

The Code of Robot

This document describes code for controlling an Arduino robot arm and wheels using Bluetooth from a smartphone. The code includes servos attached to pins to control each joint of the arm, reads input from Bluetooth to control movement, and allows adjusting the speed of movements.

Uploaded by

darshan k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

The Code of Robot

This document describes code for controlling an Arduino robot arm and wheels using Bluetooth from a smartphone. The code includes servos attached to pins to control each joint of the arm, reads input from Bluetooth to control movement, and allows adjusting the speed of movements.

Uploaded by

darshan k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

/*

Arduino Robot Arm and Mecanum Wheels Robot

Smartphone Control via Bluetooth

by Dejan, www.HowToMechatronics.com

*/

#include <SoftwareSerial.h>

#include <AccelStepper.h>

#include <Servo.h>

int i;

Servo servo01;

Servo servo02;

Servo servo03;

Servo servo04;

Servo servo05;

Servo servo06;

SoftwareSerial Bluetooth(3, 4); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)

//int wheelSpeed = 1500;

//int lbw[50], lfw[50], rbw[50], rfw[50]; // arrays for storing positions/steps

int servo1Pos, servo2Pos, servo3Pos, servo4Pos, servo5Pos, servo6Pos; // current position

int servo1PPos, servo2PPos, servo3PPos, servo4PPos, servo5PPos, servo6PPos; // previous position

//int servo01SP[50], servo02SP[50], servo03SP[50], servo04SP[50], servo05SP[50], servo06SP[50]; //


for storing positions/steps

int speedDelay = 20;

int index = 0;

int dataIn;
int m = 0;

void setup() {

servo01.attach(5);

servo02.attach(6);

servo03.attach(7);

servo04.attach(8);

servo05.attach(9);

servo06.attach(10);

Bluetooth.begin(38400); // Default baud rate of the Bluetooth module

Bluetooth.setTimeout(5);

delay(20);

Serial.begin(38400);

// Move robot arm to initial position

servo1PPos = 90;

servo01.write(servo1PPos);

servo2PPos = 100;

servo02.write(servo2PPos);

servo3PPos = 120;

servo03.write(servo3PPos);

servo4PPos = 95;

servo04.write(servo4PPos);

servo5PPos = 60;

servo05.write(servo5PPos);

servo6PPos = 110;

servo06.write(servo6PPos);

}
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;

if (dataIn == 16) {

m = 16;

if (dataIn == 17) {

m = 17;

if (dataIn == 18) {

m = 18;
}

if (dataIn == 19) {

m = 19;

if (dataIn == 20) {

m = 20;

if (dataIn == 21) {

m = 21;

if (dataIn == 22) {

m = 22;

if (dataIn == 23) {

m = 23;

if (dataIn == 24) {

m = 24;

if (dataIn == 25) {

m = 25;

if (dataIn == 26) {

m = 26;

if (dataIn == 27) {

m = 27;
}

// Move robot arm

// Move servo 1 in positive direction

while (m == 16) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo01.write(servo1PPos);

servo1PPos++;

delay(speedDelay);

// Move servo 1 in negative direction

while (m == 17) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo01.write(servo1PPos);

servo1PPos--;

delay(speedDelay);

// Move servo 2

while (m == 19) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo02.write(servo2PPos);
servo2PPos++;

delay(speedDelay);

while (m == 18) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo02.write(servo2PPos);

servo2PPos--;

delay(speedDelay);

// Move servo 3

while (m == 20) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo03.write(servo3PPos);

servo3PPos++;

delay(speedDelay);

while (m == 21) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo03.write(servo3PPos);

servo3PPos--;

delay(speedDelay);
}

// Move servo 4

while (m == 23) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo04.write(servo4PPos);

servo4PPos++;

delay(speedDelay);

while (m == 22) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo04.write(servo4PPos);

servo4PPos--;

delay(speedDelay);

// Move servo 5

while (m == 25) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo05.write(servo5PPos);

servo5PPos++;

delay(speedDelay);

}
while (m == 24) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo05.write(servo5PPos);

servo5PPos--;

delay(speedDelay);

// Move servo 6

while (m == 26) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo06.write(servo6PPos);

servo6PPos++;

delay(speedDelay);

while (m == 27) {

if (Bluetooth.available() > 0) {

m = Bluetooth.read();

servo06.write(servo6PPos);

servo6PPos--;

delay(speedDelay);

// If arm speed slider is changed

if (dataIn > 101 & dataIn < 250) {


speedDelay = dataIn / 10; // Change servo speed (delay time)

You might also like