Hello, I'm actually assigned to a project "Bionic arm", I'm using 5 emg sensors intended to control 5 servo motors (MG996). So far im stuck with the programming and wiring (circuit)
#include <Servo.h> // Include the Servo library
// Pin Definitions
const int EMG_PIN_1 = A1; // EMG Sensor 1
const int EMG_PIN_2 = A0; // EMG Sensor 2
const int SERVO_PIN_1 = 9; // Servo for Finger 1
const int SERVO_PIN_2 = 6; // Servo for Finger 2
// Threshold Values (Adjust these after calibration)
const int THRESHOLD_1 = 175
; // Threshold for Finger 1 activation
const int THRESHOLD_2 = 200; // Threshold for Finger 2 activation
// Servo objects for each finger
Servo finger1Servo;
Servo finger2Servo;
// Positions for servos (Adjust based on your setup)
const int FINGER_REST_POSITION = 0; // Position when finger is at rest
const int FINGER_ACTIVE_POSITION = 180; // Position when finger is flexed (adjust if needed)
void setup() {
// Initialize serial monitor
Serial.begin(9600);
// Attach the servos to their respective pins
finger1Servo.attach(SERVO_PIN_1);
finger2Servo.attach(SERVO_PIN_2);
// Set servos to rest position initially
finger1Servo.write(FINGER_REST_POSITION);
finger2Servo.write(FINGER_REST_POSITION);
}
void loop() {
// Read values from EMG sensors
int emgValue1 = analogRead(EMG_PIN_1);
int emgValue2 = analogRead(EMG_PIN_2);
// Print EMG values for monitoring
Serial.print("Sensor 1: ");
Serial.print(emgValue1);
Serial.print(" Sensor 2: ");
Serial.println(emgValue2);
// Determine if each finger should be active or at rest
bool finger1Active = emgValue1 > THRESHOLD_1;
bool finger2Active = emgValue2 > THRESHOLD_2;
// Control Finger 1 based on EMG sensor 1
if (finger1Active) {
Serial.println("Finger 1 Activated");
finger1Servo.write(FINGER_ACTIVE_POSITION); // Move Finger 1 to active position
} else {
Serial.println("Finger 1 at Rest");
finger1Servo.write(FINGER_REST_POSITION); // Move Finger 1 to rest position
}
// Control Finger 2 based on EMG sensor 2
if (finger2Active) {
Serial.println("Finger 2 Activated");
finger2Servo.write(FINGER_ACTIVE_POSITION); // Move Finger 2 to active position
} else {
Serial.println("Finger 2 at Rest");
finger2Servo.write(FINGER_REST_POSITION); // Move Finger 2 to rest position
}
// Short delay for stability
delay(100);
}
This is the code im trying to use to calibrate at least 2 fingers. the main issue is only 1 emg is working not at same time. Please help me out I need to be able to calibrate for the 5 fingers. Any help would be much appreciated
ok, tell us how you know 1 emg is working. What happens that indicates working and what indicates not working?
I'm constantly checking the threshold value obtained from the emg sensors (in the serial monitor) while moving my muscles it's either 1 is at rest or the other is activated but both isn't working at the same time. I've ensured that the connection is proper as well. I'm assigned to control 5 servos individually with 5 emg sensors. but for now, I'm blanked
ok, then reading the sensor output gives either 1 or 0. Will a sensor hold a 1 until you read the value or is the 1 something that will appear for just an instant and then go back to zero? A data sheet for the sensor will certainly help in supplying help!
If the signal is digital, 0 or 1, why are you using analog read? A link to the data sheet will also work.
The signal from EMG sensors is typically analog, not digital. EMG sensors measure the muscle's electrical activity, which results in a varying analog voltage.
I have a basic knowledge of the project working that's why I'm seeking help.
And many other projects where they have applied the same principle but instead of controlling the fingers individually, they're just doing open & close palm - They have used the same 'Analog read' - so I was using this as a reference. I require a program which allows me to control each servo motors with separate emg sensors
https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Biometric/Muscle%2520Sensor%2520v3%2520Users%2520Manual.pdf&ved=2ahUKEwj_otiRodeJAxVvha8BHc3QDIAQFnoECBMQAQ&usg=AOvVaw1WuhNed0MEbJlniyUG9VY2
So you chose to use only 0 and 1 out of 1024 possible values from the analog read. Seems a bit irrational, don't you think? What are the ACTUAL values that are being read? And can you pick two that show mussel activity and mussel inactivity?
ok my bad seems like there has been a miscommunication, 1) the signal is analog range from 0-1024 based on your muscle's activation.
2) the values shown when im moving 2 fingers are 175 & 200 respectively but when im using the same values in the code for the threshold value only 1 servo is getting actuated, and the other is at rest. Both are not working at the same time.
If the values read are the same as your threshold values, and your code shows you are looking for values GREATER than the threshold values, then the tests will fail.
So, what should be changed and how would I be able to implement these for the remaining 5 fingers. I need to rewrite the code for all individual finger movements based on the specific readings obtained from their respective emg sensors signal
If you would print the actual analog value right after reading, you would know the values. Right now you are guessing.
How many analog pins does your Arduino have? Did you pick one with enough analog pins to match your project?
the values 175 & 200 are noted from the readings of the emg sensor emitted whenever im moving specific fingers. The Arduino has enough analog pins, The issue is that activation of the motors is not happening simultaneously.
Step 1 is to increase the serial Baud rate to the maximum value possible.
Step 2 is to comment out the serial.Print() statements before commanding the servo movement.
The use of so much serial.Print() is blocking the fast execution of the rest of your code.