Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: Jerry on March 26, 2013, 12:06:18 PM

Title: ultrasonic Sensor hc sr04...
Post by: Jerry on March 26, 2013, 12:06:18 PM
have anyone work on these sensors with atmega16
Title: Re: ultrasonic Sensor hc sr04...
Post by: jlizotte on March 26, 2013, 03:58:44 PM
I'm using them on the Arduino Mega 2560 Rev 3. What do you need to know?

I'm working with one so far. I mounted it on a servo in an enclosure. When it detects an obstacle, it looks left, forward, and right, then will move my robot in the direction with the most clearance. I just finished testing it on my desk, and it's ready to install on my bot. You can see the video of the test at http://www.johnlizotte.com/robotics/testing-servo-mounted-ultrasonic-sensors/ (http://www.johnlizotte.com/robotics/testing-servo-mounted-ultrasonic-sensors/). (Sorry I don't know how to embed it here or compress it enough to post here yet).

Once it's mounted and my code is tweaked, I have 5 more of them to install.
Title: Re: ultrasonic Sensor hc sr04...
Post by: billhowl on March 27, 2013, 05:57:38 AM
http://vduenasg.blogspot.sg/2012/01/ultrasonido-hc-sr04.html (http://vduenasg.blogspot.sg/2012/01/ultrasonido-hc-sr04.html)
Code: [Select]
# Include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <HD44780/HD44780.h>
 
/ / __MENSAJES
const  uint8_t  SMS1 []  PROGMEM = "HC-SR04 VDUENASG \ 0 " ;
const  uint8_t  SMS2 []  PROGMEM = "Ultrasound \ 0 " ;
const  uint8_t  SMS3 []  PROGMEM = "centimeters \ 0 " ;
 
uint8_t  run ;
uint16_t  ultra ,  time ;
 
void  initialize ()
{
DDRD | = 1 << 7 | 0 << 6 ;
DDRA = 0 b11110111 ;
stdout = & lcd_str ;
/ / Starting Stream
LCDinit ();

TCCR1A = 0 B00000000 ;
TCCR1B = 0 B11000000 ;
/ / Capture Input - Normal - Rising Edge
TIMSK = 0 B00100000 ;
/ / Enabled input capture interrupt
CopyStringtoLCD ( SMS1 , 1 , 0 );
_delay_ms ( 1000 );
CopyStringtoLCD ( SMS2 , 3 , 1 );
_delay_ms ( 2000 );
LCDclr ();
sei ();
/ / Interrupts are enabled
}
 
/ / __INTERRUPCIONES
ISR ( TIMER1_CAPT_vect )
{
if  ( run == 0 )
{
TCCR1B = 0 B10000011 ;
/ / Clk/64 - Falling edge
run = 1 ;
}
else  if ( run == 1 )
{
TCCR1B = 0 B11000000 ;
/ / Rising edge - stop clk
ultra = TCNT1 ;
TCNT1 = 0x00 ;
/ / Reset counter
run = 0 ;
}
}
 
/ / MAIN __PROGRAMA
int  main ( void )
{
initialize ();
while ( 1 )
{
PORTD | = 1 << 7 ;
_delay_us ( 10 );
PORTD & = 0 << 7 ;

_delay_ms ( 50 );

LCDGotoXY ( 5 , 0 );
time = ( ultra * 8 );
printf ( "% d" , time / 58 );
printf ( "% c" , '.' );

if  ( time % 58 < 10 )
{
printf ( "% c" , '0 ' );
printf ( "% d" , time % 58 );
}
else
printf ( "% d" , time % 58 );
CopyStringtoLCD ( SMS3 , 2 , 1 );
}

}