-
Notifications
You must be signed in to change notification settings - Fork 4
/
Robotic_arm.txt
51 lines (40 loc) · 1.24 KB
/
Robotic_arm.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <Servo.h>
const int potPinBase = A0;
const int potPinHand = A1;
const int basePin = 8;
const int handPin = 6;
Servo servoBase;
Servo servoHand;
void setup() {
servoBase.attach(basePin);
servoHand.attach(handPin);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void scanServos() {
// Scan from 0 to 180 degrees
for (int servoPos = 0; servoPos <= 180; servoPos++) {
servoBase.write(servoPos);
servoHand.write(servoPos);
delay(100);
}
// Now scan back from 180 to 0 degrees
for (int servoPos = 180; servoPos >= 0; servoPos--) {
servoBase.write(servoPos);
servoHand.write(servoPos);
delay(100);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void loop() {
// Read the potentiometer values and control the servos
int potValueBase = analogRead(potPinBase);
int angleBase = map(potValueBase, 0, 1023, 0, 180);
angleBase = 180 - angleBase;
servoBase.write(angleBase);
int potValueHand = analogRead(potPinHand);
int angleHand = map(potValueHand, 0, 1023, 0, 180);
servoHand.write(angleHand);
delay(15); // Small delay for stability
// Uncomment the following line to perform the scan operation
// scanServos();
}