safwan092

Final Code - simple arm robot 6dof voice controlled geetech 5 commands only

Jan 8th, 2026
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.27 KB | None | 0 0
  1. /*
  2. Voice Controlled 6DOF Robotic Arm
  3. Using PWM Servo Driver and Voice Recognition Module
  4.  
  5. Voice Commands:
  6. Up = 1 (0x11)
  7. Down = 2 (0x12)
  8. Left = 3 (0x13)
  9. Right = 4 (0x14)
  10. Grab = 5 (0x15)
  11. */
  12.  
  13. #include <Wire.h>
  14. #include <Adafruit_PWMServoDriver.h>
  15. #include <SoftwareSerial.h>
  16.  
  17. // ================= CONFIGURATION =================
  18. // Adjust these values according to your setup
  19.  
  20. // Movement steps in degrees for each servo (changeable from here)
  21. const int STEP_BASE = 40; // Base rotation (Left/Right)
  22. const int STEP_SHOULDER = 25; // Shoulder (Up/Down)
  23. const int STEP_ELBOW = 25; // Elbow
  24. const int STEP_WRIST = 25; // Wrist
  25. const int STEP_WRIST_ROT = 25; // Wrist rotation
  26. const int STEP_GRIPPER = 40; // Gripper
  27.  
  28. // Default movement step (for any servos not specifically configured)
  29. const int DEFAULT_STEP = 25;
  30.  
  31. // Servo angle limits for each joint (min, max)
  32. const int SERVO_LIMITS[6][2] = {
  33. {10, 170}, // Servo 0: Base rotation (left/right)
  34. {100, 170}, // Servo 1: Shoulder (up/down)
  35. {90, 170}, // Servo 2: Elbow
  36. {90, 170}, // Servo 3: Wrist
  37. {0, 180}, // Servo 4: Wrist rotation
  38. {50, 120} // Servo 5: Gripper
  39. };
  40.  
  41. // Movement steps array for each servo
  42. const int STEP_SIZES[6] = {
  43. STEP_BASE, // Base
  44. STEP_SHOULDER, // Shoulder
  45. STEP_ELBOW, // Elbow
  46. STEP_WRIST, // Wrist
  47. STEP_WRIST_ROT, // Wrist rotation
  48. STEP_GRIPPER // Gripper
  49. };
  50.  
  51. // Initial positions for servos
  52. int servoPositions[6] = {90, 120, 90, 90, 90, 90};
  53.  
  54. // PWM Settings
  55. #define SERVOMIN 125 // Minimum pulse length count
  56. #define SERVOMAX 575 // Maximum pulse length count
  57.  
  58. // Voice module commands
  59. #define CMD_UP 0x11
  60. #define CMD_DOWN 0x12
  61. #define CMD_LEFT 0x13
  62. #define CMD_RIGHT 0x14
  63. #define CMD_GRAB 0x15
  64.  
  65. // Additional movement commands you can add
  66. #define CMD_ELBOW_UP 0x16
  67. #define CMD_ELBOW_DOWN 0x17
  68. #define CMD_WRIST_UP 0x18
  69. #define CMD_WRIST_DOWN 0x19
  70.  
  71. // ================= OBJECT DECLARATIONS =================
  72. Adafruit_PWMServoDriver pwmBoard = Adafruit_PWMServoDriver(0x40);
  73. SoftwareSerial voiceSerial(4, 3); // RX, TX
  74.  
  75. // ================= FUNCTION PROTOTYPES =================
  76. void initializeVoiceModule();
  77. void initializeServos();
  78. void moveServo(uint8_t servoNum, int angle);
  79. void moveUp();
  80. void moveDown();
  81. void moveLeft();
  82. void moveRight();
  83. void toggleGripper();
  84. int angleToPulse(int angle);
  85. void safeMoveServo(uint8_t servoNum, int targetAngle);
  86. void moveServoBy(uint8_t servoNum, int delta);
  87. void printServoPositions();
  88. void printStepSizes();
  89. void processVoiceCommand(byte command);
  90. void moveElbowUp();
  91. void moveElbowDown();
  92. void moveWristUp();
  93. void moveWristDown();
  94.  
  95. // ================= SETUP =================
  96. void setup() {
  97. Serial.begin(9600);
  98. voiceSerial.begin(9600);
  99.  
  100. Serial.println(F("=========================================="));
  101. Serial.println(F("Voice Controlled 6DOF Robotic Arm"));
  102. Serial.println(F("=========================================="));
  103.  
  104. // Print step sizes
  105. printStepSizes();
  106.  
  107. // Initialize voice recognition module
  108. initializeVoiceModule();
  109.  
  110. // Initialize servo driver and servos
  111. initializeServos();
  112.  
  113. Serial.println(F("\nSystem Ready!"));
  114. Serial.println(F("Voice commands: Up, Down, Left, Right, Grab"));
  115. Serial.println(F("=========================================="));
  116. }
  117.  
  118. // ================= MAIN LOOP =================
  119. void loop() {
  120. // Check for voice commands
  121. if (voiceSerial.available()) {
  122. byte command = voiceSerial.read();
  123. processVoiceCommand(command);
  124. }
  125. }
  126.  
  127. // ================= VOICE MODULE INITIALIZATION =================
  128. void initializeVoiceModule() {
  129. Serial.println(F("Initializing voice module..."));
  130.  
  131. voiceSerial.write(0xAA);
  132. voiceSerial.write('0x00'); // Use 0x00 instead of '0x00'
  133. delay(100);
  134.  
  135. voiceSerial.write(0xAA);
  136. voiceSerial.write(0x37); // Compact mode
  137. delay(100);
  138.  
  139. voiceSerial.write(0xAA);
  140. voiceSerial.write('0x00'); // Use 0x00 instead of '0x00'
  141. delay(100);
  142.  
  143. voiceSerial.write(0xAA);
  144. voiceSerial.write(0x21); // Import group 1
  145.  
  146. Serial.println(F("Voice module initialized"));
  147. }
  148.  
  149. // ================= SERVO INITIALIZATION =================
  150. void initializeServos() {
  151. Serial.println(F("Initializing servos..."));
  152.  
  153. Wire.begin();
  154. pwmBoard.begin();
  155. pwmBoard.setPWMFreq(60);
  156.  
  157. // Set all servos to initial positions
  158. for (int i = 0; i < 6; i++) {
  159. moveServo(i, servoPositions[i]);
  160. delay(100);
  161. }
  162.  
  163. Serial.println(F("Servos initialized"));
  164. }
  165.  
  166. // ================= VOICE COMMAND PROCESSING =================
  167. void processVoiceCommand(byte command) {
  168. switch (command) {
  169. case CMD_UP:
  170. Serial.println(F("Voice Command: UP"));
  171. moveUp();
  172. break;
  173.  
  174. case CMD_DOWN:
  175. Serial.println(F("Voice Command: DOWN"));
  176. moveDown();
  177. break;
  178.  
  179. case CMD_LEFT:
  180. Serial.println(F("Voice Command: LEFT"));
  181. moveLeft();
  182. break;
  183.  
  184. case CMD_RIGHT:
  185. Serial.println(F("Voice Command: RIGHT"));
  186. moveRight();
  187. break;
  188.  
  189. case CMD_GRAB:
  190. Serial.println(F("Voice Command: GRAB"));
  191. toggleGripper();
  192. break;
  193.  
  194. // Uncomment these if you add more voice commands to your module
  195. /*
  196. case CMD_ELBOW_UP:
  197. Serial.println(F("Voice Command: ELBOW UP"));
  198. moveElbowUp();
  199. break;
  200.  
  201. case CMD_ELBOW_DOWN:
  202. Serial.println(F("Voice Command: ELBOW DOWN"));
  203. moveElbowDown();
  204. break;
  205.  
  206. case CMD_WRIST_UP:
  207. Serial.println(F("Voice Command: WRIST UP"));
  208. moveWristUp();
  209. break;
  210.  
  211. case CMD_WRIST_DOWN:
  212. Serial.println(F("Voice Command: WRIST DOWN"));
  213. moveWristDown();
  214. break;
  215. */
  216.  
  217. default:
  218. Serial.print(F("Unknown command: 0x"));
  219. Serial.println(command, HEX);
  220. break;
  221. }
  222.  
  223. // Display current servo positions
  224. printServoPositions();
  225. }
  226.  
  227. // ================= SERVO CONTROL FUNCTIONS =================
  228. void moveUp() {
  229. Serial.print(F("Moving shoulder UP by "));
  230. Serial.print(STEP_SIZES[1]);
  231. Serial.println(F(" degrees..."));
  232. moveServoBy(1, STEP_SIZES[1]); // Shoulder servo
  233. }
  234.  
  235. void moveDown() {
  236. Serial.print(F("Moving shoulder DOWN by "));
  237. Serial.print(STEP_SIZES[1]);
  238. Serial.println(F(" degrees..."));
  239. moveServoBy(1, -STEP_SIZES[1]); // Shoulder servo
  240. }
  241.  
  242. void moveLeft() {
  243. Serial.print(F("Moving base LEFT by "));
  244. Serial.print(STEP_SIZES[0]);
  245. Serial.println(F(" degrees..."));
  246. moveServoBy(0, STEP_SIZES[0]); // Base servo
  247. }
  248.  
  249. void moveRight() {
  250. Serial.print(F("Moving base RIGHT by "));
  251. Serial.print(STEP_SIZES[0]);
  252. Serial.println(F(" degrees..."));
  253. moveServoBy(0, -STEP_SIZES[0]); // Base servo
  254. }
  255.  
  256. // Additional movement functions (uncomment if you add more voice commands)
  257. void moveElbowUp() {
  258. Serial.print(F("Moving elbow UP by "));
  259. Serial.print(STEP_SIZES[2]);
  260. Serial.println(F(" degrees..."));
  261. moveServoBy(2, STEP_SIZES[2]); // Elbow servo
  262. }
  263.  
  264. void moveElbowDown() {
  265. Serial.print(F("Moving elbow DOWN by "));
  266. Serial.print(STEP_SIZES[2]);
  267. Serial.println(F(" degrees..."));
  268. moveServoBy(2, -STEP_SIZES[2]); // Elbow servo
  269. }
  270.  
  271. void moveWristUp() {
  272. Serial.print(F("Moving wrist UP by "));
  273. Serial.print(STEP_SIZES[3]);
  274. Serial.println(F(" degrees..."));
  275. moveServoBy(3, STEP_SIZES[3]); // Wrist servo
  276. }
  277.  
  278. void moveWristDown() {
  279. Serial.print(F("Moving wrist DOWN by "));
  280. Serial.print(STEP_SIZES[3]);
  281. Serial.println(F(" degrees..."));
  282. moveServoBy(3, -STEP_SIZES[3]); // Wrist servo
  283. }
  284.  
  285. void toggleGripper() {
  286. static bool isGripperOpen = false;
  287.  
  288. if (isGripperOpen) {
  289. Serial.println(F("Closing gripper..."));
  290. safeMoveServo(5, SERVO_LIMITS[5][1]); // Closed position
  291. } else {
  292. Serial.println(F("Opening gripper..."));
  293. safeMoveServo(5, SERVO_LIMITS[5][0]); // Open position
  294. }
  295.  
  296. isGripperOpen = !isGripperOpen;
  297. }
  298.  
  299. // ================= SMART SERVO CONTROL =================
  300. void moveServoBy(uint8_t servoNum, int delta) {
  301. int currentPos = servoPositions[servoNum];
  302. int targetPos = currentPos + delta;
  303.  
  304. // Check if movement is within limits
  305. if (targetPos < SERVO_LIMITS[servoNum][0]) {
  306. Serial.print(F("Servo "));
  307. Serial.print(servoNum);
  308. Serial.print(F(" reached minimum limit ("));
  309. Serial.print(SERVO_LIMITS[servoNum][0]);
  310. Serial.println(F("°)!"));
  311. targetPos = SERVO_LIMITS[servoNum][0];
  312. } else if (targetPos > SERVO_LIMITS[servoNum][1]) {
  313. Serial.print(F("Servo "));
  314. Serial.print(servoNum);
  315. Serial.print(F(" reached maximum limit ("));
  316. Serial.print(SERVO_LIMITS[servoNum][1]);
  317. Serial.println(F("°)!"));
  318. targetPos = SERVO_LIMITS[servoNum][1];
  319. }
  320.  
  321. // Only move if target is different from current
  322. if (targetPos != currentPos) {
  323. safeMoveServo(servoNum, targetPos);
  324. } else {
  325. Serial.println(F("Movement blocked by limit"));
  326. }
  327. }
  328.  
  329. void safeMoveServo(uint8_t servoNum, int targetAngle) {
  330. // Constrain angle to servo limits
  331. targetAngle = constrain(targetAngle,
  332. SERVO_LIMITS[servoNum][0],
  333. SERVO_LIMITS[servoNum][1]);
  334.  
  335. // Update position and move servo
  336. servoPositions[servoNum] = targetAngle;
  337. moveServo(servoNum, targetAngle);
  338.  
  339. Serial.print(F("Servo "));
  340. Serial.print(servoNum);
  341. Serial.print(F(" moved to: "));
  342. Serial.print(targetAngle);
  343. Serial.println(F("°"));
  344. }
  345.  
  346. void moveServo(uint8_t servoNum, int angle) {
  347. // Note: +1 offset because PWM channels start from 0
  348. pwmBoard.setPWM(servoNum + 1, 0, angleToPulse(angle));
  349. delay(50); // Small delay for smooth movement
  350. }
  351.  
  352. // ================= UTILITY FUNCTIONS =================
  353. int angleToPulse(int angle) {
  354. return map(angle, 0, 180, SERVOMIN, SERVOMAX);
  355. }
  356.  
  357. void printServoPositions() {
  358. Serial.println(F("\nCurrent Servo Positions:"));
  359. Serial.println(F("------------------------"));
  360.  
  361. const char* servoNames[] = {"Base", "Shoulder", "Elbow", "Wrist", "WristRot", "Gripper"};
  362.  
  363. for (int i = 0; i < 6; i++) {
  364. Serial.print(servoNames[i]);
  365. Serial.print(" (S");
  366. Serial.print(i);
  367. Serial.print("): ");
  368. Serial.print(servoPositions[i]);
  369. Serial.print("° [");
  370. Serial.print(SERVO_LIMITS[i][0]);
  371. Serial.print("° - ");
  372. Serial.print(SERVO_LIMITS[i][1]);
  373. Serial.print("°] Step: ");
  374. Serial.print(STEP_SIZES[i]);
  375. Serial.println("°");
  376. }
  377.  
  378. Serial.println(F("------------------------"));
  379. }
  380.  
  381. void printStepSizes() {
  382. Serial.println(F("Movement Step Sizes:"));
  383. Serial.println(F("---------------------"));
  384. Serial.print(F("Base (Left/Right): "));
  385. Serial.print(STEP_BASE);
  386. Serial.println(F("°"));
  387. Serial.print(F("Shoulder (Up/Down): "));
  388. Serial.print(STEP_SHOULDER);
  389. Serial.println(F("°"));
  390. Serial.print(F("Elbow: "));
  391. Serial.print(STEP_ELBOW);
  392. Serial.println(F("°"));
  393. Serial.print(F("Wrist: "));
  394. Serial.print(STEP_WRIST);
  395. Serial.println(F("°"));
  396. Serial.print(F("Wrist Rotation: "));
  397. Serial.print(STEP_WRIST_ROT);
  398. Serial.println(F("°"));
  399. Serial.print(F("Gripper: "));
  400. Serial.print(STEP_GRIPPER);
  401. Serial.println(F("°"));
  402. Serial.println(F("---------------------"));
  403. }
Advertisement
Add Comment
Please, Sign In to add comment