Notifications
Clear all

MyoWare EMG sensor computer mouse clicking

57 Posts
4 Users
9 Likes
3,536 Views
(@gusmill33)
Member
Joined: 2 years ago
Posts: 20
Topic starter  

I am using various components to create an accelerometer based mouse that uses a myoware emg muscle sensor to click. I am needing the code to read a range from the muscle sensor to corespond to left and right slicking. The code just needs to be edited for this change. I would like the left click value to be 300-500 and the right click value to be 600-1000. Could someone help with this? code placed below. 

The only code that needs to be edited involves the clickig aspect. The acceleromter cursor movement works fine. 

 

#include <Wire.h>
#include <MPU6050.h>
#include <Mouse.h>



MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int vx, vy;
int button1 = 6;
int button2 = 7;
int buttonState1 = 0; 
int buttonState2 = 0;



void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Wire.begin();
Mouse.begin();
// For versions prior to Arduino 1.0.1
// pinMode(buttonPin, INPUT);
// digitalWrite(buttonPin, HIGH);
// put your setup code here, to run once:
pinMode(button1, INPUT);
digitalWrite(button1, HIGH);
pinMode(button2, INPUT);
digitalWrite(button2, HIGH);
mpu.initialize();
if (!mpu.testConnection()) { while (1); }
}
void loop() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

vx = -(gx+150)/150; 
vy = (gy-150)/150;

Serial.print("gx = ");
Serial.print(gx);
Serial.print(" | gy = ");
Serial.print(gy);

Serial.print(" | X = ");
Serial.print(vx);
Serial.print(" | Y = ");
Serial.println(vy);

Mouse.move(vx, vy);

attachInterrupt(1, keyboard, CHANGE);
attachInterrupt(7,fire,HIGH);
if (digitalRead(button1) == HIGH) {
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else {
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
if (digitalRead(button2) == HIGH) {
if (!Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.press(MOUSE_RIGHT);
}
}
else {
if (Mouse.isPressed(MOUSE_RIGHT)) {
Mouse.release(MOUSE_RIGHT);
}
}
delay(20);
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(button1);
Serial.print("X: ");
Serial.print(vx);


Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);

// delay(100); // add some delay between reads


}
void keyboard()
{
vx = analogRead(vx);
vy = analogRead(vy);
buttonState1 = digitalRead(button1);
Serial.print("X: ");
Serial.print(vx);


Serial.print(" | Y: ");
Serial.print(vy);
Serial.print(" | Button: ");
Serial.println(buttonState1);

//delay(100); // add some delay between reads

}
void fire()
{

}

   
Quote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @gusmill33

I am using various components to create an accelerometer based mouse that uses a myoware emg muscle sensor to click. I am needing the code to read a range from the muscle sensor to corespond to left and right slicking. The code just needs to be edited for this change. I would like the left click value to be 300-500 and the right click value to be 600-1000. Could someone help with this? code placed below. 

The only code that needs to be edited involves the clickig aspect. The acceleromter cursor movement works fine. 

Sorry, but you can't sense the PC's mouse movement or click status from the Arduino using the serial port.

You'd need to modify the PC program to sense the mouse click and then send some special character(s) to the Arduino via the serial port.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7025
 

@will Are you sure, he is using one of those 6 axis MPU units.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander

@will Are you sure, he is using one of those 6 axis MPU units.

No, I'm not sure because it sounds like he's expecting to read the PC's mouse and use it from the Arduino, but I may be interpreting it backwards and he's actually trying to send it from the Arduino to the PC. That's what the <Mouse.h> library does.

Need more detailed information, especially about where the left and right click "range" needs to go.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7025
 

@will I had a quick look but it's bad enough reading someone else's code and doubly bad if not properly formatted. I believe he is building a mouse using the MPU6050 as his accelerometer. Check their webpage at https://invensense.tdk.com/products/motion-tracking/6-axis/mpu-6050/

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@zander 

OK, if he's building a mouse to send data back to the PC, it's feasible.

But I still can't figure out the part about " I am needing the code to read a range from the muscle sensor to corespond to left and right slicking. The code just needs to be edited for this change. I would like the left click value to be 300-500 and the right click value to be 600-1000."

This doesn't convey enough information for me to see exactly what he wants to do. Which value is he wanting to be reading to test against the ranges specified ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@gusmill33)
Member
Joined: 2 years ago
Posts: 20
Topic starter  

@will So I would like to read the values outputed by the MyoWare EMG sensor and translate them into either a left or right click. If its possible I would like to have the values 300-500 translated to a left click and the values 600-1000 translated to a right click. Does this make sense or help?


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @gusmill33

@will So I would like to read the values outputed by the MyoWare EMG sensor and translate them into either a left or right click.

Which values, (&ax, &ay, &az, &gx, &gy, &gz) 

If its possible I would like to have the values 300-500 translated to a left click and the values 600-1000 translated to a right click. Does this make sense or help?

Can you understand that "translated into" has no meaning to the readers of the post, only to you. You need to tell us exactly what has to happen in each case so that we can tell the Arduino exactly what to do in each case.

For instance, you're currently determining these values by using pins button1 and button2. Does this replace those or work independently ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7025
 

@gusmill33 Am I misunderstanding, it's as you said, 

if (reading is between 300 anbd 500) then rt click, else left click

or you could use an elif construct to test for the 600 to 1000 but beware of boundary conditions.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7025
 

@will I have no idea what slicking is.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @zander FYI @gusmill33

@will I have no idea what slicking is.

Sorry, it's a typo - should be CLICKING 🙂

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
(@gusmill33)
Member
Joined: 2 years ago
Posts: 20
Topic starter  

@will So I am removing button1 and button2 and replacing it with the muscle sensor. So then I could have something like, int SENSOR_PIN = A0; instead. Then, if the muscle sensors incomming values are anywhere from 300-500 there is a corresponding "left click' of the mouse and th same for the right click with values 600-1000. 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7025
 

@gusmill33 Sounds like you got it.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@gusmill33)
Member
Joined: 2 years ago
Posts: 20
Topic starter  

@zander I am still confused on how to have the values be read then to then click. I think I understand generally what to do I just don’t know how TO do it.

This post was modified 2 years ago by GusMill33

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@gusmill33 

I assume that you'll be polling the sensor every time through the loop. When the test value enters the critical region for either left or right button, how many times do you need to send the clicking to the PC ? For instance, if the loop is traversed every 1 ms, it's likely that the sensor value will be the same, but I'm wondering if you would send another click or not.

If not, under what conditions would you send another click. For example, after 5 ms; only after test value leaves either critical range; only after test values enters the other critical range, etc.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
Page 1 / 4