Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Arduino Codes
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Medics
Arduino Codes
Commits
23a4bfc8
Commit
23a4bfc8
authored
1 month ago
by
mhby1g21
Browse files
Options
Downloads
Patches
Plain Diff
added serial version
parent
312a8a60
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
serial/serial.ino
+142
-0
142 additions, 0 deletions
serial/serial.ino
with
142 additions
and
0 deletions
serial/serial.ino
0 → 100644
+
142
−
0
View file @
23a4bfc8
#include
<Servo.h>
// Create servo objects
Servo
index_finger
;
Servo
middle_finger
;
Servo
ring_finger
;
Servo
pinky_finger
;
// Servo pins - adjust according to your Arduino connections
const
int
INDEX_PIN
=
2
;
const
int
MIDDLE_PIN
=
3
;
const
int
RING_PIN
=
4
;
const
int
PINKY_PIN
=
5
;
// Current positions of each finger
int
index_pos
=
90
;
int
middle_pos
=
90
;
int
ring_pos
=
90
;
int
pinky_pos
=
90
;
// Buffer for incoming serial data
String
inputBuffer
=
""
;
bool
commandReady
=
false
;
void
setup
()
{
// Initialize serial communication at 9600 bps
Serial
.
begin
(
9600
);
while
(
!
Serial
)
{
;
// Wait for serial port to connect (needed for native USB)
}
// Attach servos to their pins
index_finger
.
attach
(
INDEX_PIN
);
middle_finger
.
attach
(
MIDDLE_PIN
);
ring_finger
.
attach
(
RING_PIN
);
pinky_finger
.
attach
(
PINKY_PIN
);
// Initialize all fingers to neutral position
index_finger
.
write
(
index_pos
);
middle_finger
.
write
(
middle_pos
);
ring_finger
.
write
(
ring_pos
);
pinky_finger
.
write
(
pinky_pos
);
// Show startup message
Serial
.
println
(
"InMoov Hand Controller Ready"
);
Serial
.
println
(
"Format: [finger]:[angle]"
);
Serial
.
println
(
"Example: index:180"
);
}
void
loop
()
{
// Check for incoming serial data
while
(
Serial
.
available
()
>
0
)
{
// Read incoming byte
char
incomingByte
=
Serial
.
read
();
// Add to buffer if not end of line
if
(
incomingByte
!=
'\n'
)
{
inputBuffer
+=
incomingByte
;
}
else
{
// End of line, set flag to process command
commandReady
=
true
;
}
}
// Process command if ready
if
(
commandReady
)
{
Serial
.
print
(
"Received command: "
);
Serial
.
println
(
inputBuffer
);
processCommand
(
inputBuffer
);
// Clear buffer and reset flag
inputBuffer
=
""
;
commandReady
=
false
;
}
// Small delay to avoid hogging CPU
delay
(
10
);
}
void
processCommand
(
String
command
)
{
// Parse command in format: "finger:angle"
int
colonIndex
=
command
.
indexOf
(
':'
);
if
(
colonIndex
!=
-
1
)
{
String
finger
=
command
.
substring
(
0
,
colonIndex
);
String
angleStr
=
command
.
substring
(
colonIndex
+
1
);
int
angle
=
angleStr
.
toInt
();
// Constrain angle to valid range
angle
=
constrain
(
angle
,
0
,
180
);
// Update finger position based on command
if
(
finger
==
"index"
)
{
index_pos
=
angle
;
index_finger
.
write
(
index_pos
);
Serial
.
print
(
"Index finger moved to: "
);
Serial
.
println
(
angle
);
}
else
if
(
finger
==
"middle"
)
{
middle_pos
=
angle
;
middle_finger
.
write
(
middle_pos
);
Serial
.
print
(
"Middle finger moved to: "
);
Serial
.
println
(
angle
);
}
else
if
(
finger
==
"ring"
)
{
ring_pos
=
angle
;
ring_finger
.
write
(
ring_pos
);
Serial
.
print
(
"Ring finger moved to: "
);
Serial
.
println
(
angle
);
}
else
if
(
finger
==
"pinky"
)
{
pinky_pos
=
angle
;
pinky_finger
.
write
(
pinky_pos
);
Serial
.
print
(
"Pinky finger moved to: "
);
Serial
.
println
(
angle
);
}
else
if
(
finger
==
"all"
)
{
// Move all fingers to the same position
index_pos
=
angle
;
middle_pos
=
angle
;
ring_pos
=
angle
;
pinky_pos
=
angle
;
index_finger
.
write
(
index_pos
);
middle_finger
.
write
(
middle_pos
);
ring_finger
.
write
(
ring_pos
);
pinky_finger
.
write
(
pinky_pos
);
Serial
.
print
(
"All fingers moved to: "
);
Serial
.
println
(
angle
);
}
else
{
Serial
.
print
(
"Unknown finger command: "
);
Serial
.
println
(
finger
);
}
}
else
{
Serial
.
print
(
"Invalid command format. Received: "
);
Serial
.
println
(
command
);
Serial
.
println
(
"Format should be: finger:angle"
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment