//**************************************************
// Morse Code Balloon Beacon
// by Bill Brown WB8ELK <http://www.wb8elk.com>
// 
// Transmits Morse Code for balloon tracking
// by keying the SparkFun 434 MHz transmitter module
//
// Created 29 December 2008
// << -- Keyed CW mode -- >>
//**************************************************

int adc0;
int adclen;
byte morse;
byte cwlen;
byte cw;
int tone;
byte x;

int cwoutPin = 9; // TX output pin

char asciibuf[5];
char callsign[15] = {'W','B','8','E','L','K',' ','B','A','L','L','O','O','N'};


// ********************************************************
// ASCII to Morse Code Lookup Table
// ********************************************************

const unsigned char morse_table[63] = { 	// Morse Code Lookup table

	0b00000001,	//;sp - 0x20 
	0b01001010,	//; ! - 0x21
	0b01101101,	//; " - 0x22
	0b01011110,	//; # - 0x23 // Not available in Morse Code - replace with "-"
	0b10110111,	//; $ - 0x24
	0b01011110,	//; % - 0x25 // Not available in Morse Code - replace with "-"
	0b00111101,	//; & - 0x26
	0b01100001,	//; ' - 0x27
	0b00110010,	//; ( - 0x28
	0b01010010,	//; ) - 0x29
	0b01011110,	//; * - 0x2A // Not available in Morse Code - replace with "-"
	0b00110101,	//; + - 0x2B 
	0b01001100,     //; , - 0x2C
	0b01011110,     //; - - 0x2D
	0b01010101,     //; . - 0x2E
	0b00110110,	//; / - 0x2F
	0b00100000,	//; 0 - 0x30
	0b00100001,	//; 1 - 0x31
	0b00100011,	//; 2 - 0x32
	0b00100111,	//; 3 - 0x33
	0b00101111,	//; 4 - 0x34
	0b00111111,	//; 5 - 0x35
	0b00111110,	//; 6 - 0x36
	0b00111100,	//; 7 - 0x37
	0b00111000,	//; 8 - 0x38
	0b00110000,	//; 9 - 0x39
	0b01111000,	//; : - 0x3A
	0b00101010,	//; ; - 0x3B
	0b00110010,	//; < - 0x3C // send ( since no < in Morse
	0b00101110,	//; = - 0x3D 
	0b01010010,	//; > - 0x3E // send ) since no > in Morse
	0b01110011,	//; ? - 0x3F
	0b01101001,	//; @ - 0x40 
	0b00000101,	//; A - 0x41
	0b00011110,	//; B - 0x42
	0b00011010,	//; C - 0x43
	0b00001110,	//; D - 0x44
	0b00000011,	//; E - 0x45
	0b00011011,	//; F - 0x46
	0b00001100,	//; G - 0x47
	0b00011111,	//; H - 0x48
	0b00000111,	//; I - 0x49
	0b00010001,	//; J - 0x4A
	0b00001010,	//; K - 0x4B
	0b00011101,	//; L - 0x4C
	0b00000100,	//; M - 0x4D
	0b00000110,	//; N - 0x4E
	0b00001000,	//; O - 0x4F
	0b00011001,	//; P - 0x50
	0b00010100,	//; Q - 0x51
	0b00001101,	//; R - 0x52
	0b00001111,	//; S - 0x53
	0b00000010,	//; T - 0x54
	0b00001011,	//; U - 0x55
	0b00010111,	//; V - 0x56
	0b00001001,	//; W - 0x57
	0b00010110,	//; X - 0x58
	0b00010010,	//; Y - 0x59
	0b00011100	//; Z - 0x5A
	
};

// END of Morse Table

void setup()
{

  pinMode(cwoutPin, OUTPUT);      // sets the digital pin as output 

}


void loop()
{
  
// Morse Code repeating sequence main loop

while(1){

// ***********************
// Send Callsign
// ***********************



delay(10000); // Wait 10 seconds between each transmission


for(x=0;x<14;x++){
	sendcw(callsign[x]);
  }

delay(1000); // Wait 1 second before sending telemetry

//***************************************************
// Read the Temperature Sensor and send the value out  
//***************************************************

 adc0 = analogRead(0);
 
 itoa(adc0, asciibuf, 10);
 
  if (adc0<10){
    adclen = 1;
  }
  if (adc0<100 && adc0>=10){
    adclen = 2;
  }
  if (adc0<1000 && adc0>=100){
    adclen = 3; 
  }
  if(adc0>=1000){
    adclen = 4;
  }

  for (cw=0;cw<adclen;cw++){
    sendcw(asciibuf[cw]);
    delay(100);
  }

  
} // End of main while
  
  
} // End of Loop

//------------------------------------------------

// ***********************
// Functions
// ***********************

//*********************
// CW output function *
//*********************
void sendcw(int cwchar)
{
  
 morse = morse_table[cwchar-0x20];
  
while(1){

    if(cwchar == ' '){ // if SPACE character then delay 500 mSec between words
      delay(500);
      return;
    }
  
    if (morse & 0b00000001){ // compare LSB [ 1=Dit  0=Dah ]
      digitalWrite(cwoutPin, HIGH);
      delay(100); // dit timing - 100 msec
      digitalWrite(cwoutPin, LOW);
      delay(100);    
    }
    else{     
      digitalWrite(cwoutPin, HIGH);
      delay(300); // dah timing - 300 msec
      digitalWrite(cwoutPin, LOW);
      delay(100);       
    }

    morse = morse >> 1; // shift right to see if Dit or Dah

    if(morse == 1){ // if the value is 1 then end of CW character
      delay(200);
      return;
    }
  } // End of Morse while loop
  
} // End of SENDCW routine
// -------------------------------