Stap 2: programmeren
Hier is de fundamentele stroom van het programma (in pseudo-code):
lus
-Controleer voor knoppen
-als knop
--Kies "frequentie"
--lus
---set leidde hoog
---piëzo hoog instellen
---wachten "bracht pulslengte"
---set leidde laag
---"frequentie" wachten / 2 - "bracht pulslengte"
---piezo laag instellen
---"frequentie" wachten / 2
---check voor knoppen
---als knop
----pauze
---eindigen als
--einde lus
-Als
einde lus
en de volledige code
//**************************// THE TUNE TRAINER v1.0// BY: GREG SCHOPPE// http://www.gschoppe.com//**************************// STRINGS ========================================#define e 3034 // 329.63 Hz#define b 4050 // 246.94 Hz#define g 5102 // 196.00 Hz#define d 6811 // 146.83 Hz#define a 9091 // 110.00 Hz#define E 12135 // 82.407 Hz#define NONE 0 // Default State// -- length of time the LED is on in each cycle#define FlashLength 1024// SETUP ============================================// Set up input on 6 pinsint inPin_e = 7;int inPin_b = 6;int inPin_g = 5;int inPin_d = 4;int inPin_a = 3;int inPin_E = 2;// Set up output on a PWM pin (digital 9, 10 or 11)int freqOut = 11;// Do we want debugging on serial out? 1 for yes, 0 for noint DEBUG = 0;void setup() { pinMode(freqOut, OUTPUT); // declare pushbuttons as input pinMode(inPin_e, INPUT); //high-e pinMode(inPin_b, INPUT); // b pinMode(inPin_g, INPUT); // g pinMode(inPin_d, INPUT); // d pinMode(inPin_a, INPUT); // a pinMode(inPin_E, INPUT); // low-e // turn on pull-up resistors digitalWrite(inPin_e,HIGH); //high-e digitalWrite(inPin_b,HIGH); // b digitalWrite(inPin_g,HIGH); // g digitalWrite(inPin_d,HIGH); // d digitalWrite(inPin_a,HIGH); // a digitalWrite(inPin_E,HIGH); // low-e if (DEBUG) { Serial.begin(9600); // Set serial out if we want debugging } // SIGNAL THAT THE CHIP IS READY digitalWrite(freqOut,HIGH); delay(500); digitalWrite(freqOut,LOW); }int input () { if(digitalRead(inPin_e)==LOW) { return(e); } if(digitalRead(inPin_b)==LOW) { return(b); } if(digitalRead(inPin_g)==LOW) { return(g); } if(digitalRead(inPin_d)==LOW) { return(d); } if(digitalRead(inPin_a)==LOW) { return(a); } if(digitalRead(inPin_E)==LOW) { return(E); } return(NONE);}void playTone (int period) { if(period==NONE) { //if no button has been pressed return; } int ToneLength = period/2 - FlashLength; int OffLength = period/2; // SIGNAL THAT THE INPUT WAS ACCEPTED digitalWrite(freqOut,LOW); delay(100); digitalWrite(freqOut,HIGH); delay(100); digitalWrite(freqOut,LOW); delay(500); // CONTINUE TO STROBE LOOP // -- the strobe loop runs 4 strobes before each test, // -- to try and lose as few microseconds as possible. while(true) { // BEGIN SET OF 4 STROBES // ON TONE & LIGHT PORTB = (B00001100 | PORTB); // -- turns on Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(FlashLength); // OFF LIGHT PORTB = (B11110111 & PORTB); // -- turns off Pin 11; Leaves all others as-is; delayMicroseconds(ToneLength); // OFF BOTH PORTB = (B11110011 & PORTB); // -- turns off Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(OffLength); // ON TONE & LIGHT PORTB = (B00001100 | PORTB); // -- turns on Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(FlashLength); // OFF LIGHT PORTB = (B11110111 & PORTB); // -- turns off Pin 11; Leaves all others as-is; delayMicroseconds(ToneLength); // OFF BOTH PORTB = (B11110011 & PORTB); // -- turns off Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(OffLength); // ON TONE & LIGHT PORTB = (B00001100 | PORTB); // -- turns on Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(FlashLength); // OFF LIGHT PORTB = (B11110111 & PORTB); // -- turns off Pin 11; Leaves all others as-is; delayMicroseconds(ToneLength); // OFF BOTH PORTB = (B11110011 & PORTB); // -- turns off Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(OffLength); // ON TONE & LIGHT PORTB = (B00001100 | PORTB); // -- turns on Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(FlashLength); // OFF LIGHT PORTB = (B11110111 & PORTB); // -- turns off Pin 11; Leaves all others as-is; delayMicroseconds(ToneLength); // OFF BOTH PORTB = (B11110011 & PORTB); // -- turns off Pin 11 and Pin 10; Leaves all others as-is; delayMicroseconds(OffLength); // END SET OF 4 STROBES //Check Input Pins if ( (PIND & B11111100) != B11111100) { // -- PIND is a register containing the state of pins 0-7 as a byte; // -- i.e. B11111101 would signify that pins 1 and 5 were LOW and the rest were HIGH. // -- the testing against B11111100 is to ignore inputs on pin 0 and pin 1 (RX and TX) // -- so, the above code means that if any of pins 2-7 are low, we exit the loop. return; } } }void loop() { playTone(input());}
Ik heb een beetje tijd opgeslagen in de binnenste lus met sommige gehackte samen bit operaties, maar over het geheel genomen de codering is vrij basic. Zijn ook een beetje van een hack, dus gelieve niet zeuren me over malafide stijl syntaxis wetten. Ik weet wat ze zijn, maar wanneer ik haasten ben, ik negeer ze...
door een wijziging van de verklaringen bij de bovenkant kunt u de frequenties van elke knop.