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