Go Down

Topic: Joystick Mouse Control issue (Read 1 time) previous topic - next topic

freerpg

http://arduino.cc/en/Tutorial/JoystickMouseControl
thats the tutorial but when I compile the program I get errors

sketch_feb20a.ino: In function 'void setup()':
sketch_feb20a:51: error: 'Mouse' was not declared in this scope
sketch_feb20a.ino: In function 'void loop()':
sketch_feb20a:74: error: 'Mouse' was not declared in this scope
sketch_feb20a:81: error: 'Mouse' was not declared in this scope
sketch_feb20a:81: error: 'MOUSE_LEFT' was not declared in this scope
sketch_feb20a:88: error: 'Mouse' was not declared in this scope
sketch_feb20a:88: error: 'MOUSE_LEFT' was not declared in this scope


so what am I missing? is there a library I can't see? or is there something missing in the code?

zoomkat

Quote
so what am I missing? is there a library I can't see? or is there something missing in the code?


Per your link, do you have this library?

Quote
Using the Mouse library, you can controls...


Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Quick5pnt0

Am I correct in assuming you're using a Leonardo?

freerpg

no I just compiled it no arduino board connected, and where is the library?

PaulS

Quote
no I just compiled it no arduino board connected, and where is the library?

Which Arduino? There are over a dozen models.

From: http://arduino.cc/en/Tutorial/JoystickMouseControl
Quote
Hardware Required

    Arduino Leonardo, Micro, or Due board

If you have one of them, you have the Mouse library. If you don't, you still have the library. It's just not included.

And, if you don't have one of them, the Arduino that you have won't be able to output the correct information to do anything.

Hi everyone,

I am trying to make my joystick mouse completely functioning (normal click, drag, etc.). As of now I am stuck with how to allow it to differentiate between a regular click and a drag click (I'm using variable called: var). Here's my program (it started as the example on the website):

  /*
  JoystickMouseControl

Controls the mouse from a joystick on an Arduino Leonardo or Micro.
Uses a pushbutton to turn on and off mouse control, and
a second pushbutton to click the left mouse button

Hardware:
* 2-axis joystick connected to pins A0 and A1
* pushbuttons connected to pin D2 and D3

The mouse movement is always relative. This sketch reads
two analog inputs that range from 0 to 1023 (or less on either end)
and translates them into ranges of -6 to 6.
The sketch assumes that the joystick resting values are around the
middle of the range, but that they vary within a threshold.

WARNING:  When you use the Mouse.move() command, the Arduino takes
over your mouse!  Make sure you have control before you use the command.
This sketch includes a pushbutton to toggle the mouse control state, so
you can turn on and off mouse control.

created 15 Sept 2011
updated 28 Mar 2012
by Tom Igoe

this code is in the public domain

*/

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 5;      // switch to turn on and off mouse control
const int selPin = 4;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis 
const int yAxis = A1;         // joystick Y axis
const int ledPin = 7;         // Mouse control LED

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state

int var = 1;
int varstate = digitalRead(selPin);

void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);  // the LED pin
  pinMode(selPin, INPUT);
// take control of the mouse:
  digitalWrite(selPin, HIGH);
  Mouse.begin();
  Serial.begin(9600);
}

void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    }
  }
  // save switch state for next comparison:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, -yReading, 0);
   } 
 
  int varstate = digitalRead(selPin);
  for(int i = 1; varstate == LOW; i++) {
    var++;
    varstate = digitalRead(selPin);

    if (varstate == HIGH && var > 1 && var < 150) {
    Mouse.click(MOUSE_LEFT);
    var = 1;
     }
     
     while(var > 150 && varstate == LOW) {
       Mouse.press();
       var++;
       varstate = digitalRead(varstate); 
     }
     Mouse.release();
     var = 1;
  }
  Serial.println (var);
    delay(10);
}
   

int readAxis(int thisAxis) {
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  }

  // return the distance for this axis:
  return distance;
}

*************************************************************************************************************************************************************************

Let me know if you have any ideas or answers.

Thanks in advance!


PaulS

Quote
Let me know if you have any ideas

You could learn to post code correctly. There are two interesting stickies at the top of this forum that should be required reading.

#7
May 07, 2013, 10:15 pm Last Edit: May 07, 2013, 10:18 pm by AWOL Reason: 1

Quote
Let me know if you have any ideas

You could learn to post code correctly. There are two interesting stickies at the top of this forum that should be required reading.


Moderator edit. Removed offensive content.  Please, read the stickies.

And here's my code:
Code: [Select]
 /*
 JoystickMouseControl

Controls the mouse from a joystick on an Arduino Leonardo or Micro.
Uses a pushbutton to turn on and off mouse control, and
a second pushbutton to click the left mouse button

Hardware:
* 2-axis joystick connected to pins A0 and A1
* pushbuttons connected to pin D2 and D3

The mouse movement is always relative. This sketch reads
two analog inputs that range from 0 to 1023 (or less on either end)
and translates them into ranges of -6 to 6.
The sketch assumes that the joystick resting values are around the
middle of the range, but that they vary within a threshold.

WARNING:  When you use the Mouse.move() command, the Arduino takes
over your mouse!  Make sure you have control before you use the command.
This sketch includes a pushbutton to toggle the mouse control state, so
you can turn on and off mouse control.

created 15 Sept 2011
updated 28 Mar 2012
by Tom Igoe

this code is in the public domain

*/

// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 5;      // switch to turn on and off mouse control
const int selPin = 4;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis  
const int yAxis = A1;         // joystick Y axis
const int ledPin = 7;         // Mouse control LED

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state

int var = 1;
int varstate = digitalRead(selPin);

void setup() {
 pinMode(switchPin, INPUT);       // the switch pin
 pinMode(ledPin, OUTPUT);  // the LED pin
 pinMode(selPin, INPUT);
// take control of the mouse:
 digitalWrite(selPin, HIGH);
 Mouse.begin();
 Serial.begin(9600);
}

void loop() {
 // read the switch:
 int switchState = digitalRead(switchPin);
 // if it's changed and it's high, toggle the mouse state:
 if (switchState != lastSwitchState) {
   if (switchState == HIGH) {
     mouseIsActive = !mouseIsActive;
     // turn on LED to indicate mouse state:
     digitalWrite(ledPin, mouseIsActive);
   }
 }
 // save switch state for next comparison:
 lastSwitchState = switchState;

 // read and scale the two axes:
 int xReading = readAxis(A0);
 int yReading = readAxis(A1);

 // if the mouse control state is active, move the mouse:
 if (mouseIsActive) {
   Mouse.move(xReading, -yReading, 0);
  }  
 
 int varstate = digitalRead(selPin);
 for(int i = 1; varstate == LOW; i++) {
   var++;
   varstate = digitalRead(selPin);

   if (varstate == HIGH && var > 1 && var < 150) {
   Mouse.click(MOUSE_LEFT);
   var = 1;
    }
   
    while(var > 150 && varstate == LOW) {
      Mouse.press();
      var++;
      varstate = digitalRead(varstate);  
    }
    Mouse.release();
    var = 1;
 }
 Serial.println (var);
   delay(10);
}
 

int readAxis(int thisAxis) {
 // read the analog input:
 int reading = analogRead(thisAxis);

 // map the reading from the analog input range to the output range:
 reading = map(reading, 0, 1023, 0, range);

 // if the output reading is outside from the
 // rest position threshold,  use it:
 int distance = reading - center;

 if (abs(distance) < threshold) {
   distance = 0;
 }

 // return the distance for this axis:
 return distance;
}





PaulS

Quote
As of now I am stuck with how to allow it to differentiate between a regular click and a drag click

Perhaps you should start by explaining what a "regular click" is, and what a "drag click" is. Perhaps we know the actions under different names.

"regular" as in LEFT click

"drag click" as in LEFT "click" then "drag" the mouse to highlight...

tobyb121

What is this supposed to be doing?
Code: [Select]

while(var > 150 && varstate == LOW) {
       Mouse.press();
       var++;
       varstate = digitalRead(varstate); 
}

You assign varstate to the pin state of the pin varstate? in a loop?! so next time you are reading the state of pin 0 or 1, depending on what they read last time???

When you say you don't know how to differentiate between a click and a click+drag, that should be up to your pc to decide, Mouse.click() just calls Mouse.press then Mouse.release immediately afterwards. You should just call mouse.press when your button goes low, and mouse.release when your button goes high.


Go Up