;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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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?