// THE BARKLEY CODE SAMPLE#3 - Plauying a repteated signal #define trig555 4 // TRIGGER THE 555 TIME RELAY - CONNECTION TO GPIO 4 #define in555 5 // OUTPUT FROM 555 TIMER TO GPIO5 SET AS AN INPUT #define ultra 8 // GPIO IS IS THE ULTRASONIC TRANSDUCER DRIVER PIN #define buzzer 9 // GPIO 9 DRIVES THE BUZZER #define reset555 6 // GPIO 6 ENABLES AND DISABLES THE 555 TIMER #define LED1 12 // GPIO PINS 12 AND 13 DRIVE THE LEDS #define LED2 13 #define B 3 // DIP SWITCH-B IS CONNECTION TO GPIO 3, AND THE DIP SWITCH-A CONNECTION TO GPIO 2 #define A 2 int sense = 0; // THIS VARIABLE IS USED FOR VOLUME CONTROL int freq = 0; // THIS VARIABLE IS USED FOR FREQUENCY CONTROL int freq2 = 0; // ALSO USED FOR FREQUENCY CONTROL int state = 0; // STATE AND STATE2 ARE USED TO KEEP PROGRAMMING IN LOOPS int state2 = 0; void setup() { // LET'S SET UP ALL OF OUR GPIOs pinMode(trig555,OUTPUT); // SETYUP THE 555 TIMER TRIGGER AS AN OUTPUT digitalWrite(trig555,LOW); // TURN DELAY TRIGGER OFF pinMode(ultra,INPUT); // THIS CAN BE SET UP AS AN INPUT OR OUTPUT, BUT ONLY SET IT UP AS OUTPUT IF YOU WANT TO DRIVE THE BUZZER AND ULTRASONIC TRANDUCER SEPERATELY. FOR NOW, LEAVE IT AS AN INPUT, AND PLACE A JUMPER ON THE HEADER BELOW GPIO 9 pinMode(B,INPUT); // CONTINUED FROM ABOVE... IF YOU SHORT THE JUMPER BELOW GPIO 9, THEN THE BUZZER DRIVER SIGNAL WILL CONTROL BOTH THE BUZZER AND ULTRASONIC TRANSDUCER IN PARALLEL. UNRELATED, SET DIP-B AS AN INPUT pinMode(A,INPUT); // SET DIP-A AS AN INPUT pinMode(buzzer,OUTPUT); // SET THE BUZZER AS AN OUTPUT. IF THE JUMPER BELOW GPIO 9 IS SHORTED, THEN THIS SIGNAL WILL ACT TO CONTROL BOTH THE BUZZER AND ULTRASONIC TRANSDUCER AT THE SAME TIME. digitalWrite(buzzer,LOW); // TURN THIS SIGNAL OFF pinMode(LED1,OUTPUT); // SET BOTH LEDS AS OUTPUTS pinMode(LED2,OUTPUT); pinMode(in555,INPUT); // THIS IS THE DELAY INPUT CREATED BY THE 555 TIMER. FOR MORE INFO ON THE 555 TIMER, SEE BELOW pinMode(reset555,OUTPUT); // SET THE 555 RESET LINE AS AN OUPTU AND THEN TURN IT ON BY SETTING THE LINE HIGH. digitalWrite(reset555,HIGH); } // HERE ARE THE GPIOs THAT WE ARE USING... // A0 is FREQUENCY adustment. Adjust the FREQ variable resistor to play around with the frequency. High frequencies will be hard to haer. // A1 is sound sensitivity adjustment. Adjust the SENSE variable resistor to heighten or lower the sound sensitivity from the microphone, as the microphone will wait for loud noises and then compare againse a value determined by this variable resistor. // 13 and 12 are LEDs // 8 is driver buzzer // 9 is ultrasonic driver // 2 and 3 is DIP A and DIP B respectively. Normally low. Pulled-high when ON // 0 is NORMALLY-LOW button. Goes to 5v when pressed. // 4 is delay trigger. Pulse on for 1ms to 5ms to activate delay // 5 is delay output set as an input // Jumper located right below GPIO 9 connections GPIOs 8 and 9 together if shorted. // BSTEN is boost-enable jumper. Only power this booster on if you want a higher power ultrasonic signal. // 5v/BST header selects between 5v drive signal for ultarsonic, or 16v drive signal. If BST is selected, BSTEN jumper must be shorted. void loop(){ // Here is our main loop. It will loop the drive frequency by sampling the FREQ variable resistor. Adjust the FREQ V-resistor to change the frequency freq = analogRead(0); // Sample the FREQ variable resistor voltage. if(freq < 1){ // IF THE RETURNED VALUE IS LESS THAN 1, change it to 1. freq = 1; // Otherwise, leave it as it is. } digitalWrite(buzzer,HIGH); // Make sure that the 2-pin header below GPIO 9 is connected with a jumper. Turn the drive signal on. delayMicroseconds(freq); // Wait as many Microseconds as directed by the value sampled from the FREQ resistor. Will be between 1-1023 digitalWrite(buzzer,LOW); // Turn the drive signal off *************************** See the note at the end if this function delayMicroseconds(freq); // Wait the same amount of time as the last delay // IF you want to, set the OFF delay or ON delay to a fixed value. See the note above where you see "**********************". The above code as it stands offers a 50% duty cyclem which means that the signal is always 50% on and 50% off... // ... due to the on and off delays bebeing the same value sampled from the FREQ resistor. If you set the on or off delay to say 15 microseconds, then you'll have a fixed on or off time, and a variable duty cycle, which will offer different sounds and frequencyes. }