button matrix time between presses

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, I have mentioned this before, and I have got something that outputs values, but for certain values (specifically in the corners of the 8x8 matrix) suprious things seem to happen.
My code to poll, and "deal" with the button presses is below:

void KeyMatrix::pollKeys() {
	uint8_t countRows = 0, countCols = 0;
	for (uint8_t maskRows = 1; maskRows != 0; maskRows += maskRows) {
		PORTK = ~maskRows; //low one col with all others pull-up
		_delay_us(10); //after setting the state of a pin there may need to be a delay
		if (~PINH != 0) {
			for (uint8_t maskCols = 1; maskCols != 0; maskCols += maskCols) {
				if (~PINH & maskCols) {
					ATOMIC_BLOCK(ATOMIC_FORCEON)
					{
						button[countRows][countCols] = flagmS;
					}
					buttonPressed[countRows][countCols] = true;
				} else
					buttonPressed[countRows][countCols] = false;
				countCols++;
			}
			countCols = 0;
		}
		countRows++;
	}
}

void KeyMatrix::outputMidiNotes(Arpeggiator *arpeggiator) {
	for (uint8_t i = 0; i < 8; i++) {
		for (uint8_t j = 0; j < 8; j++) {
			if (buttonPressed[j] != buttonPressedLast[i][j]) {
				serial->printf("% d, % d : % u\n", i, j, button[i][j]);
				buttonPressedLast[i][j] = buttonPressed[i][j];
			}
		}
	}
}

the printf is just there to see what the output looks like.
My buttons are in pairs, like on a midi keyboard so I want to know the time between button presses.
My arrays are:

	unsigned int button[8][7];
	bool buttonPressed[8][7];
	bool buttonPressedLast[8][7];

and my timer is setup as a millisecond timer:

void initTimer0() {
	TCCR0A |= (1 << COM0A1) | (1 << WGM01);
	TCCR0B |= (1 << CS01) | (1 << CS00); // 64 prescale
	TIMSK0 |= (1 << OCIE0A);
	OCR0A = 249; // 1000 microseconds
	sei();
}

ISR(TIMER0_COMPA_vect) {
	flagmS++;
}

It [i]seems to work, however when i and j = 0 or 7 it continues to print out values as though (buttonPressed[i][j] != buttonPressedLast[i][j]) is true, but if Im holding it down it shouldnt be.

BTW if Im doing this in a very slow way, any better ideas are very welcome.

Thanks
ALex