Mouse Code issue with Teensy 2.0

My issue is that i have a code that should work with my teensy 2.0 board and a 2 axis analog joystick. Here is my code.

const int switchPin = 5;      // switch to turn on and off mouse control
const int mouseButton = B0;    // input pin for the mouse pushButton
const int xAxis = A1;         // joystick X axis  
const int yAxis = A0;         // joystick Y axis
const int ledPin = 11;         // 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

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

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);
  }  

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT); 
    }
  } 
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT); 
    }
  }

  delay(responseDelay);
}

/*
  reads an axis (0 or 1 for x or y) and scales the 
 analog input range to a range from 0 to <range>
 */

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;
}

20130621_151450.jpg

And now with code tags?

You'd be a lot better off posting in a forum at PJRC (Teensy) Forum
and when/if you do, use code tags there too.

Pete

AWOL:
And now with code tags?

Time for an intelligent scanner to block these posts if it detects code :grin:

what is a code tag?

You mean you didn't read this? :astonished:

Better?!

My issue is that i have a code that should work with my teensy 2.0 board and a 2 axis analog joystick. Here is my code.

What the hell does this mean? "I have some code that I think should work, but it doesn't?" If that is the case, explaining what the code actually does, and how that differs from what you expect is going to be necessary before anyone does more than laugh at you.

What this is doing is moving my cursor into the upper left corner! Happy?

What this is doing is moving my cursor into the upper left corner! Happy?

Ecstatic. What do your serial prints show you is happening? Oh, wait. You don't have any. Why not?

Because i am basically a newbie. and i need guidance on how to do some of this. So can i please have your help???

Well now my cursor moves diagonally up left when i push down and diagonally down right when i push up. When i push left is acts as a left click and right does nothing.

Well now my cursor moves diagonally up left when i push down and diagonally down right when i push up. When i push left is acts as a left click and right does nothing.

"I've modified the code. I'm not going to show you the code. I'm not going to show you the serial output. I still expect you to tell me what is wrong."

We've hired a psychic. His start date is the 12th of never. If you can't wait until then, I bet you can figure out what you need to do.

Why do you think i posted it here in the first place. Oh wait, because I don't know what is wrong!

because I don't know what is wrong!

We can't see your code.
We can't see your serial output.

Hopefully, the psychic will be able to help you. I have a feeling he's going to be swamped for a while, though.

is the
int range = 12
wrong?

is the
Code:

int range = 12

wrong?

Yes.

So what do i change it to?

So what do i change it to?

I suggest that you get a sledge hammer. Pound the Teensy until it is all smashed up. Your problems with it will be over.

Quit playing games. When we say post your code, we do NOT mean half of one line.

My whole code. i do not know where to put the Serial.println.

const int switchPin = 5; // switch to turn on and off mouse control
const int mouseButton = B0; // input pin for the mouse pushButton
const int xAxis = A1; // joystick X axis
const int yAxis = A0; // joystick Y axis
const int ledPin = 11; // 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

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

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);
}

// read the mouse button and click or not click:
// if the mouse button is pressed:
if (digitalRead(mouseButton) == HIGH) {
// if the mouse is not pressed, press it:
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
// else the mouse button is not pressed:
else {
// if the mouse is pressed, release it:
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}

delay(responseDelay);
}

/*
reads an axis (0 or 1 for x or y) and scales the
analog input range to a range from 0 to
*/

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;
}

so is the stuff in red wrong?