Diy 3d printed robot arm
Easy project for Arduino enthusiasts
Components and supplies
3
Feetech Mini Servo motor 120 degrees 9g
8
3D Printed parts
1
Arduino Uno Rev3
1
Netzteil
3
Feetech 6 KG 360 Degrees Continuous Rotation Servo motor
1
9V Battery Supply
Tools and machines
1
Laptop
1
Multimeter
Apps and platforms
1
HTerm
1
Visual Studio Code Extension for Arduino
1
Visual Studio 2022
Project description
Code
Control via serialinterface
cpp
1#include <Arduino.h> 2#include <TransmissionClass.H> 3#include <Servo.h> 4 5Transmission t1; 6 7Servo servo1; 8Servo servo2; 9Servo servo3; 10Servo servo4; 11Servo servo5; 12Servo servo6; 13 14char buf; 15 16int PosMotor1; 17int PosMotor2; 18int PosMotor3; 19int PosMotor4; 20int PosMotor5; 21int PosMotor6; 22 23int PrePosMotor1; 24int PrePosMotor2; 25int PrePosMotor3; 26int PrePosMotor4; 27int PrePosMotor5; 28int PrePosMotor6; 29 30void setup() { 31 32 Serial.begin(115200); 33 t1.ChangeTransmissionStatus(); 34 35 servo1.attach(3); 36 servo2.attach(5); 37 servo3.attach(6); 38 servo4.attach(9); 39 servo5.attach(10); 40 servo6.attach(11); 41 42 PosMotor1 = t1.GetPosMotor1(); 43 PosMotor2 = t1.GetPosMotor2(); 44 PosMotor3 = t1.GetPosMotor3(); 45 PosMotor4 = t1.GetPosMotor4(); 46 PosMotor5 = t1.GetPosMotor5(); 47 PosMotor6 = t1.GetPosMotor6(); 48 49 PrePosMotor1 = PosMotor1; 50 PrePosMotor2 = PosMotor2; 51 PrePosMotor3 = PosMotor3; 52 PrePosMotor4 = PosMotor4; 53 PrePosMotor5 = PosMotor5; 54 PrePosMotor6 = PosMotor6; 55 56 57 58} 59 60void loop() { 61 62 t1.UpdatePositions(); 63 64 PosMotor1 = t1.GetPosMotor1(); 65 if(PosMotor1 != PrePosMotor1) 66 { 67 servo1.write(PosMotor1); 68 PrePosMotor1 = PosMotor1; 69 Serial.println(PosMotor1); 70 } 71 72 PosMotor2 = t1.GetPosMotor2(); 73 if(PosMotor2 != PrePosMotor2) 74 { 75 servo2.write(PosMotor2); 76 PrePosMotor2 = PosMotor2; 77 Serial.println(PosMotor2); 78 } 79 80 PosMotor3 = t1.GetPosMotor3(); 81 if(PosMotor3 != PrePosMotor3) 82 { 83 servo3.write(PosMotor3); 84 PrePosMotor3 = PosMotor3; 85 Serial.println(PosMotor3); 86 } 87 88 PosMotor4 = t1.GetPosMotor4(); 89 if(PosMotor4 != PrePosMotor4) 90 { 91 servo4.write(PosMotor4); 92 PrePosMotor4 = PosMotor4; 93 Serial.println(PosMotor4); 94 } 95 96 PosMotor5 = t1.GetPosMotor5(); 97 if(PosMotor5 != PrePosMotor5) 98 { 99 servo5.write(PosMotor5); 100 PrePosMotor5 = PosMotor5; 101 Serial.println(PosMotor5); 102 } 103 104 PosMotor6 = t1.GetPosMotor6(); 105 if(PosMotor6 != PrePosMotor6) 106 { 107 servo6.write(PosMotor6); 108 PrePosMotor6 = PosMotor6; 109 Serial.println(PosMotor6); 110 } 111}
Control via potentiometers
cpp
1#include <Arduino.h> 2#include <Servo.h> 3 4#define Poti1 A0 5#define Poti2 A1 6#define Poti3 A2 7 8Servo M1; 9Servo M2; 10Servo M3; 11 12int PosMotor1 , PosMotor2 , PosMotor3; 13 14void setup() 15{ 16 Serial.begin(9600); 17 M1.attach(3); 18 M2.attach(5); 19 M3.attach(6); 20 21 pinMode(Poti1 , INPUT); 22 pinMode(Poti2 , INPUT); 23 pinMode(Poti3 , INPUT); 24} 25 26void loop() 27{ 28 PosMotor1 = analogRead(Poti1) / 5.683; 29 if(PosMotor1 > 180) 30 { 31 PosMotor1 = 180; 32 } 33 if(PosMotor1 <= 0) 34 { 35 PosMotor1 = 0; 36 } 37 M1.write(PosMotor1); 38 39 PosMotor2 = analogRead(Poti2) / 5.683; 40 if(PosMotor2 > 180) 41 { 42 PosMotor2 = 180; 43 } 44 if(PosMotor2 <= 0) 45 { 46 PosMotor2 = 0; 47 } 48 M2.write(PosMotor2); 49 50 PosMotor3 = analogRead(Poti3) / 5.683; 51 if(PosMotor3 > 180) 52 { 53 PosMotor3 = 180; 54 } 55 if(PosMotor3 <= 0) 56 { 57 PosMotor3 = 0; 58 } 59 M3.write(PosMotor3); 60 Serial.println(PosMotor3); 61 62}
Class for serial COM
cpp
1//Written by Julian Schermer 2 3#ifndef TransmissionClass_H 4#define TransmissionClass_H 5#include <Arduino.h> 6 7class Transmission 8{ 9 10 //Declaration of the main variables of the class 11 12 private: 13 bool _Status; 14 int _messagepos; 15 const int _MAX_LENGHT = 19; 16 char _buf; 17 char _Message[19]; 18 19 char _Motor1[3]; 20 char _Motor2[3]; 21 char _Motor3[3]; 22 char _Motor4[3]; 23 char _Motor5[3]; 24 char _Motor6[3]; 25 26 int _PosMotor1; 27 int _PosMotor2; 28 int _PosMotor3; 29 int _PosMotor4; 30 int _PosMotor5; 31 int _PosMotor6; 32 33 34 //Parameterless Constructor 35 //In our case is no need for changing values of variables by the start of the programm 36 37 public: 38 39 int Counter = 0; 40 41 Transmission() 42 { 43 _messagepos = 0; 44 _Status = false; 45 46 _PosMotor1 = 90; 47 _PosMotor2 = 90; 48 _PosMotor3 = 90; 49 _PosMotor4 = 90; 50 _PosMotor5 = 90; 51 _PosMotor6 = 90; 52 } 53 54 //Returns the value of each motor to the main program 55 56 int GetPosMotor1() 57 { 58 return _PosMotor1; 59 } 60 61 int GetPosMotor2() 62 { 63 return _PosMotor2; 64 } 65 66 int GetPosMotor3() 67 { 68 return _PosMotor3; 69 } 70 71 int GetPosMotor4() 72 { 73 return _PosMotor4; 74 } 75 76 int GetPosMotor5() 77 { 78 return _PosMotor5; 79 } 80 81 int GetPosMotor6() 82 { 83 return _PosMotor6; 84 } 85 86 87 //Activates or deactivates the Transmission 88 89 void ChangeTransmissionStatus() 90 { 91 _Status = !_Status; 92 } 93 94 //Reads the serial input and converts it into an integer 95 96 void UpdatePositions() 97 { 98 99 while (Serial.available() > 0 && _Status == true) 100 { 101 _buf = Serial.read(); 102 //Serial.println(_buf); 103 104 if(_buf != '\n' && _messagepos < _MAX_LENGHT - 1) 105 { 106 _Message[_messagepos] = _buf; 107 _messagepos++; 108 } 109 else 110 { 111 112 //Converts the main char array to 6 part arrays 113 114 _Message[_messagepos] = '\0'; 115 116 //Serial.println("array:"); 117 118 for (size_t i = 0; i < sizeof(_Motor1); i++){ 119 _Motor1[i] = _Message[i]; 120 Serial.println(_Motor1[i]); 121 } 122 123 for (size_t i = 0; i < sizeof(_Motor2); i++){ 124 _Motor2[i] = _Message[i+3]; 125 //Serial.println(_Motor2[i]); 126 } 127 128 for (size_t i = 0; i < sizeof(_Motor3); i++){ 129 _Motor3[i] = _Message[i+6]; 130 //Serial.println(_Motor3[i]); 131 } 132 133 for (size_t i = 0; i < sizeof(_Motor4); i++){ 134 _Motor4[i] = _Message[i+9]; 135 //Serial.println(_Motor4[i]); 136 } 137 138 for (size_t i = 0; i < sizeof(_Motor5); i++){ 139 _Motor5[i] = _Message[i+12]; 140 //Serial.println(_Motor5[i]); 141 } 142 143 for (size_t i = 0; i < sizeof(_Motor6); i++){ 144 _Motor6[i] = _Message[i+15]; 145 //Serial.println(_Motor6[i]); 146 } 147 148 Counter++; 149 //Serial.println("counter:"); 150 //Serial.println(Counter); 151 152 //Converts a char array to an integer 153 154 _PosMotor1 = (_Motor1[2] - '0') * 1; 155 _PosMotor1 += (_Motor1[1] - '0') * 10; 156 _PosMotor1 += (_Motor1[0] - '0') * 100; 157 158 //Serial.println("after parsing"); 159 Serial.println(_PosMotor1); 160 161 _PosMotor2 = (_Motor2[2] - 48) * 1; 162 _PosMotor2 += (_Motor2[1] - 48) * 10; 163 _PosMotor2 += (_Motor2[0] - 48) * 100; 164 165 _PosMotor3 = (_Motor3[2] - 48) * 1; 166 _PosMotor3 += (_Motor3[1] - 48) * 10; 167 _PosMotor3 += (_Motor3[0] - 48) * 100; 168 169 _PosMotor4 = (_Motor4[2] - 48) * 1; 170 _PosMotor4 += (_Motor4[1] - 48) * 10; 171 _PosMotor4 += (_Motor4[0] - 48) * 100; 172 173 _PosMotor5 = (_Motor5[2] - 48) * 1; 174 _PosMotor5 += (_Motor5[1] - 48) * 10; 175 _PosMotor5 += (_Motor5[0] - 48) * 100; 176 177 _PosMotor6 = (_Motor6[2] - 48) * 1; 178 _PosMotor6 += (_Motor6[1] - 48) * 10; 179 _PosMotor6 += (_Motor6[0] - 48) * 100; 180 181 //Prepares for the next incomming message 182 183 _messagepos = 0; 184 185 } 186 187 188 } 189 190 } 191 192 193}; 194#endif
Comments
Only logged in users can leave comments