Go Down

Topic: Stopping the mouse program example? (Read 2963 times) previous topic - next topic

caustictoast

I used the mouse example given on the Esplora page and now when I try to upload a new program the IDE gives me "COM6 is in use. Try quitting all programs before uploading" or something of the like. If it helps for some reason my joystick has both the x- and y-axis stuck at a value of 12.

Coding Badly


Does this work... unplug the Esplora from the computer, plug the Esplora back into the computer, immediately try to upload.

ThisIsMyName

From http://arduino.cc/en/Tutorial/EsploraJoystickMouse

This sketch will take over the mouse movement of your computer. If you lose control of your cursor do the following :
- unplug the Esplora
- open the EsploraBlink sketch in the Arduino software
- hold the Esplora's reset button down while plugging it back in to your computer
- while continuing to hold the reset button, click "Upload" in the Arduino software
- when you see the message "Done compiling" in the Arduino IDE, release the reset button

Acererak

#3
Dec 27, 2012, 11:12 pm Last Edit: Dec 28, 2012, 12:46 am by Acererak Reason: 1
For some reason by default my joystick is at x-8 y-1 while in center and when I run this example my mouse is constantly moving up and to the right.  Is there a way to calibrate the joystick to zero?

I've worked around it by modifying the code:
Code: [Select]
 int xValue = Esplora.readJoystickX()+8;        // read the joystick's X position
 int yValue = Esplora.readJoystickY()+1;        // read the joystick's Y position


I've now added functionality for this and fixed this problem:
Code: [Select]
#include <Esplora.h>

int initx,inity;                                 //declares global variables for inital x/y values

void setup()
{
 Serial.begin(9600);                            // initialize serial communication with your computer
 Mouse.begin();                                 // take control of the mouse
 initx = Esplora.readJoystickX();               //read initial value for x
 inity = Esplora.readJoystickY();               //read initial value for y
}

void loop()
{
 int xValue = Esplora.readJoystickX() - initx;  // read the joystick's X position - initial value
 int yValue = Esplora.readJoystickY() - inity;  // read the joystick's Y position - initial value
 int button = Esplora.readJoystickSwitch();     // read the joystick pushbutton
 int sw1 = Esplora.readButton(SWITCH_DOWN);     // read switch 1 state
 int sw4 = Esplora.readButton(SWITCH_RIGHT);    // read switch 4 state
 Serial.print("Joystick X: ");                  // print a label for the X value
 Serial.print(xValue);                          // print the X value
 Serial.print("\tY: ");                         // print a tab character and a label for the Y value
 Serial.print(yValue);                          // print the Y value
 Serial.print("\tStick Button: ");              // print a tab character and a label for the button
 Serial.print(button);                          // print the button value
 Serial.println();                              // create a new line
 Serial.print("Switch 1: ");                    // print a label for switch 1 value
 Serial.print(sw1);                             // print the switch 1 value
 Serial.print("\tSwitch 4: ");                  // print a tab character and a label for switch 4 value
 Serial.print(sw4);                             // print the switch 4 value
 Serial.println();                              // create a new line
 
 int mouseX = map( xValue,-512, 512, 10, -10);  // map the X value to a range of movement for the mouse X
 int mouseY = map( yValue,-512, 512, -10, 10);  // map the Y value to a range of movement for the mouse Y
 
 if((Esplora.readButton(SWITCH_DOWN) == LOW)  
     || (Esplora.readJoystickButton() == LOW)){  // checks to see if switch 1 or joystick button is pressed
   Mouse.press(MOUSE_LEFT);                     // holds down left mouse button
 }
 else{
   Mouse.release(MOUSE_LEFT);                   // releases left mouse button when not pressed
 }
 
 if(Esplora.readButton(SWITCH_RIGHT) == LOW){   // checks to see if switch 4 is pressed
 Mouse.press(MOUSE_RIGHT);                      // holds down right mouse button
 }
 else{
   Mouse.release(MOUSE_RIGHT);                  // releases right mouse button when not pressed
 }
 
 Mouse.move(mouseX, mouseY, 0);                 // move the mouse
 
 delay(10);                                     // a short delay before moving again
}

Go Up