PS2 mouse and weak led

I am trying to light up leds in a row with PS2 mouse.But leds light up very weak and sometimes arduino freezes.What am i missing?

#define WProgram.h Arduino.h
#include <ps2.h>
const int dataPin = 5;
const int clockPin = 6;
const int mouseRange = 2500; // the maximum range of x/y values

char x; // values read from the mouse
char y;
byte status;
int xPosition = 0; // values incremented and decremented when mouse moves
int yPosition = 0;
const byte REQUEST_DATA = 0xeb; // command to get data from the mouse
PS2 mouse(clockPin, dataPin);
void setup()
{
mouseBegin();
}
void loop()
{
// get a reading from the mouse
mouse.write(REQUEST_DATA); // ask the mouse for data
mouse.read(); // ignore ack
status = mouse.read(); // read the mouse buttons
if(status & 1) // this bit is set if the left mouse btn pressed
xPosition = 0; // center the mouse x position
if(status & 2) // this bit is set if the right mouse btn pressed
yPosition = 0; // center the mouse y position
x = mouse.read();
y = mouse.read();
if( x != 0 || y != 0)
{
// here if there is mouse movement
xPosition = xPosition + x; // accumulate the position
xPosition = constrain(xPosition,-mouseRange,mouseRange);
if( (0<=xPosition) && (xPosition<=500))
{
digitalWrite (2,HIGH);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (7,LOW);
}
if( (501<=xPosition) && (xPosition<=1000))
{
digitalWrite (3,HIGH);
digitalWrite (2,LOW);
digitalWrite (4,LOW);
digitalWrite (7,LOW);
}
if( (1001<=xPosition) && (xPosition<=1500))
{
digitalWrite (4,HIGH);
digitalWrite (3,LOW);
digitalWrite (2,LOW);
digitalWrite (7,LOW);
}
if( (1501<=xPosition) && (xPosition<=2000))
{
digitalWrite (7,HIGH);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
}

yPosition = constrain(yPosition + y, -mouseRange,mouseRange);
if( (0<yPosition) && (yPosition<500))
{
digitalWrite (8,HIGH);
digitalWrite (9,LOW);
digitalWrite (10,LOW);
digitalWrite (11,LOW);
}
if( (501<yPosition) && (yPosition<1000))
{
digitalWrite (9,HIGH);
digitalWrite (8,LOW);
digitalWrite (10,LOW);
digitalWrite (11,LOW);
}
if( (1001<yPosition) && (yPosition<1500))
{
digitalWrite (10,HIGH);
digitalWrite (8,LOW);
digitalWrite (9,LOW);
digitalWrite (11,LOW);
}
if( (1501<yPosition) && (yPosition<2000))
{
digitalWrite (11,HIGH);
digitalWrite (10,LOW);
digitalWrite (9,LOW);
digitalWrite (8,LOW);
}
}
}
void mouseBegin()
{
// reset and initialize the mouse
mouse.write(0xff); // reset
delayMicroseconds(100);
mouse.read(); // ack byte
mouse.read(); // blank
mouse.read(); // blank

mouse.write(0xf0); // remote mode
mouse.read(); // ack
delayMicroseconds(100);
}

Hi,
I don't know why your arduino freezes, but you should use pinMode in your setup()-routine.
If you don't set it, leds indeed burn very weak.

void setup() {
  // set the led pins as output:
  pinMode (2,OUTPUT);
  pinMode (3,OUTPUT);
  pinMode (4,OUTPUT);
  pinMode (7,OUTPUT);
  mouseBegin();
}

Thank you.

Updated code:

#define WProgram.h Arduino.h
#include <ps2.h>
const int dataPin = 5;
const int clockPin = 6;
const int mouseRange = 2500; // the maximum range of x/y values

char x; // values read from the mouse
char y;
byte status;
int xPosition = 0; // values incremented and decremented when mouse moves
int yPosition = 0;
const byte REQUEST_DATA = 0xeb; // command to get data from the mouse
PS2 mouse(clockPin, dataPin);
void setup()
{
  pinMode (2,OUTPUT);
   pinMode (3,OUTPUT);
    pinMode (4,OUTPUT);
     pinMode (7,OUTPUT);
      pinMode (8,OUTPUT);
       pinMode (9,OUTPUT);
        pinMode (10,OUTPUT);
         pinMode (11,OUTPUT);
  
mouseBegin();
}
void loop()
{
// get a reading from the mouse
mouse.write(REQUEST_DATA); // ask the mouse for data
mouse.read(); // ignore ack
status = mouse.read(); // read the mouse buttons
if(status & 1) // this bit is set if the left mouse btn pressed
xPosition = 0; // center the mouse x position
if(status & 2) // this bit is set if the right mouse btn pressed
yPosition = 0; // center the mouse y position
x = mouse.read();
y = mouse.read();
if( x != 0 || y != 0)
{
// here if there is mouse movement
xPosition = xPosition + x; // accumulate the position
xPosition = constrain(xPosition,-mouseRange,mouseRange);
if( (0<=xPosition) && (xPosition<=500))
{
digitalWrite (2,HIGH);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
digitalWrite (7,LOW);
}
if( (501<=xPosition) && (xPosition<=1000))
{
digitalWrite (3,HIGH);
digitalWrite (2,LOW);
digitalWrite (4,LOW);
digitalWrite (7,LOW);
}
if( (1001<=xPosition) && (xPosition<=1500))
{
digitalWrite (4,HIGH);
digitalWrite (3,LOW);
digitalWrite (2,LOW);
digitalWrite (7,LOW);
}
if( (1501<=xPosition) && (xPosition<=2000))
{
digitalWrite (7,HIGH);
digitalWrite (2,LOW);
digitalWrite (3,LOW);
digitalWrite (4,LOW);
}

yPosition = constrain(yPosition + y, -mouseRange,mouseRange);
if( (0<yPosition) && (yPosition<500))
{
digitalWrite (8,HIGH);
digitalWrite (9,LOW);
digitalWrite (10,LOW);
digitalWrite (11,LOW);
}
if( (501<yPosition) && (yPosition<1000))
{
digitalWrite (9,HIGH);
digitalWrite (8,LOW);
digitalWrite (10,LOW);
digitalWrite (11,LOW);
}
if( (1001<yPosition) && (yPosition<1500))
{
digitalWrite (10,HIGH);
digitalWrite (8,LOW);
digitalWrite (9,LOW);
digitalWrite (11,LOW);
}
if( (1501<yPosition) && (yPosition<2000))
{
digitalWrite (11,HIGH);
digitalWrite (10,LOW);
digitalWrite (9,LOW);
digitalWrite (8,LOW);
}
}
}
void mouseBegin()
{
// reset and initialize the mouse
mouse.write(0xff); // reset
delayMicroseconds(100);
mouse.read(); // ack byte
mouse.read(); // blank
mouse.read(); // blank

mouse.write(0xf0); // remote mode
mouse.read(); // ack
delayMicroseconds(100);
}

Leds are bright now but arduino still freezes.Sometimes in three seconds,sometimes in three minutes.It often freezes when I don’t touch mouse for 30 seconds.Any ideas?

The Arduino doesn't lock up. At least, it very (very) rarely does. Even when it appears to "lock up" it is still executing code, just unexpected code.

Your code is likely getting stuck somewhere you don't expect. You should use Serial.print() or flash the LED on Pin13 to debug the code. For example, printing out the x,y coordinates after you receive them. This will allow you to trace the code to where it appears to be getting stuck.

You would do yourself a HUGE favor if you use the "Auto-Format" command in the Tools menu. It makes your code readable.

Weird, after Auto-Format process,everything seems OK.

Auto-formatting wouldn’t fix anything. The compiler doesn’t care how the code is formatted.

Either the problem really wasn’t a problem or you changed something minor without realizing it.

Glad it’s working now.