UnoJoy + Software Serial & PS2 Library

Hi there,,

I've been stressing alot since it's weird that the microprocessor just crash for random reasons.
Now what I want to build is a Keypad + Mouse in one house as a Joystick.
I used 2 other library's, the Software Serial, and a PS2 Library without scroll stuff.

The software serial is used for a Keypad which outputs serial at 57600 bps, and the keys are outputted like 11-29, value 0-1 >(131 = key 13 is pressed).
The PS/2 library is used for a mouse on PS/2.
and UnoJoy from Google Code Archive - Long-term storage for Google Code Project Hosting.

Now I met serveral problems with it when combinding it all together.. first one, keypad works fine, but the mouse movements freeze after a random 5 seconds. It seems the timer is still running while the void loop has been stopped/crashed.
So I edit some value's, well actually deleting the delaymicroseconds in the ps2.c, right now the mouse is working fine, but whenever I press a key on the keypad, the arduino crashes instantly. Like they are seperated from functions but crashes the loop :/.

If I put very long delays between the keypad readings and mouse readings, then it works halfy. so if I press rapidly on the keys, the arduino crashes after a few seconds.

I hope someone can help me out, and im stll trying things atm.
Here's my code. (yes its a mess, I've sketched from existents example's.)

Main Script:

#include <ps2.h>
#include "UnoJoy.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(16, 17); // RX, TX
PS2 mouse(15, 14);
 //Ints used for keypad
  word inByte;  // I dont know why I used *word* but seems to work, the keypad was programmed on Bascom with Atmega32.
  int key;
  int keyvalue;
  unsigned int keybuffer[30];

 // ints used for mouse
  int mstat;
  int mx;
  int my;

void mouse_init()
{
  mouse.write(0xff);  // reset
  mouse.read();  // ack byte
  mouse.read();  // blank */
  mouse.read();  // blank */
  mouse.write(0xf0);  // remote mode
  mouse.read();  // ack
 
}


void setup(){
pinMode(13, OUTPUT);  // Led output
 mySerial.begin(57600);  // softserial
  mouse_init();  //ps2 library
  setupUnoJoy();
 

}


void loop(){
  
digitalWrite(13, digitalRead(13) ^ 1);   // Activity, if the loop/arduino crashes.. the LED doesnt blink or stays static on.

  // Get keyboard info from SoftwareSerial, because the hardware serial is used by UnoJoy atmega16u2
  if (mySerial.available() > 0) {
    // read the incoming byte
  word inByte = mySerial.read();

 if(inByte > 10 && inByte < 30){ // If it is a key, assign to key.
  // Serial.println("hoi");
   key = inByte;
   mySerial.println(key);   
 }

 inByte = mySerial.read();
 if(inByte < 2){  //if it is a value, assign to a value. aka On/ Off
  keyvalue = inByte;
 }
}

for(int ki=10; ki <= 30;ki++){ //Puts the keypad keys in a array.
  if(key == ki){
  keybuffer[ki] = keyvalue;
  }
} 

 // get mouse info

  mouse.write(0xeb);  // give me data!
  mouse.read();      // ignore ack
  mstat = mouse.read();
  mx = mouse.read();
  my = mouse.read();
  
// Always be getting fresh data
  dataForController_t controllerData = getControllerData();
  setControllerData(controllerData);
  }

dataForController_t getControllerData(void){
  
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  dataForController_t controllerData = getBlankDataForController();
  controllerData.triangleOn = keybuffer[25];
  controllerData.circleOn = keybuffer[15];
  controllerData.squareOn = keybuffer[18];
  controllerData.crossOn = keybuffer[17];
  controllerData.dpadUpOn = keybuffer[23];
  controllerData.dpadDownOn = keybuffer[27];
  controllerData.dpadLeftOn = keybuffer[28];
  controllerData.dpadRightOn = keybuffer[12];
  controllerData.l1On = 0; //muis
  controllerData.r1On = 0; //muis
  controllerData.l2On = keybuffer[11]; 
  controllerData.r2On = keybuffer[13]; 
  controllerData.l3On = keybuffer[29]; 
  controllerData.r3On = keybuffer[26]; 
  controllerData.selectOn = keybuffer[24];
  controllerData.startOn = keybuffer[22];
  controllerData.homeOn = keybuffer[14];
  


  if(keybuffer[16] == 1){
controllerData.leftStickY = 0;
}

if(keybuffer[20] == 1){
  controllerData.leftStickY = 255;
}

if(keybuffer[20] == 0 && keybuffer[16] == 0){
 controllerData.leftStickY = 127;
}
if(keybuffer[16] == 0 && keybuffer[20] == 0){
  controllerData.leftStickY = 127;
}


if(keybuffer[19] == 1){
  controllerData.leftStickX = 0;
}

if(keybuffer[21] == 1){
  controllerData.leftStickX = 255;
}

if(keybuffer[21] == 0 && keybuffer[19] == 0){
  controllerData.leftStickX = 127;
}
if(keybuffer[19] == 0 && keybuffer[21] == 0){
  controllerData.leftStickX = 127;
}


  controllerData.rightStickX = 127 + mx * 4;
  controllerData.rightStickY = 127 + my * 4;
  // And return the data!
  return controllerData;
}

UnoJoy.h is left alone. no edits ~.
SoftwareSerial is not edited, and I only removed the delaymicroseconds at the ps2 library. (I know it shouldnt be deleted but my mouse still works for somereasons)

I'm not really super skilled in C yet, but i'm a learning person so I hope to get some advice and tips ~

Thanks.
Dnstje

I've dissected some codes.. and it's SoftwareSerial cant work with PS/2 mouse library..
Can somebody tell me why?

I've dissected some codes.. and it's SoftwareSerial cant work with PS/2 mouse library..
Can somebody tell me why?

You came to the conclusion that they don't work together. Why?

Well, I don't know yet why those two doesnt work. right now I 'm using two arduino's for my project to archive this >_>.
I have the feeling one of them is changing a timer, atleast I read that software serial uses a interrupt, while ps/2 lib uses alot of delayMicroseconds. I think the problem might be in there, but im not sure.

Now i've just added a second arduino, one to merge the keypad and mouse to serial, > sends over to the 2nd arduino with UnoJoy on it, that arduino translates it to joystick value's to my pc.
expensive way but ok.

I'm not that skilled with programming yet, though it can be done with a single arduino.

Also when coding the serial stuff.. seems like Serial.write(123); is broken in Arduino 1.0.1 ?
I just want to send a byte with the value 123, but when receiving that byte, it just gave me a random value..

int inByte = Serial.read();
lcd.print(inByte);

using this gave me weird value's. unless I used Arduino 021.
But this is already solved for me, using an older version of arduino ide.

I just want to send a byte with the value 123, but when receiving that byte, it just gave me a random value..

int inByte = Serial.read();
lcd.print(inByte);

using this gave me weird value's. unless I used Arduino 021.

Serial.read() returns an int, with the error value in the high order byte, and the data in the low order byte. If you know that the Serial.read() function will not return an error, and you should because you have called Serial.available() and are only calling Serial.read() when there is something to read, then you should be storing the result in a byte sized variable, not a multibyte variable.