Stap 24: Test knoppen
De volgende code op de Arduino te uploaden. Als alles correct is aangesloten kunnen elke knop zich zal oplichten zolang ingedrukt blijft. Als u merken bent dat sommige knoppen zijn niet als responsieve proberen de geleidende ringen en de sporen op de pcb met water reinigen en grondig drogen, dit enkele reactie problemen opgelost voor mij.
//BUTTON TEST w/ 74HC595 and 74HC165 //by Amanda Ghassaei 2012 /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * */ //this firmware will cause the buttons to light up momentarily while they are pressed. //pin connections int ledLatchPin = 6; int ledClockPin = 5; int ledDataPin = 7; int buttonLatchPin = 4; int buttonClockPin = 3; int buttonDataPin = 2; //looping variables byte i; byte j; byte k; //storage for led states, 4 bytes byte ledData[] = {0, 0, 0, 0}; //storage for buttons, 4 bytes byte buttonCurrent[] = {0,0,0,0}; byte buttonLast[] = {0,0,0,0}; byte buttonEvent[] = {0,0,0,0}; byte buttonState[] = {0,0,0,0}; //button debounce counter- 16 bytes byte buttonDebounceCounter[4][4]; void setup() { DDRD = 0xFA;//set pins D7-D4 as output, D2 as input } // buttonCheck - checks the state of a given button. //this buttoncheck function is largely copied from the monome 40h firmware by brian crabtree and joe lake void buttonCheck(byte row, byte index) { if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) && // if the current physical button state is different from the ((buttonCurrent[row] ^ buttonState[row]) & (1 << index))) { // last physical button state AND the current debounced state if (buttonCurrent[row] & (1 << index)) { // if the current physical button state is depressed buttonEvent[row] = 1 << index; // queue up a new button event immediately buttonState[row] |= (1 << index); // and set the debounced state to down. } else{ buttonDebounceCounter[row][index] = 12; } // otherwise the button was previously depressed and now // has been released so we set our debounce counter. } else if (((buttonCurrent[row] ^ buttonLast[row]) & (1 << index)) == 0 && // if the current physical button state is the same as (buttonCurrent[row] ^ buttonState[row]) & (1 << index)) { // the last physical button state but the current physical // button state is different from the current debounce // state... if (buttonDebounceCounter[row][index] > 0 && --buttonDebounceCounter[row][index] == 0) { // if the the debounce counter has // been decremented to 0 (meaning the // the button has been up for // kButtonUpDefaultDebounceCount // iterations/// buttonEvent[row] = 1 << index; // queue up a button state change event if (buttonCurrent[row] & (1 << index)){ // and toggle the buttons debounce state. buttonState[row] |= (1 << index); } else{ buttonState[row] &= ~(1 << index); } } } } void shift(){ for (i=0;i<4;i++){ buttonLast[i] = buttonCurrent[i]; byte dataToSend = (1 << (i+4)) | (15 & ~ledData[i]); // set latch pin low so the LEDs don't change while sending in bits digitalWrite(ledLatchPin, LOW); // shift out the bits of dataToSend shiftOut(ledDataPin, ledClockPin, LSBFIRST, dataToSend); //set latch pin high so the LEDs will receive new data digitalWrite(ledLatchPin, HIGH); //once one row has been set high, receive data from buttons //set latch pin high digitalWrite(buttonLatchPin, HIGH); //shift in data buttonCurrent[i] = shiftIn(buttonDataPin, buttonClockPin, LSBFIRST) >> 3; //latchpin low digitalWrite(buttonLatchPin, LOW); for (k=0;k<4;k++){ buttonCheck(i,k); } } } void updateLEDs(){ //update the leds to reflect hte state of the buttons for (j=0;j<4;j++){ ledData[j] = buttonState[j]; } } void loop() { shift(); updateLEDs(); }