Jump to content

Ultra-sonic Range Finder -> Arduino -> RS232 -> Atari Computer

Posted by k-Pack, 26 January 2015 · 1,124 views

Ultrasound arduino rs232
Ultra-sonic Range Finder -> Arduino -> RS232 -> Atari Computer If capturing text from your Arduino to a terminal buffer is all you want to do, you are lucky.  You may want to collect data and use it in your own programs. Unfortunately there are not a lot of example programs to learn from and the 850 interface manual can be cryptic for us mortals. Persistence does pay off. Usually the “let’s try this” style of debugging will eventually get you to the proper combination of port settings and program logic.
I wanted to hook up a sensor to the Arduino and then read the output through the RS232 port using Atari BASIC.  I choose the ultrasound sensor.  It will measure the time it takes a sound wave to travel from the sender to an object and back. If you know the speed of sound you can calculate the distance.  
Now you can imagine that by moving your hand in front of the sensor you can program the SOUND command to change pitch.  You can imagine it but doing it may not be so practical.
The Arduino Side
There are several manufactures of ultrasound sensors.  Some will have 3 pins, some will have 4.  Both have a +5 volt and GrouND pins.  If the sensor has 4 pins, one will trigger the sound pulse and the other change logic states when it senses the sound. 
Attached Image
I am using the Parallax Ping))) sensor.  One data pin is used as output to trigger the pulse. Before the pulse can travel back from an object the pin is set as input.  The Ping))) sensor was previously wired and running using an example program in “Arduino: A Quick-Start Guide”.  Remember, the Arduino has to be programmed prior to installing the RS232 shield.  The following program was uploaded, the RS232 shield installed and the Sensor was wired to pin 7, +5 volt and ground.
// Modified Program to read Parallax PING))) sensor
// Send time ultrasound travels to object

const unsigned int pingPin = 7;
const unsigned int baudRate = 9600;

void setup(){

void loop(){
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  pinMode(pingPin, INPUT);
  const unsigned long duration = pulseIn(pingPin, HIGH);
  if (duration == 0) {
  }else {
The program can be tested by reading the input with a terminal program or the Arduino IDE through an RS232 port. Or, remove the shield and wire up the sensor, then read the data through the USB port.
Attached Image
The ATARI side
The first thing you may want to do is connect the Arduino to the 850 and read the data stream using a terminal program on your 8 bit.  Be sure to set the baud rate at 9600.
The BASIC program was derived from an example in the 850 manual.  Appendix 9 – User Programs, 5. Reading a Digitizer: More Input Than BASIC Can Handle (Page 77-78).  The Arduino is programmed to continuously send data to a buffer.  The Atari asks for input slower then it is being placed in the buffer. When the buffer is full the Arduino overwrites the data.  To keep the buffer in sync with the Atari, a double input method is used in line 100.  Read the buffer the first time to empty it and then the second time retrieves the latest reading. (I recommend finding a copy of the manual to read.)
The first test program reads the data from the Arduino and prints out the text input and the value. Getting the SS$ into a floating point variable was a little more involved then INSS = VAL(SS$).  The first problem was that a graphic character (CHR$(10)?) was placed at the beginning of the data.  Not sure where it came from but it needed to be ignored.  The second was knowing where the string ended. Line 105 and 110 were used to take care of this problem (most of the time).   
10 DIM SS$(10):REM holds input from sound sensor
20 XIO 36,#1,15,0,"R1:":REM set baud
30 OPEN #1,5,0,"R1:":REM open port
40 XIO 40,#1,0,0,"R1:":REM start I/O
100 INPUT #1,SS$:INPUT #1,SS$:REM clear buffer,get reading
105 X=LEN(SS$):REM Length of text string
110 INSS=VAL(SS$(2,X)):REM convert text to floating point
120 ? SS$,INSS:REM print data to screen
130 GOTO 100:REM do it again
Now it was a matter of scaling the INSS variable for the SOUND command.  That was easy but…….. it was not expected that the SOUND would turn off when the INPUT command was executed .  I tried a few things to see if it would stay on but to no avail.   This listing is just the last iteration that was tried. 
10 DIM SS$(10)
20 XIO 36,#6,15,0,"R1:"
30 OPEN #6,5,0,"R1:"
40 XIO 40,#6,0,0,"R1:"
100 INPUT #6,SS$:INPUT #6,SS$
102 CLOSE #6
105 X=LEN(SS$)
110 INSS=VAL(SS$(2,X))
120 ? SS$,INSS
125 SOUND 3,INSS/31,10,10
126 FOR Y=1 TO 100:NEXT Y
130 GOTO 30
This last program simply uses a simple calculation to estimate the distance from the sensor to the object.  It might come in handy for a security system or if you’re building a robot with an Atari brain.  It was noted that every so often a string length error would occur. I would bet that when this happened the string variable SS$ was empty causing a length error when trying to read SS$(2,0).  This was solved with the TRAP 100 command. Anytime an error occurred it would try again.    
10 DIM SS$(10)
20 XIO 36,#6,15,0,"R1:"
30 OPEN #6,5,0,"R1:"
40 XIO 40,#6,0,0,"R1:"
100 INPUT #6,SS$:INPUT #6,SS$
102 TRAP 100
105 X=LEN(SS$)
110 INSS=VAL(SS$(2,X))
120 ? INSS/29/2;" CM"
126 FOR Y=1 TO 100:NEXT Y
130 GOTO 100
In hindsight I might have started with a lower baud rate and picked another sensor.  Optimization of hardware and software will have to wait for another day and another project. 
Analog Computing Pocket Reference Card, The, Analog Computing Magazine, 1985
Atari DOS 2.5: 1050 Disk Drive Owner’s manual, Atari Corp. 1985, CO72033-001 Rev. A.
Atari 850 Interface Module Operator’s Manual, Atari, Inc. 1980, CO15953 Rev. 1
Poole, Ion, Your Atari Computer, OSBORNE/McGraw-Hill 1982 
Schmidt, Maik, Arduino: A Quick-Start Guide, Pragmatic Programmers, LLC. 2011. (Page 87-93)

Latest Visitors