Hello there!
I am kind of new to Arduino so I don't know that much so any help advice will be nice.
What I am doing here is I want to control a robot arm made by 5 servo motors. Those motors are controlled by 5 potentiometers. The data form potentiometers is send by HC-05 Bluetooth module to HC-06. The modules are connected to each other and I am reading data form HC-05. The problem is that the robot trembles when the circuit is powered on. For the motors I am using power bank to power them up. Is there a way to remove this trembling? Components uno mini and other circuit is maker uno similar to normal uno. 5 10k oms potentiometers.
Here is the code of HC-05:
// TX: HC-05 (master) send "<a,b,c,d,e>\n" form 5 potentiometers
#include <SoftwareSerial.h>
SoftwareSerial BT(3, 2); // RX(D3) к-> HC-05 TX, TX(D2) к-> HC-05 RX (voltage divider)
const byte POT_PINS[5] = {A0, A1, A2, A3, A4};
const unsigned long PERIOD_MS = 30; // ~33Hz sending
unsigned long lastSend = 0;
int mapToAngle(int raw) {
if (raw < 0) raw = 0;
if (raw > 1023) raw = 1023;
return map(raw, 0, 1023, 0, 180);
}
void setup() {
Serial.begin(9600);
BT.begin(9600);
Serial.println("TX ready (HC-05 @ 9600)");
}
void loop() {
// 1) read 5 potentiometers then map 0...180
int a[5];
for (int i = 0; i < 5; i++) {
a[i] = mapToAngle(analogRead(POT_PINS[i]));
}
// 2) send data trow Bluetooth on ~33Hz
unsigned long now = millis();
if (now - lastSend >= PERIOD_MS) {
lastSend = now;
BT.print('<');
BT.print(a[0]); BT.print(',');
BT.print(a[1]); BT.print(',');
BT.print(a[2]); BT.print(',');
BT.print(a[3]); BT.print(',');
BT.print(a[4]); BT.print(">\n");
// debug Serial Monitor
Serial.print("TX <");
for (int i = 0; i < 5; i++) {
Serial.print(a[i]);
if (i < 4) Serial.print(',');
}
Serial.println('>');
}
}
Code for HC-06:
// RX: HC-06 (slave) recive "<a,b,c,d,e>" and control 5 servo
#include <NeoSWSerial.h>
#include <Servo.h>
// HC-06 TX -> D2 (RX on Arduino), HC-06 RX <- D3 (voltage divider)
NeoSWSerial BT(2,3); // RX, TX
const byte SERVO_PINS[5] = {4, 5, 6, 7, 8};
Servo servos[5];
char buf[48];
int idx = 0;
bool inPacket = false;
void setup() {
Serial.begin(9600);
BT.begin(9600);
for (int i = 0; i < 5; i++) {
servos[i].attach(SERVO_PINS[i]);
servos[i].write(90);
}
Serial.println("RX ready (HC-06 @ 9600)");
}
void processPacket() {
buf[idx] = '\0';
int v[5], cnt = 0;
char* tok = strtok(buf, ",");
while (tok && cnt < 5) {
int val = constrain(atoi(tok), 0, 180);
v[cnt++] = val;
tok = strtok(NULL, ",");
}
if (cnt == 5) {
for (int i = 0; i < 5; i++) {
servos[i].write(v[i]);
}
Serial.print("RX <");
for (int i = 0; i < 5; i++) {
Serial.print(v[i]);
if (i < 4) Serial.print(',');
}
Serial.println('>');
}
}
void loop() {
while (BT.available()) {
char c = (char)BT.read();
if (c == '<') { inPacket = true; idx = 0; continue; }
if (!inPacket) continue;
if (c == '>') {
processPacket();
inPacket = false;
idx = 0;
continue;
}
if (idx < (int)sizeof(buf) - 1) {
buf[idx++] = c;
} else {
inPacket = false;
idx = 0;
}
}
}



