     1	/*
     2	 * Arduino iambic CW keyer
     3	 * for Ham radio usage
     4	 * Richard Chapman
     5	 * KC4IFB
     6	 * January, 2009
     7	 */ 
     8	 
      	
     9	#define SPEEDIN 0                      // analog pin to read the speed value 
    10	#define DOTIN 11                     // high -> dot paddle closed
    11	#define DASHIN 12                    // high -> dash paddle closed 
    12	#define KEYOUT 13                    // drives the 2N2222 that closes the key connection
    13	       
    14	int dotLength; // length of a dot in milliseconds
    15	int dotVal;
    16	int dashVal;
    17	int speedDial; 
    18	void setup()                    // run once, when the sketch starts
    19	{
    20	  pinMode(KEYOUT, OUTPUT);      // sets modes on the i/o pins
    21	  pinMode(DOTIN, INPUT);        // 
    22	  pinMode(DASHIN, INPUT); 
    23	  // no need to set SPEED pin as input since it is analog
    24	  digitalWrite(KEYOUT, LOW);     // initially the key is open
    25	  
    26	     // activate internal pullup resistors 
    27	   digitalWrite(DOTIN,HIGH); 
    28	   digitalWrite(DASHIN,HIGH);
    29	}
      	
    30	void loop()                     // run over and over again
    31	{
      	
    32	   
    33	   // get the speed
    34	   speedDial = analogRead(SPEEDIN); 
    35	   dotLength = 1200 / ( 5 + (speedDial/64)); 
    36	   dotVal = digitalRead(DOTIN);
    37	   dashVal = digitalRead(DASHIN); 
    38	   
    39	   if ((dotVal == LOW) && (dashVal == HIGH)){ // dot only
    40	       sendDot();
    41	   } else if ((dashVal == LOW) && (dotVal == HIGH)) {  // dash only
    42	       sendDash(); 
    43	   } else if ((dashVal == LOW) && (dotVal == LOW)) {// both pressed
    44	       // iambic
    45	       sendDot();
    46	       dotDelay();
    47	       sendDash();
    48	   }  else {  // nothing pressed
    49	   } 
    50	   dotDelay(); 
    51	} 
      	
      	
    52	void sendDot() { 
    53	  digitalWrite(KEYOUT,HIGH);
    54	  delay(dotLength);
    55	  digitalWrite(KEYOUT,LOW);
    56	}
      	
    57	void sendDash() {
    58	  digitalWrite(KEYOUT,HIGH);
    59	  delay(3*dotLength);
    60	  digitalWrite(KEYOUT,LOW);
    61	}
      	
    62	void dotDelay() { 
    63	  delay(dotLength); 
    64	}
      	
      	
    65	  
