aboutsummaryrefslogtreecommitdiff
path: root/Arduino/ButtonMatrix/ButtonMatrix.pde
blob: 095665992c02e3333d73f0c3d4f3502a5e5d543e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <Keypad.h> // Needs to be the modified version

const byte ROWS = 19;
const byte COLS = 8;   

// Top	
// these constants describe the pins for the acellarometer . They won't change:
const int agroundpin = A4;             // analog input pin 4 -- ground
const int apowerpin = A5;              // analog input pin 5 -- voltage
const int axpin = A3;                  // x-axis of the accelerometer
const int aypin = A2;                  // y-axis
const int azpin = A1;                  // z-axis (only on 3-axis models)
const int aselfTest = A0;

// Bottom
// these constants describe the pins for the acellarometer . They won't change:
const int bgroundpin = A12;             // analog input pin 4 -- ground
const int bpowerpin = A13;              // analog input pin 5 -- voltage
const int bxpin = A11;                  // x-axis of the accelerometer
const int bypin = A10;                  // y-axis
const int bzpin = A9;                  // z-axis (only on 3-axis models)
const int bselfTest = A8;


const int id = 1;

/* Each of the buttons can be refered to by a number 
 At the moment the byte overflow for the 127+ numbers is dealt with at the 
 getKey time, but if a unsigned byte could be used here it would remove the need
 for the correction?
 */
char keys[COLS][ROWS] = {
  {
    1, 9,  17, 25, 33, 41, 49, 57, 65, 73, 81, 89, 97,  105, 113, 121, 129, 137, 145            }
  ,
  {
    2, 10, 18, 26, 34, 42, 50, 58, 66, 74, 82, 90, 98,  106, 114, 122, 130, 138, 146            }
  ,
  {
    3, 11, 19, 27, 35, 43, 51, 59, 67, 75, 83, 91, 99,  107, 115, 123, 131, 139, 147            }
  ,
  {
    4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148            }
  ,
  {
    5, 13, 21, 29, 37, 45, 53, 61, 69, 77, 85, 93, 101, 109, 117, 125, 133, 141, 149            }
  ,
  {
    6, 14, 22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 142, 150            }
  ,
  {
    7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 135, 143, 151            }
  ,
  {
    8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152            }
};

byte colPins[COLS] = {
  9,  8,  7,  6,  5,  4,  3,  2};
byte rowPins[ROWS] = {
  14, 15, 16, 17, 18, 19, 20, 21, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42};

/*                                                 
 Becuase of the diodes being fitted the WRONG WAY ROUND, 
 the columns are rows and the rows are columns, have fun!
 */
//                                               ROW      COL      ROWS  COLS
Keypad keypad = Keypad( makeKeymap(keys), colPins, rowPins, COLS, ROWS); 

void setup(){
  Serial.begin(9600);
  //Serial.println("Go");

  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.
  pinMode(agroundpin, OUTPUT);
  pinMode(apowerpin, OUTPUT);
  //pinMode(selfTest, OUTPUT);
  digitalWrite(agroundpin, LOW);
  digitalWrite(apowerpin, HIGH);
  //digitalWrite(selfTest, HIGH);
  pinMode(bgroundpin, OUTPUT);
  pinMode(bpowerpin, OUTPUT);
  //pinMode(selfTest, OUTPUT);
  digitalWrite(bgroundpin, LOW);
  digitalWrite(bpowerpin, HIGH);
  //digitalWrite(selfTest, HIGH);

}
/* Send numbers back and forth to test the integrity of the serial communication 
 Computer sends the first byte
 */
boolean runSerialTest() {
  Serial.println("Begining Serial Test");
  delay(10);
  byte sByte, cByte;
  for (int i=0; i<10000; i++) {
    sByte = Serial.read();
    if (sByte != cByte && i != 0) return false;
    sByte = ((sByte * 10) % 127);
    Serial.write(sByte);
    cByte = ((sByte * 10) % 127); // The next expected byte
  }
  return true;
}

void loop(){

  int ax = analogRead(axpin);
  int ay = analogRead(aypin);
  int bx = analogRead(bxpin);
  int by = analogRead(bypin);

  Serial.print("B");
  Serial.print(" ");
  char customKey = keypad.getKey();
  int customKeyInt = customKey;
  if (customKeyInt < 0) customKeyInt += 256;
  if (customKey != NO_KEY){
    Serial.print(customKeyInt);
  }
  Serial.println();
  
  Serial.print("A");
  Serial.print(" ");
  // print the sensor values:
  Serial.print(ax);
  // print a tab between values:
  Serial.print(" ");
  Serial.print(ay);
  Serial.print(" ");
  // print the sensor values:
  Serial.print(bx);
  // print a tab between values:
  Serial.print(" ");
  Serial.println(by);
  // print a tab between values:
  // delay before next reading:
  delay(60);
  //delay(100);
}