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