Stap 25: Seriële communicatie
Op dit punt in het project ik heb een beetje ongeduldig en wilde beginnen met het maken van sommige geluiden, zodat ik sommige dingen aan de firmware te maken van de Arduino verzenden en ontvangen van informatie serieel wordt toegevoegd. Ik heb gepost de onderstaande code. Ik gebruikte een timer onderbreken om te houden van de Arduino inspelen op seriële berichten, kunt u lezen een beetje meer over hoe dat hier werkt. (als u deze code wilt testen, gebruik de app verbonden gaat, de nieuwere versie van de app van de beat slicer op github is een beetje anders).
//BUTTON TEST w/ 74HC595 and 74HC165 and serial communication //by Amanda Ghassaei //June 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 send data back and forth with the maxmsp patch "beat slicer" //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; byte ledByte; //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 Serial.begin(57600); cli();//stop interrupts //set timer2 interrupt every 128us TCCR2A = 0;// set entire TCCR2A register to 0 TCCR2B = 0;// same for TCCR2B TCNT2 = 0;//initialize counter value to 0 // set compare match register for 7.8khz increments OCR2A = 255;// = (16*10^6) / (7812.5*8) - 1 (must be <256) // turn on CTC mode TCCR2A |= (1 << WGM21); // Set CS11 bit for 8 prescaler TCCR2B |= (1 << CS11); // enable timer compare interrupt TIMSK2 |= (1 << OCIE2A); sei();//allow interrupts } // 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); if (buttonEvent[i]<> 1) & 3; byte ledx = (ledByte >> 3) & 3; if (ledstate){ ledData[ledy] |= 8 >> ledx; } else{ ledData[ledy] &= ~ (8 >> ledx); } }//end if serial available }//end do while (Serial.available() > 8); } void loop() { shift();//updates leds and receives data from buttons }
Ik stuurde deze seriële gegevens naar een toepassing van de audioverwerking schreef ik in MaxMSP. U kunt downloaden van deze toepassing hieronder (genaamd beat slicer) en open het met MaxMSP of MaxRuntime (gratis versie) hier. Ik ben het runnen van Max 5, ik neem aan dat het zal werken op Max 6 ook. Het zal prompt u voor uitgezocht een audiobestand (mp3, wav, aiff). Vervolgens kunt u het bewerken van deze audio-bestand met behulp van de knop Raster zoals in de video hieronder: