Hello All,
I would really appreciate some assistance with my project as there is not much online that I can find.
I recently finished restoring a 1960s Kleinschmidt teletypewriter TT4C/TG that was used by the Army during the Vietnam War. I have the machine working and now I am trying to connect it to my computer through an Arduino Uno. The Arduino controls a relay and the relay controls the 110vDC current loop for the teletype. The relay has an input voltage range of 3.5-32vDC so I can directly connect it to the 5v output pins of the Arduino.
The teletype works on baudot code, which is similar to morse code and a precursor to ASCII (I think). Baudot code uses 'mark' and 'space', just like morse code uses dots and dashes, except there is no difference in time. A mark, or marking impulse, opens the current loop circuit for 22ms and a space, or spacing impulse, closes the circuit for 22ms. These occur in groups of five with a start and stop, and the teletype converts them into a letter or figure and prints it on the paper roll. For example, the letter R is: start, space, mark, space, mark, space, stop. The letter Y is: start, mark, space, mark, space, mark, stop.
The idea is to use the Arduino to convert a letter input from the serial communicator to baudot code through the relay. In theory, if you type the letter 'R' into the serial the Arduino then turns the output pin off for 22ms, then on for 22ms, then off for 22ms, etc.
The base for my code comes from this video: Arduino Morse Code Translator - YouTube
There are two main challenges I need assistance with:
1. The output pin stays HIGH regardless of whether there is anything in the serial. If send a letter through the serial, the pin does not go LOW for the time it should.
2. When there is nothing in the serial I need the output pin to be HIGH so the current loop circuit is complete, otherwise the teletype goes a bit funny. I was originally going to include the line 'digitalWrite (4, HIGH)' in the loop section of the code, but I think that would cancel out the translating function so I removed it. If anyone knows how I would write this in code please let me know!
Any assistance would be great! Thank you!
Here is my code (I have removed letters D-Z and all numbers and figures due to the forum's character limit but they all follow the same pattern as A, B, and C):
//NOTE: INPUT TO SERIAL MUST BE ALL CAPS
//NOTE: INPUT TO SERIAL MUST NOT CONTAIN APOSTROPHES
//NOTE: ALWAYS END SENTENCE WITH FULL STOP
String BaudotCode = ""; //string to store input from serial monitor
byte BaudotCodeLength = 0; //byte to store length of BaudotCode
char i; // char for parsing of BaudotCode
int relay = 4; //relay output is set to pin 4
//--------------------------------------------------------------------
// SETUP
//---------------------------------------------------------------
void setup() {
// put your setup code here, to run once:
pinMode(relay, OUTPUT); //output to arduino relay
Serial.begin(9600); //turn on serial monitor at 9600 baud
}
//-------------------------------------------------------------------
// FUNCTIONS
//-----------------------------------------------------------------
void START(){
digitalWrite(relay, LOW); //start impuse set to turn relay off for 22 milliseconds
delay(22);
}
void MARKING_IMPULSE(){
digitalWrite(relay, HIGH); //mark impuse set to turn relay on for 22 milliseconds
delay(22);
}
void SPACING_IMPULSE(){
digitalWrite(relay, LOW); //space impuse set to turn relay off for 22 milliseconds
delay(22);
}
void STOP(){
digitalWrite(relay, HIGH); //stop impuse set to turn relay on for 31 milliseconds
delay(31);
}
void LTRS_SHIFT(){
digitalWrite(relay, LOW); //start impulse
delay(22);
digitalWrite(relay, HIGH); //five marking impulses and a stop impulse
delay(141);
}
void FIGS_SHIFT(){
digitalWrite(relay, LOW); //start impulse
delay(22);
digitalWrite(relay, HIGH); //two marking impulses
delay(44);
digitalWrite(relay, LOW); //one space impulse
delay(22);
digitalWrite(relay, HIGH); //two marking impulses and a stop impulse
delay(75);
}
void CAR_RET(){
digitalWrite(relay, LOW); //start impulse and three spacing impulses
delay(88);
digitalWrite(relay, HIGH); //one marking impulse
delay(22);
digitalWrite(relay, LOW); //one space impulse
delay(22);
digitalWrite(relay, HIGH); //stop impulse
delay(31);
}
void LINE_FEED(){
digitalWrite(relay, LOW); //start impulse and a space impulse
delay(44);
digitalWrite(relay, HIGH); //marking impulse
delay(22);
digitalWrite(relay, LOW); //three spacing impulses
delay(66);
digitalWrite(relay, HIGH); //stop impulse
delay(31);
}
void translate(){ //translate character to baudot code
switch(i){
case 'A': //if it is an A
START(); //start impulse
MARKING_IMPULSE(); //marking impulse
MARKING_IMPULSE(); //marking impulse
SPACING_IMPULSE(); //spacing impulse
SPACING_IMPULSE(); //spacing impulse
SPACING_IMPULSE(); //spacing impulse
STOP(); //stop impulse
break;
case 'B':
START();
MARKING_IMPULSE();
SPACING_IMPULSE();
SPACING_IMPULSE();
MARKING_IMPULSE();
MARKING_IMPULSE();
STOP();
break;
case 'C':
START();
SPACING_IMPULSE();
MARKING_IMPULSE();
MARKING_IMPULSE();
MARKING_IMPULSE();
SPACING_IMPULSE();
STOP();
break;
}
}
void doString(){ //parse the string
BaudotCodeLength = BaudotCode.length(); //determine the length of the string
for(int x = 0;x<=BaudotCodeLength;x++){ //repeat parsing until end of the string
i = BaudotCode.charAt(x); //get charachter at position x
translate(); //translate charachter into baudot code
}
}
//----------------------------------------------------------
// LOOP
//----------------------------------------------------------
void loop(){
// put your main code here, to run repeatedly:
while (Serial.available()) { //when new information in serial
char inChar = (char)Serial.read(); //get the new byte
BaudotCode += inChar; //add it to BaudotCode
if (inChar == 'n') { //if receive as CR
Serial.println(BaudotCode);
doString(); //process the string
}
}
delay(1000); //delay between strings
BaudotCode = ""; //reset string
}

