0

I need help :( I have this robotic arm project and I am using standard potentiometers attached to a "controller assembly" to detect the movement of the human arm.

I have monitored the values of my sensors, and they are consistent.

My problem is that, from time to time, my servo motors are moving unwantedly.

I have a reliable separate power supply, 5V 20A. The external power supply and the Arduino Mega's ground are also connected.

here is my "controller" code

//ControllerArmModule
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX

int wristNod = 0;
int wristTwist = 0;

int elbowBend = 0;
int elbowNod = 0;
int elbowReach = 0;

//for sending the values to String
String wristNDel, wnd;
String wristTDel, wtd;

String elbowBDel, ebd;
String elbowNDel, eNd;
String elbowRDel, erd;

void setup() {
wristNDel = String();
wnd = String();
wristTDel = String();
wtd = String();

elbowBDel = String();
ebd = String();
elbowNDel = String();
eNd = String();
elbowRDel = String();
erd = String();

BTSerial.begin(38400);
Serial.begin(9600);
}

void loop() {
//=========================================================
//wrist control++++++++++++++++++++++++++++++++++++++++++++
wristNod = analogRead(A6);
wristNod = constrain(wristNod, 370,  840);
//wristNod = map(wristNod, 370, 840, 0, 20);
wnd = "n";
wristNDel = wristNod + wnd;
BTSerial.println(wristNDel); 

wristTwist = analogRead(A7);
wristTwist = constrain(wristTwist, 540,  1023);
//wristTwist = map(wristTwist, 540, 1023, 20, 0);
wtd = "w";
wristTDel = wristTwist + wtd;
BTSerial.println(wristTDel);


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//elbow control--------------------------------------------
elbowBend = analogRead(A10);
elbowBend = constrain(elbowBend, 480, 850);
//elbowBend = map(elbowBend, 480, 850, 0, 20);
ebd = "v";
elbowBDel = elbowBend + ebd;
BTSerial.print(elbowBDel);
Serial.println(elbowBDel);

elbowNod = analogRead(A11);
elbowNod = constrain(elbowNod, 200, 700);
//elbowNod = map(elbowNod,  200, 700, 0, 20);
eNd = "o";
elbowNDel = elbowNod + eNd;
BTSerial.print(elbowNDel);

elbowReach = analogRead(A12);
elbowReach = constrain(elbowReach, 120, 790);
//elbowReach = map(elbowReach, 120, 790, 0, 20);
erd = "a";
elbowRDel = elbowReach + erd;
BTSerial.print(elbowRDel);
//---------------------------------------------------------

delay(300);
}

and here is the other end. "robotic arm"

//RoboticArmModule
#include <Servo.h>
#include <SoftwareSerial.h>

Servo wristNodServo;
Servo wristTwistServo;

Servo elbowBendServo;
Servo elbowNodServo;
Servo elbowReachServo;

SoftwareSerial BTSerial(10, 11); // RX | TX

String rawString = "";

int wristNod = 0;
int wristTwist = 0;

int elbowBend = 0;
int elbowNod = 0;
int elbowReach = 0;

void setup() {

wristNodServo.attach(22);
wristTwistServo.attach(24);

elbowBendServo.attach(28);
elbowNodServo.attach(30);
elbowReachServo.attach(32);

Serial.begin(9600);
BTSerial.begin(38400);
}

void loop() {
if(BTSerial.available()){ 
int inChar = BTSerial.read();
if (isDigit(inChar)) {
  // convert the incoming byte to a char and add it to the string:
  rawString += (char)inChar;
}
//======================================================
//wrist control+++++++++++++++++++++++++++++++++++++++++
if (inChar == 'n') {
  wristNod = rawString.toInt();
  wristNod = constrain(wristNod, 370,  840);
  wristNod = map(wristNod, 370,  840, 180, 20);
  rawString = "";
}

if (inChar == 'w') {
  Serial.println(rawString);
  wristTwist = rawString.toInt();
  wristTwist = constrain(wristTwist, 540,  1023);
  wristTwist = map(wristTwist, 540,  1023, 180, 0);
  rawString = "";
}
//Serial.println(wristTwist);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//elbow control-----------------------------------------
if (inChar == 'v') {
  elbowBend = rawString.toInt();
  elbowBend = constrain(elbowBend, 480, 850);
  elbowBend = map(elbowBend, 480, 850, 90, 180);
  rawString = "";
}

if (inChar == 'o') {
  elbowNod = rawString.toInt();
  elbowNod = constrain(elbowNod, 200, 700);
  elbowNod = map(elbowNod, 200, 700, 180, 50);
  rawString = "";
}
if (inChar == 'a') {
  elbowReach = rawString.toInt();
  elbowReach = constrain(elbowReach, 120, 790);
  elbowReach = map(elbowReach, 120, 790, 15, 180);
  rawString = "";
}

//------------------------------------------------------
wristNodServo.write(wristNod);
wristTwistServo.write(wristTwist);

elbowBendServo.write(elbowBend);
elbowNodServo.write(elbowNod);
elbowReachServo.write(elbowReach);

//delay(30);
  }
}

I hope someone could help me.

  • Try to output debug messages at all important parts of your code. Maybe add a timestamp to each message. Then, when it happens again, directly check the logs for what happened. We need to know, where the problem happens. – chrisl Jun 26 '18 at 22:09
  • 1
    All ADCs have noise. Use an exponential moving average even if it is short. Adding bluetooth adds complexity. Try w/o it to see if everything is working as expected first. At the receiver end do not allow unexpected values. You have an expectation as to how fast the value will change. Put code in the receiver end to remove unexpected values which fall out side of those expectations. If the received value is not changing turn off the PWM to the servos to keep them from fidgeting. – st2000 Jun 27 '18 at 0:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.