Components and supplies
Piezo sounder element
7mm Pushbutton (Normally open type)
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
"Bug" keyer code
c_cpp
1 2//Morse "bug" keyer 3 4//buttons 5#define button_1 2 6#define button_2 3 7 8//speed selector pins 9//take one to ground to select speed 10//default is 5 words per minute 11#define selector_1 A0 12#define selector_2 A1 13#define selector_3 A2 14 15//the buzzer 16const int sounder_pin=5;// the pin we attach our sounder to 17const int duty_cycle=128;//50% duty cycle for analogWrite() 18 19 20//required to debounce switches 21#define MAX_CHECKS 10 22volatile uint8_t Debounced_State=0;//accessed by isr and main loop code 23uint8_t State[MAX_CHECKS]={0}; 24uint8_t Index=0; 25 26 27void setup() { 28 // put your setup code here, to run once: 29 30 pinMode(button_1,INPUT_PULLUP); 31 pinMode(button_2,INPUT_PULLUP); 32 33 pinMode(selector_1,INPUT_PULLUP); 34 pinMode(selector_2,INPUT_PULLUP); 35 pinMode(selector_3,INPUT_PULLUP); 36 37 pinMode(sounder_pin, OUTPUT); 38 39 // initialize timer1 40 noInterrupts(); // disable all interrupts 41 TCCR1A = 0; 42 TCCR1B = 0; 43 TCNT1 = 0; 44 45 OCR1A = 625; // compare match register 16MHX/256/100HZ 46 TCCR1B |= (1 << WGM12); // CTC mode 47 TCCR1B |= (1 << CS12); // 256 prescaler 48 TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt 49 interrupts(); // enable all interrupts 50 51} 52 53void loop() { 54 // put your main code here, to run repeatedly: 55 56static int transferred_value=0; 57static bool do_character=false; 58static bool do_dot=false; 59static bool wait_up=false; 60 61 noInterrupts(); // disable all interrupts 62 transferred_value=Debounced_State; 63 interrupts(); // enable all interrupts 64 65 66if (!transferred_value){wait_up=false;}//if all buttons released 67if (transferred_value){ 68 69 if(transferred_value==2){do_dot=true;do_character=true;wait_up=true;}//dot 70 if(transferred_value==1){if(!wait_up){do_dot=false;do_character=true;wait_up=true;}}//dash 71 72 } 73 74 75//morse speed 76//all timing is based around the length of the dot 77//source for timing -> http://www.kent-engineers.com/codespeed.htm 78 79static unsigned long unit_length=240;//default 5 words per minute 80if(!digitalRead(A0)){unit_length=240;}//dot time calculated for 5 words per minute 81if(!digitalRead(A1)){unit_length=92;}//dot time calculated for 13 words per minute 82if(!digitalRead(A2)){unit_length=60;}//dot time calculated for 20 words per minute 83 84 85const unsigned long dot=unit_length; 86const unsigned long dash=unit_length*3; 87const unsigned long intra_letter_space=unit_length;//space between dot's and dashes within letter 88 89 90if (do_character) 91{ 92 do_character=false; 93 94 95 if (!do_dot){ //dash 96 analogWrite(sounder_pin, duty_cycle); 97 delay(dash); // wait for a while 98 analogWrite(sounder_pin,0); 99 delay(intra_letter_space); // space between letter parts 100 } 101 else{ //dot 102 analogWrite(sounder_pin, duty_cycle); 103 delay(dot); // wait for a while 104 analogWrite(sounder_pin,0); 105 delay(intra_letter_space); // space between letter parts 106 } 107 108} 109 110 111} 112/*-----------------*/ 113//my_functions 114ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 115{ 116//code source - A Guide to Debouncing by Jack G. Ganssle 117//read buttons 118 uint8_t temp=0x00; 119 120 temp|=!digitalRead(button_1)<<1; 121 temp|=!digitalRead(button_2)<<0; 122 123//debounce 124 uint8_t i,j; 125 126 State[Index]= temp; 127 ++Index; 128 j=0xFF; 129 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 130 Debounced_State=j; 131 if(Index>=MAX_CHECKS){Index=0;} 132 133} 134//-----------end------------ 135
"Bug" keyer code
c_cpp
1 2//Morse "bug" keyer 3 4//buttons 5#define button_1 2 6#define button_2 3 7 8//speed selector pins 9//take one to ground to select speed 10//default is 5 words per minute 11#define selector_1 A0 12#define selector_2 A1 13#define selector_3 A2 14 15//the buzzer 16const int sounder_pin=5;// the pin we attach our sounder to 17const int duty_cycle=128;//50% duty cycle for analogWrite() 18 19 20//required to debounce switches 21#define MAX_CHECKS 10 22volatile uint8_t Debounced_State=0;//accessed by isr and main loop code 23uint8_t State[MAX_CHECKS]={0}; 24uint8_t Index=0; 25 26 27void setup() { 28 // put your setup code here, to run once: 29 30 pinMode(button_1,INPUT_PULLUP); 31 pinMode(button_2,INPUT_PULLUP); 32 33 pinMode(selector_1,INPUT_PULLUP); 34 pinMode(selector_2,INPUT_PULLUP); 35 pinMode(selector_3,INPUT_PULLUP); 36 37 pinMode(sounder_pin, OUTPUT); 38 39 // initialize timer1 40 noInterrupts(); // disable all interrupts 41 TCCR1A = 0; 42 TCCR1B = 0; 43 TCNT1 = 0; 44 45 OCR1A = 625; // compare match register 16MHX/256/100HZ 46 TCCR1B |= (1 << WGM12); // CTC mode 47 TCCR1B |= (1 << CS12); // 256 prescaler 48 TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt 49 interrupts(); // enable all interrupts 50 51} 52 53void loop() { 54 // put your main code here, to run repeatedly: 55 56static int transferred_value=0; 57static bool do_character=false; 58static bool do_dot=false; 59static bool wait_up=false; 60 61 noInterrupts(); // disable all interrupts 62 transferred_value=Debounced_State; 63 interrupts(); // enable all interrupts 64 65 66if (!transferred_value){wait_up=false;}//if all buttons released 67if (transferred_value){ 68 69 if(transferred_value==2){do_dot=true;do_character=true;wait_up=true;}//dot 70 if(transferred_value==1){if(!wait_up){do_dot=false;do_character=true;wait_up=true;}}//dash 71 72 } 73 74 75//morse speed 76//all timing is based around the length of the dot 77//source for timing -> http://www.kent-engineers.com/codespeed.htm 78 79static unsigned long unit_length=240;//default 5 words per minute 80if(!digitalRead(A0)){unit_length=240;}//dot time calculated for 5 words per minute 81if(!digitalRead(A1)){unit_length=92;}//dot time calculated for 13 words per minute 82if(!digitalRead(A2)){unit_length=60;}//dot time calculated for 20 words per minute 83 84 85const unsigned long dot=unit_length; 86const unsigned long dash=unit_length*3; 87const unsigned long intra_letter_space=unit_length;//space between dot's and dashes within letter 88 89 90if (do_character) 91{ 92 do_character=false; 93 94 95 if (!do_dot){ //dash 96 analogWrite(sounder_pin, duty_cycle); 97 delay(dash); // wait for a while 98 analogWrite(sounder_pin,0); 99 delay(intra_letter_space); // space between letter parts 100 } 101 else{ //dot 102 analogWrite(sounder_pin, duty_cycle); 103 delay(dot); // wait for a while 104 analogWrite(sounder_pin,0); 105 delay(intra_letter_space); // space between letter parts 106 } 107 108} 109 110 111} 112/*-----------------*/ 113//my_functions 114ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine 115{ 116//code source - A Guide to Debouncing by Jack G. Ganssle 117//read buttons 118 uint8_t temp=0x00; 119 120 temp|=!digitalRead(button_1)<<1; 121 temp|=!digitalRead(button_2)<<0; 122 123//debounce 124 uint8_t i,j; 125 126 State[Index]= temp; 127 ++Index; 128 j=0xFF; 129 for (i=0;i<MAX_CHECKS;i++){j=j&State[i];} 130 Debounced_State=j; 131 if(Index>=MAX_CHECKS){Index=0;} 132 133} 134//-----------end------------ 135
Downloadable files
Circuit diagram
Circuit diagram
Comments
Only logged in users can leave comments
glennedi
0 Followers
•0 Projects
Table of contents
Intro
6
0