You are on page 1of 11

Arduino + Processing Make a Radar Screen to Visualise Sensor Data from SRF-05 Part 1 Setting u!

u! t"e #ircuit and $ut!utting Values


First things first, we need to build our circuit. This is the easy bit! Well be using the Arduino to control a servo that will rotate our sensor around 180 degrees. The Arduino will then send the value fro the distance sensor along with the current angle of the servo to the serial !ort. "ow, #ve had a bit of an issue getting values fro y $%F0& fro Arduino to 'rocessing. #n Arduino it wor(s fine and the serial !ort shows the values correctly but in 'rocessing # couldnt get the values. $o e !eo!le suggest using Fir ata but the trouble with this is that it has very few of the functions that # need for e)a !le delay*icroseconds+, is not available when using Fir ata. Also # saw alot of !eo!le trying to get this wor(ing using different ode settings for the $%F0& but to be honest # li(e y code so # want to use that - # use se!arate echo and trigger !ins rather than a singular !in. # rec(on this ay also hel! you out should you be having !roble s getting other values.

Arduino SRF05 Sketc"


const int nu %eadings . 10/ int readings0nu %eadings1/ 22 the readings fro the analog in!ut int inde) . 0/ 22 the inde) of the current reading int total . 0/ 22 the running total int average . 0/ 22 the average int echo'in . 3/ 22 the $%F0&4s echo !in int init'in . 5/ 22 the $%F0&4s init !in unsigned long !ulseTi e . 0/ 22 variable for reading the !ulse unsigned long distance . 0/ 22 variable for storing the distance void setu!+, 6 22 a(e the init !in an out!ut7 !in*ode+init'in, 89T'9T,/ 22 a(e the echo !in an in!ut7 !in*ode+echo'in, #"'9T,/ 22 initiali:e the serial !ort7 for +int this%eading . 0/ this%eading ; nu %eadings/ this%eading<<, 6 readings0this%eading1 . 0/ = 22 set the serial !ort to begin logging values which 'rocessing will later read. $erial.begin+>?00,/ = void loo!+, 6 22 send the sensor a 10 icrosecond !ulse7 digitalWrite+init'in, @#A@,/ delay*icroseconds+&0,/ digitalWrite+init'in, B8W,/ 22 wait for the !ulse to return. The !ulse

22 goes fro low to @#A@ to low, so we s!ecify 22 that we want a @#A@Cgoing !ulse below7 22 converts the icrosecond ti e to distance in D* !ulseTi e . !ulse#n+echo'in, @#A@,/ distance . !ulseTi e2&8/ 22 subtract the last reading7 total. total C readings0inde)1/ 22 read fro the sensor7 readings0inde)1 . distance/ 22 add the reading to the total7 total. total < readings0inde)1/ 22 advance to the ne)t !osition in the array7 inde) . inde) < 1/ 22 if we4re at the end of the array... if +inde) E. nu %eadings, 6 22 ...wra! around to the beginning7 inde) . 0/ = 22 calculate the average7 average . total 2 nu %eadings/ 22 !rint out an A character to set the start of the value and hel! us 22 cast this value to a string and integer later on in 'rocessing. $erial.!rint+4A4,/ 22 now write out our value fro the sensor. $erial.!rint+average,/ 22 delay ust be the sa e as the delay in the !rocessing code delay+100,/ =

#ts not too different fro the !revious tutorial, Fust gets rid of the BGH and the ain thing to note is the serial !ort out!ut at the botto . We add an A to ar( the beginning of the value in the serial out!ut and we use this later on to hel! cast ou value as a string and then integer, for so e reason trying to !ass the serial value as an integer or trying to !arse it as such fails.

SRF05 Processing Sketc"


"ow for the 'rocessing code we Fust need this bit and thats it - were Fust loo(ing in the log file here to see the values being read correctly. Iasically we do a few chec(s to a(e sure we have no null values, that the out!ut were reading is of e)!ected length and has the correct identifying sy bol at the front, you dont have to use JA, this can actually be anything you want. 22 i !ort the serial libary for 'rocessing i !ort !rocessing.serial.K/

22 define a new !ort obFect $erial !ort/ 22 setu! the !ort, this referes to the !ort obFect above 22 D8*8 is y 9$I !ort that # use, your4s aybe anythign li(e D8*1, etc.. 22 >?00 is the baud rating +how uch data is transferred over a set ti e, 22 it is i !ortant to a(e sure your Arduino is out!utting to the sa e baud rating!! void setu!+,6 !ort . new $erial+this, LD8*8L, >?00,/ = 22 begin our out!ut for 'rocessing void draw+,6 22 delay 100 delay+100,/ illiseconds C this ust be the sa e delay used in the Arduino s(etch. reading the serial !ort

22 create a string to store the value fro $tring serialIuffer . !ort.read$tring+,/

22 chec( if the value is not null, that the value is greater that 3 and no ore than M characters if +serialIuffer !. null NN serialIuffer.length+, E. 3 NN serialIuffer.length+, ;. M, 6 22 chec( that the first character in the string is A 22 we add A to our out!ut to cast the serial value to a string ty!e. 22 #ts also handy as the A is doubled u! as a ar(er for a new value in the serial log if +serialIuffer.inde)8f+LAL, .. 0, 6 22 if this is true then re ove A serialIuffer . serialIuffer.substring+1,serialIuffer.length+,,/ = 22 double chec( to a(e sure there are no ore A4s in our string C otherwise we can4t convert to a nu ber if +serialIuffer.inde)8f+LAL, .. C1, 6 22 convert the string to an integer int i . #nteger.!arse#nt+serialIuffer,/ 22 !rint our integer !rintln+i,/ = = =

*a(e sure... *a(e sure that your serial !ort is set to the sa e baud rating in both the Arduino and 'rocessing s(etch. *a(e sure that the delay used in both e)ecutable ethods atches.

*a(e sure that the correct !ort is being read.

Arduino Radar Parts list


$%F0& 9ltrasonic range finder Arduino Heu ilanove w2 AT*GAA538 Ireadboard 2 'rototy!ing board Ou !er2 Donnector wires 1) $ervo +has to need no ore than &v su!!ly, Poull also need so e way to ount the sensor to the servo.

Arduino Radar Ser%o #ircuit


$traight forward, we have the Arduino !roviding !ower to the breadboard and we have the servo and the $%FC0& sharing this !ower. Then we have the servo out!ut !in going to Arduino digital !in > and the $%FC0& !ins going to digital !in 3 and 5. Poull notice that in y !ictures # have 3 servos - # Fust using the botto one of the !air to rotate the sensor round. 8n your servo youll need to figure out a way to ount the sensor on to the servo wheel - # used a lot of bluCtac! Poull also see #ve ounted y sensor vertically so that the when the servo oves therell be less interference with recieving values - !lacing the sensor horisontally see ed to give differences of u! to and so eti es over &c between the first and second readings. *y servos do tend to ove a bit so #ve used ore bluCta(2 odelling clay to hold the down and in !lace - if the servos ove other than the way theyre eant to then it eans dodgy readings.

Arduino SRF05 Radar Sketc"


The hardest bit - rotate the servo fro left to right, then right to left and for every degree of ove ent ta(e a series of readings and send the to the serial !ort. Well want to !roduce an average reading value for consistancy. 9nfortunately with this ultrasound sensor we have to be Quite slow to a(e sure were getting accurate values and we have to allow ti e for the signal to co e bac( each ti e and register in order to !roduce the average value. We do the rotation using a for loo! to count to 180 and for each iteration we ove the servo by <1 or C1 de!ending on which way were going - if youve hac(ed your servos then you can do a full 5?0 loo!. Huring this loo! we do another F8% loo! to count to 102 nu %eadings and for each iteration we add the distance easured to the total and after 10 readings we get our average by dividing the total by the nu ber of readings. Then reset the total and the counter to start again for the ne)t servo !osition. Finally before finishing the the F8% loo! for the servo we out!ut the servo !osition and average reading to the serial !ort each with a !receeding character for us to later use to identify the values when reading the serial !ort in 'rocessing. The last line is using !rintln which will start a new line for the ne)t set of values - each reading has its own line in the serial buffer a(ign it uch easier to get our values bac( out. 2K luc(ylarry.co.u(

%adar $creen Risualisation for $%FC0& $ends sensor readings for every degree oved by the servo values sent to serial !ort to be !ic(ed u! by 'rocessing K2 Sinclude 22 include the standard servo library $ervo left%ight$ervo/ 22 set a variable to a! the servo int left%ight'os . 0/ 22 set a variable to store the servo !osition const int nu %eadings . 10/ 22 set a variable for the nu ber of readings to ta(e int inde) . 0/ 22 the inde) of the current reading int total . 0/ 22 the total of all readings int average . 0/ 22 the average int echo'in . 3/ 22 the $%F0&4s echo !in int init'in . 5/ 22 the $%F0&4s init !in unsigned long !ulseTi e . 0/ 22 variable for reading the !ulse unsigned long distance . 0/ 22 variable for storing distance 2K setu! the !ins, servo and serial !ort K2 void setu!+, 6 left%ight$ervo.attach+>,/ 22 a(e the init !in an out!ut7 !in*ode+init'in, 89T'9T,/ 22 a(e the echo !in an in!ut7 !in*ode+echo'in, #"'9T,/ 22 initiali:e the serial !ort7 $erial.begin+>?00,/ = 2K begin rotating the servo and getting sensor values K2 void loo!+, 6 for+left%ight'os . 0/ left%ight'os ; 180/ left%ight'os<<, 6 22 going left to right. left%ight$ervo.write+left%ight'os,/ for +inde) . 0/ inde);.nu %eadings/inde)<<, 6 22 ta(e ) nu ber of readings fro the sensor and average the digitalWrite+init'in, B8W,/ delay*icroseconds+&0,/ digitalWrite+init'in, @#A@,/ 22 send signal delay*icroseconds+&0,/ 22 wait &0 icroseconds for it to return digitalWrite+init'in, B8W,/ 22 close signal !ulseTi e . !ulse#n+echo'in, @#A@,/ 22 calculate ti e for signal to return distance . !ulseTi e2&8/ 22 convert to centi etres total . total < distance/ 22 u!date total delay+10,/ = average . total2nu %eadings/ 22 create average reading ite if +inde) E. nu %eadings, 6 of the array inde) . 0/ total . 0/ = 22 reset the counts when at the last

$erial.!rint+LTL,/ 22 !rint leading T to ar( the following value as degrees $erial.!rint+left%ight'os,/ 22 current servo !osition $erial.!rint+LRL,/ 22 !receeding character to se!arate values $erial.!rintln+average,/ 22 average of sensor readings = 2K start going right to left after we got to 180 degrees sa e code as above K2 for+left%ight'os . 180/ left%ight'os E 0/ left%ight'osCC, 6 22 going right to left left%ight$ervo.write+left%ight'os,/ for +inde) . 0/ inde);.nu %eadings/inde)<<, 6 digitalWrite+init'in, B8W,/ delay*icroseconds+&0,/ digitalWrite+init'in, @#A@,/ delay*icroseconds+&0,/ digitalWrite+init'in, B8W,/ !ulseTi e . !ulse#n+echo'in, @#A@,/ distance . !ulseTi e2&8/ total . total < distance/ delay+10,/ = average . total2nu %eadings/ if +inde) E. nu %eadings, 6 inde) . 0/ total . 0/ = $erial.!rint+LTL,/ $erial.!rint+left%ight'os,/ $erial.!rint+LRL,/ $erial.!rintln+average,/ = =

Arduino + Processing Make a Radar Screen to Visualise Sensor Data from SRF-05 Part & Visualising t"e Data
This is where all the wor( is done to read an inter!ret the values fro the servo and the sensor. #f the readings are to erratic then you wont have nice sha!es. Also if you dont allow enough ti e to the signals to be sent bac( then youll get false distance readings. $o this code is only as good as your Arduino code and sensor setu!. Too( e a few evenings to wor( this out to get it how # wanted, the hardest bit is the trigono etry involved which isnt that difficult, everything else is done by loo!s and a few #F state ents. # use the F8% loo!s alot because the all the ele ents can be dis!layed !rogra atically without having to write each rectangle, cirle and line to the screen with their own state ent.

#f youre not fa iliar with 'rocessing then head over to 'rocessing.org. 'rocessing is a tool2 #HG that lets you !rogra and code gra!hics and ani ation, its free to use and !retty !owerful - best !art is it wor(s hand in hand with Arduino in the sa e D2D<< style of code, letting us ta(e data fro Arduino and whatevers !lugged into it and then visualise it on screen, e.g. li(e a radar screen. First we have to setu! our variables, bac(ground and load in the serial !ort libraries to ensure we can read the data sent by the Arduino. We also have to include a function fro the serial library called serialGvent+, which listens for data being sent and allows us to read the data easily. We do so e easy string wor(, s!litting the serial !ort lines to get the servo !osition and sensor value. Also we can setu! the radar screen, drawing the te)t, easure ents and dis!lay grid. With !rocessing whats nearest the to! of the draw+, function is dis!layed first with everything subseQuentally drawn on to! of this. $o our lines and te)t will be at the botto of the function so it will always be visible. To draw the lines and easure ents we use a F8% loo!. The draw+, function draws one fra e, so we basically reCdraw the fra e 180 ti es - we use a cou!le of arrays to store the !revious values to a(e it loo( continuous. "ow that we have those we can then begin to dis!lay the values on our screen. We use a F8% loo! to loo! through each ite in our array, newRalue and oldRalue. These are setu! to hold 181 ite s - 1 ite !er servo !osition with 1 e)tra Fust in case, we loo! through these to constantly dis!lay the !revious readings - if we were to use the servo !osition itself to iterate through the array then no !revious data would be dis!layed since the servo !osition is always changing. Thoughout we need to calculate the T and P coCordinates of each servo !osition and sensor distance reading. To get these values we use trigono etry using sine and cosine and converting the servo !osition to a radian using the sensor reading as the distance fro the center fro which to draw the !oint. To learn ore about this and to save e writing it u! chec( out this tutorial at 'rocessing.org.

'"e Sketc"
Ielow is the code used to create the radar screen, it has co ents to hel! e)!lain. Gverything used is an in built function of the 'rocessing language and you can find the reference for the functions that # use in the reference section on the 'rocessing.org website. $o ethods such as fill+,, stro(e+,, rect+,, line+, etcU 2K luc(ylarry.co.u( %adar $creen Risualisation for $%FC0& *a!s out an area of what the $%FC0& sees fro a to! down view. Ta(es and dis!lays 3 readings, one left to right and one right to left. His!lays an average of the 3 readings His!lays otion alert if there is a large difference between the 3 values. K2 i !ort !rocessing.serial.K/ 22 i !ort serial library

$erial y'ort/ 22 declare a serial !ort float ), y/ 22 variable to store ) and y coCordinates for vertices int radius . 5&0/ 22 set the radius of obFects int w . 500/ 22 set an arbitary width value int degree . 0/ 22 servo !osition in degrees int value . 0/ 22 value fro sensor int otion . 0/ 22 value to store which way the servo is !anning int01 newRalue . new int01811/ 22 create an array to store each new sensor value for each servo !osition int01 oldRalue . new int01811/ 22 create an array to store the !revious values. 'Font yFont/ 22 setu! fonts in 'rocessing int radarHist . 0/ 22 set value to configure %adar distance labels int first%un . 0/ 22 value to ignore triggering otion on the first 3 servo swee!s 2K create bac(ground and serial buffer K2 void setu!+,6 22 setu! the bac(ground si:e, colour and font. si:e+V&0, M&0,/ bac(ground +0,/ 22 0 . blac( yFont . createFont+LverdanaL, 13,/ te)tFont+ yFont,/ 22 setu! the serial !ort and buffer y'ort . new $erial+this, $erial.list+,011, >?00,/ y'ort.buffer9ntil+4n4,/ = 2K draw the screen K2 void draw+,6 fill+0,/ 22 set the following sha!es to be blac( no$tro(e+,/ 22 set the following sha!es to have no outline elli!se+radius, radius, V&0, V&0,/ 22 draw a circle with a width2 height . V&0 with its center !osition +) and y, set by the radius rect*ode+DG"TG%,/ 22 set the following rectangle to be drawn around its center rect+5&0,M03,800,100,/ 22 draw rectangle +), y, width, height, if +degree E. 1V>, 6 22 if at the far right then set otion . 12 true we4re about to go right to left otion . 1/ 22 this changes the ani ation to run right to left = if +degree ;. 1, 6 22 if servo at 0 degrees then we4re about to go left to right otion . 0/ 22 this sets the ani ation to run left to right = 2K setu! the radar swee! K2 2K We use trigon etry to create !oints around a circle. $o the radius !lus the cosine of the servo !osition converted to radians $ince radians 0 start at >0 degrees we add 180 to a(e it start fro the left Adding <1 +i, each ti e through the loo!s to ove 1 degree atching the one degree of servo ove ent cos is for the ) left to right value and sin calculates the y value since its a circle we !lot our lines and vertices around the start !oint for everything will

always be the center. K2 stro(eWeight+V,/ 22 set the thic(ness of the lines if + otion .. 0, 6 22 if going left to right for +int i . 0/ i ;. 30/ i<<, 6 22 draw 30 lines with fading colour each 1 degree further round than the last stro(e+0, +10Ki,, 0,/ 22 set the stro(e colour +%ed, Areen, Ilue, base it on the the value of i line+radius, radius, radius < cos+radians+degree<+180<i,,,Kw, radius < sin+radians+degree<+180<i,,,Kw,/ 22 line+start ), start y, end ), end y, = = else 6 22 if going right to left for +int i . 30/ i E. 0/ iCC, 6 22 draw 30 lines with fading colour stro(e+0,300C+10Ki,, 0,/ 22 using standard %AI values, each between 0 and 3&& line+radius, radius, radius < cos+radians+degree<+180<i,,,Kw, radius < sin+radians+degree<+180<i,,,Kw,/ = = 2K $etu! the sha!es ade fro the sensor values K2 no$tro(e+,/ 22 no outline 2K first swee! K2 fill+0,&0,0,/ 22 set the fill colour of the sha!e +%ed, Areen, Ilue, begin$ha!e+,/ 22 start drawing sha!e for +int i . 0/ i ; 180/ i<<, 6 22 for each degree in the array ) . radius < cos+radians++180<i,,,K++oldRalue0i1,,/ 22 create ) coordinate y . radius < sin+radians++180<i,,,K++oldRalue0i1,,/ 22 create y coordinate verte)+), y,/ 22 !lot vertices = end$ha!e+,/ 22 end sha!e 2K second swee! K2 fill+0,110,0,/ begin$ha!e+,/ for +int i . 0/ i ; 180/ i<<, 6 ) . radius < cos+radians++180<i,,,K+newRalue0i1,/ y . radius < sin+radians++180<i,,,K+newRalue0i1,/ verte)+), y,/ = end$ha!e+,/ 2K average K2 fill+0,1V0,0,/ begin$ha!e+,/ for +int i . 0/ i ; 180/ i<<, 6 ) . radius < cos+radians++180<i,,,K++newRalue0i1<oldRalue0i1,23,/ 22 create average y . radius < sin+radians++180<i,,,K++newRalue0i1<oldRalue0i1,23,/ verte)+), y,/ = end$ha!e+,/ 2K if after first 3 swee!s, highlight otion with red circleK2 if +first%un E. 5?0, 6 stro(e+1&0,0,0,/ stro(eWeight+1,/

noFill+,/ for +int i . 0/ i ; 180/ i<<, 6 if +oldRalue0i1 C newRalue0i1 E 5& WW newRalue0i1 C oldRalue0i1 E 5&, 6 ) . radius < cos+radians++180<i,,,K+newRalue0i1,/ y . radius < sin+radians++180<i,,,K+newRalue0i1,/ elli!se+), y, 10, 10,/ = = = 2K set the radar distance rings and out !ut their values, &0, 100, 1&0 etc.. K2 for +int i . 0/ i ;.?/ i<<,6 noFill+,/ stro(eWeight+1,/ stro(e+0, 3&&C+50Ki,, 0,/ elli!se+radius, radius, +100Ki,, +100Ki,,/ fill+0, 100, 0,/ no$tro(e+,/ te)t+#nteger.to$tring+radarHist<&0,, 580, +50&CradarHist,, &0, &0,/ radarHist<.&0/ = radarHist . 0/ 2K draw the grid lines on the radar every 50 degrees and write their values 180, 310, 3M0 etc.. K2 for +int i . 0/ i ;. ?/ i<<, 6 stro(eWeight+1,/ stro(e+0, &&, 0,/ line+radius, radius, radius < cos+radians+180<+50Ki,,,Kw, radius < sin+radians+180<+50Ki,,,Kw,/ fill+0, &&, 0,/ no$tro(e+,/ if +180<+50Ki, E. 500, 6 te)t+#nteger.to$tring+180<+50Ki,,, +radius<10, < cos+radians+180<+50Ki,,,K+w<10,, +radius<10, < sin+radians+180<+50Ki,,,K+w<10,, 3&,&0,/ = else 6 te)t+#nteger.to$tring+180<+50Ki,,, radius < cos+radians+180<+50Ki,,,Kw, radius < sin+radians+180<+50Ki,,,Kw, ?0,M0,/ = = 2K Write infor ation te)t and values. K2 no$tro(e+,/ fill+0,/ rect+5&0,M03,800,100,/ fill+0, 100, 0,/ te)t+LHegrees7 L<#nteger.to$tring+degree,, 100, 580, 100, &0,/ 22 use #nteget.to$tring to convert nu eric to string as te)t+, only out!uts strings te)t+LHistance7 L<#nteger.to$tring+value,, 100, M00, 100, &0,/ 22 te)t+string, ), y, width, height, te)t+L%adar screen code at luc(ylarry.co.u(L, &M0, 580, 3&0, &0,/ fill+0,/ rect+V0,?0,1&0,100,/ fill+0, 100, 0,/

te)t+L$creen Xey7L, 100, &0, 1&0, &0,/ fill+0,&0,0,/ rect+50,&5,10,10,/ te)t+LFirst swee!L, 11&, V0, 1&0, &0,/ fill+0,110,0,/ rect+50,V5,10,10,/ te)t+L$econd swee!L, 11&, >0, 1&0, &0,/ fill+0,1V0,0,/ rect+50,>5,10,10,/ te)t+LAverageL, 11&, 110, 1&0, &0,/ noFill+,/ stro(e+1&0,0,0,/ stro(eWeight+1,/ elli!se+3>, 115, 10, 10,/ fill+1&0,0,0,/ te)t+L*otionL, 11&, 150, 1&0, &0,/ = 2K get values fro serial !ort K2 void serialGvent +$erial y'ort, 6 $tring )$tring . y'ort.read$tring9ntil+4n4,/ 22 read the serial !ort until a new line if +)$tring !. null, 6 22 if theres data in between the new lines )$tring . tri +)$tring,/ 22 get rid of any whites!ace Fust in case $tring getT . )$tring.substring+1, )$tring.inde)8f+LRL,,/ 22 get the value of the servo !osition $tring getR . )$tring.substring+)$tring.inde)8f+LRL,<1, )$tring.length+,,/ 22 get the value of the sensor reading degree . #nteger.!arse#nt+getT,/ 22 set the values to variables value . #nteger.!arse#nt+getR,/ oldRalue0degree1 . newRalue0degree1/ 22 store the values in the arrays. newRalue0degree1 . value/ 2K sets a counter to allow for the first 3 swee!s of the servo K2 first%un<</ if +first%un E 5?0, 6 first%un . 5?0/ 22 (ee! the value at 5?0 = = =

You might also like