Test wiring

After Running the code on my Teensy from Post Number Two, I moved onto mounting the buttons and joystick in a box,

My Test device has nine buttons; rather than the severn that the finished product will have; plus the joystick’s four; but this allows me to tryout other configurations of buttons etc without having to move buttons round.

One of the problems I think I may face is that I will have 13 different switches, the finished product will have 11 including the Joysticks four.

As such I am planning on using a key matrix, to reduce my requirements from 13 inputs, to 8 data lines (Four inputs, 4 outputs). and using diodes to remove the ‘masking problem’/ghosting. also this will allow me to have multiple key presses.

I found this site that discuses how this should work. but the easiest two images to quickly show this are these two, one showing the problem, and the other showing the solution using Diodes.

Howkeymatricesworks 1277650492Howkeymatricesworks 1277655319

Accessible Mouse

Here is my Crocodile Clips drawing (doesn’t have auto route) [Accessible Mouse.cyt] but shows the layout of the switches and diodes (the three toggle switches will have diodes as well).

Letters; A,B,C,D, are to be the outputs from the Teensy, with the Numbers; 1,2,3,4 being the inputs to the Teensy, this would allow a maximum of 16 switches, with every switch having an unique address (there would be ways to map more switches to the same number of lines; but then you would have to be careful about multiple button presses again).

The Three unused lines will be available for external buttons; so that there is expansion available.

Another thing I will be doing is putting LEDs in some of the buttons; but rather than using one pin per LED; I can also use a matrix to allow me to reduce the number of pins used, I don’t know what the current limiting resistors value should be in this, using the normal value means that the LEDs light output will be reduced.

For the full details of Ghosting and Masking works please look at the original site.

I have managed to get my switches mounted in a test box, with the diodes soldered on,

Microswitches before Wires are added, showing each switch’s diode.

The wiring is two colour; White, and Black (White is the letters, and Black is the Numbers)

With all the wires attached, I now plugged it into the Teensy, and marked up the switches

Flashing the Teensy with the code below:

/*
	Affordable Accessible Mouse

	For this to work the board must be a Teensy 2.0

	If the code fails to Verify or Upload check this first; As I
	get more experience and hardware configurations I may add a
	working define line for hardware type

	See the file "Read Me" at http://git.subvert.org.uk/skippy/Affordable_Accessible_Mouse.git/tree/Read%20Me.txt
	for the full license governing this code, and any other things that may be of interest.
*/

	// Mouse Speed and Scroll Speed
	// Number between 0 and something bigger,
		#define MouseSpeed 3
		#define ScrollSpeed 3

	// Define Letters to Pins
		#define OutputA 0
		#define OutputB 1
		#define OutputC 2
		#define OutputD 3

	// Define Numbers to Pins
		#define Input1 7
		#define Input2 8
		#define Input3 9
		#define Input4 10

	// Set up feedback LEDs
		#define PCBLED 11	//PCB LED

void setup()
{
	// initialize the four pins for the Outputs
		pinMode(OutputA, OUTPUT);
		pinMode(OutputB, OUTPUT);
		pinMode(OutputC, OUTPUT);
		pinMode(OutputD, OUTPUT);

	// initialize the four pins for the Inputs
		pinMode(Input1, INPUT);
		pinMode(Input2, INPUT);
		pinMode(Input3, INPUT);
		pinMode(Input4, INPUT);

	// initialize the Pins for the LEDs
		pinMode(PCBLED, OUTPUT);

	// Allows me to use the Serial Console to debug
		Serial.begin(38400);
}

void loop()
{
	// All the Physical Buttons on the Pannel.
		int North = 0;						// North is A1
		int East = 0;						// East is A2
		int South = 0;						// South is A3
		int West = 0;						// West is A4

		int LC1 = 0;						// LeftClick1 is B1
		int LC2 = 0;						// LeftClick2 is B3

		int RC1 = 0;						// RightClick1 is B2
		int RC2 = 0;						// RightClick2 is B4

		int DC = 0;							// DoubleClick is C1
		int CL = 0;							// ClickLock is C2
		int SL = 0;							// ScrollLock is C3

	// These are not going to be buttons on the finished product; however they may be
	// attached to external switches to allow for further expansion.
		int A = 0;							// OptionalButtonA is C4
		int B = 0;							// OptionalButtonB is D1
		int C = 0;							// OptionalButtonC is D2 - Not Connected
		int D = 0;							// OptionalButtonD is D3 - Not Connected
		int E = 0;							// OptionalButtonE is D4 - Not Connected

	// Mouse and Scroll Speed.
		int Rate = MouseSpeed +1 ;			// reads in Speed from top and uses that as mouse speed.
											// the +1 is to stop the if speed = 0 well… FAIL
		int X = 0;
		int Y = 0;

	// Read in All inputs
		digitalWrite(PCBLED, HIGH);			// set the LED On
		digitalWrite(OutputA, HIGH);		// Set A High
		delay(1);							// seems to be needed to allow switches 1 to work
		if (digitalRead(Input1) == HIGH)
		{
			North = 1;
			Serial.println("North");
		}
		if (digitalRead(Input2) == HIGH)
		{
			East = 1;
			Serial.println("East");
		}
		if (digitalRead(Input3) == HIGH)
		{
			South = 1;
			Serial.println("South");
		}
		if (digitalRead(Input4) == HIGH)
		{
			West = 1;
			Serial.println("West");
		}
		digitalWrite(OutputA, LOW);			// set A Low
		digitalWrite(OutputB, HIGH);		// set B High
		delay(1);
		if (digitalRead(Input1) == HIGH)
		{
			LC1 = 1;
			Serial.println("LeftClick1");
		}
		if (digitalRead(Input2) == HIGH)
		{
			RC1 = 1;
			Serial.println("RightClick1");
		}
		if (digitalRead(Input3) == HIGH)
		{
			LC2 = 1;
			Serial.println("LeftClick2");
		}
		if (digitalRead(Input4) == HIGH)
			{
			RC2 = 1;
			Serial.println("RightClick2");
		}
		digitalWrite(OutputB, LOW);			// set B Low
		digitalWrite(PCBLED, LOW);			// set the LED off
		digitalWrite(OutputC, HIGH);		// set C High
		delay(1);
		if (digitalRead(Input1) == HIGH)
		{
			DC = 1;
			Serial.println("DoubleClick");
		}
		if (digitalRead(Input2) == HIGH)
		{
			CL = 1;
			Serial.println("ClickLock");
		}
		if (digitalRead(Input3) == HIGH)
		{
			SL = 1;
			Serial.println("ScrollLock");
		}
		if (digitalRead(Input4) == HIGH)
		{
			A = 1;
			Serial.println("OptionalButtonA");
		}
		digitalWrite(OutputC, LOW);			// set C Low
		digitalWrite(OutputD, HIGH);		// set D High
		delay(1);
		if (digitalRead(Input1) == HIGH)
		{
			B = 1;
			Serial.println("OptionalButtonB");
		}
		if (digitalRead(Input2) == HIGH)
		{
			C = 1;
			Serial.println("OptionalButtonC");
		}
		if (digitalRead(Input3) == HIGH)
		{
			D = 1;
			Serial.println("OptionalButtonD");
		}
		if (digitalRead(Input4) == HIGH)
		{
			E = 1;
			Serial.println("OptionalButtonE");
		}
		digitalWrite(OutputD, LOW);			// set D Low

	// Process Inputs

	// Lets deal with the Mouse Pointer first; as that will be the easiest to deal with.

		if (SL == 0)
		{
			X = Rate * (South - North);
			Y = Rate * (East - West);
			Mouse.move(Y, X);
		}
		else
		{
			// Really needs to toggle and have an LED attached. Same as ClickLock.
			// Should also be interlocked against all other buttons; so that it locks them out,
			Mouse.scroll(South - North);
			delay(ScrollSpeed);				// Scroll is really quite fast otherwise
		}
	// Click Lock Next
		if (CL == 1)
		{
			// Not Implemented yet.
			// Really needs to toggle and have an LED attached. Same as Scroll Lock.
		}
	// Left Click.
		if (LC1 == 1 || LC2 ==1)
		{
			Mouse.set_buttons(1, 0, 0);
			Mouse.set_buttons(0, 0, 0);
			delay(500);						// Will sort this out.
		}
	// Right Click.
		if (RC1 == 1 || RC2 ==1)
		{
			Mouse.set_buttons(0, 0, 1);
			Mouse.set_buttons(0, 0, 0);
			delay(500);						// Same as Left click; needs removed from delay.
		}
	// Double Click.
		if (DC == 1)
		{
			Mouse.set_buttons(1, 0, 0);
			Mouse.set_buttons(0, 0, 0);
			delay(100);						// Tenth of a second delay (this I guess is ok…)
			Mouse.set_buttons(1, 0, 0);
			Mouse.set_buttons(0, 0, 0);
			delay(250);						// Same as Left/Right click; needs removed from delay.
		}
}

I now have a working Prototype, and on Friday I will be in Swindon to get some user feed back before progressing with the final product. Also I will need to clean up my code.