Menu

Read sharp distance sensor and display distance in cm in the terminal

2017-03-10
2017-03-10
  • stan cartwright

    stan cartwright - 2017-03-10
    ;Read Sharp 2YOA21 distance sensor output with arduino uno
    ;and display distance in cm in a terminal.
    #chip mega328p,16
    #option Explicit
    'USART settings
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    ;
    #define a2d portc.0 ; uno A0 pin
    dir a2d in
    dim a2dval as Integer
    ;
    do
    a2dval=readad10(a2d)
    a2dval=6050/a2dval
    if a2dval>=2 then
      a2dval=a2dval-2
    end if
    if a2dval<10 then
      a2dval=10
    end if
    if a2dval>80 then
      a2dval=80
    end if
    HSerPrint "Distance= "
    HSerPrint a2dval
    HSerPrint " CM"
    HSerPrintCRLF
    wait 100 ms;
    loop
    
     
  • Anobium

    Anobium - 2017-03-11

    Stan,

    luckily this code worked. readad10() requires the AD numeration not the port - this probably worked by luck. The readad10( an0 ) or the. AVR equivilent should have been used. If this code is moved to. AN1 it is likely to fail.

     
  • stan cartwright

    stan cartwright - 2017-03-11

    I deleted #include <UNO_mega328p.h> because it worked without it.

    define a2d portc.0 ; uno A0 pin....A1 would be portc.1. I don't follow your point. You mean #define a2d portc.1 ; uno A1 pin and a2dval=readad10(a2d) wouldn't work? Why not? Shall I try it?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.