Based on some work found on this board at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169088394 and
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1166896036,
I'm pleased to present a simple beacon, broadcasting Morse code over the radio airwaves. It works by using an assembly increment on PORTB in the fast loop in the broadcast function. The least significant bit toggles once per loop, the second toggles every two loops, the third every four, and so on.
Just run the code and plug a length of wire into Digital Pin 8 and tune your AM dial to around 1337 kHz. You can also get 1337/2 kHz on Pin 9 and 1337/4 kHz on Pin 10, etc. but this will probably be off your dial. The LED on Pin 13 flashes the message too, if you like to look at the pretty lights.
This is patched together from code from a lot of sources, so if you care about me infringing on your copyright, sorry. Feel free to infringe on this code however you feel necessary, but it expects to be cuddled after you've had your way with it.
long millisAtStart=0;
long millisAtEnd=0;
const long period_broadcast=8; //period of shortest broadcast (256 port changes)
#define LENGTH_DIT 64
//number of period_broadcasts in one 'dit',
//all other lengths are scaled from this
const int length_dit=LENGTH_DIT;//number of periods for dit
const int pause_dit=LENGTH_DIT;//pause after dit
const int length_dah=3*LENGTH_DIT;//number of persots for dah
const int pause_dah=LENGTH_DIT;//pause after dah
const int length_pause=7*LENGTH_DIT;//pause between words
void dit(void);
void dah(void);
void pause(void);
void broadcast(int N_cycles);
void dontbroadcast(int N_cycles);
// ### INC ### Increment Register (reg = reg + 1)
#define ASM_INC(reg) asm volatile ("inc %0" : "=r" (reg) : "0" (reg))
void setup()
{
Serial.begin(9600);
DDRB=0xFF; //Port B all outputs
//Do one dit to determine approximate frequency
millisAtStart=millis();
dit();
millisAtEnd=millis();
Serial.print(millisAtEnd-millisAtStart);
Serial.print(" ");
Serial.print((length_dit+pause_dit)*period_broadcast*256/(millisAtEnd-millisAtStart)/2);
Serial.print("kHz ");
Serial.println();
}
void loop()
{
dah();
dit();
dah();
dah();
pause();
dah();
dit();
dah();
dah();
pause();
dah();
dah();
dit();
dit();
pause();
pause();
}
void dit(void)
{
for(int i=0;i<length_dit;i++)
{
broadcast(period_broadcast);
}
for(int i=0;i<pause_dit;i++)
{
dontbroadcast(period_broadcast);
}
}
void dah(void)
{
for(int i=0;i<length_dah;i++)
{
broadcast(period_broadcast);
}
for(int i=0;i<pause_dah;i++)
{
dontbroadcast(period_broadcast);
}
}
void pause(void)
{
for(int i=0;i<length_pause;i++)
{
dontbroadcast(period_broadcast);
}
}
void broadcast(int N_cycles)
{
unsigned int portvalue;
for (int i=0;i<N_cycles;i++)
{
portvalue=0;
do
{
PORTB=portvalue;
ASM_INC(portvalue);
}
while(portvalue<255);
}
}
void dontbroadcast(int N_cycles)
{
unsigned int portvalue;
PORTB=0x00;
for (int i=0;i<N_cycles;i++)
{
portvalue=0;
do
{
ASM_INC(portvalue);
//add some assembly No OPerations to keep timing the same
asm volatile ("NOP");
}
while(portvalue<255);
}
}