Loading...
Pages: [1]   Go Down
Author Topic: ultrasonic sensor arduino distance unstable  (Read 470 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i  use ultrasonic sensor arduino and  atmega328p  ,when i upload the arduino example ping code , the result will be normal,stable.but when i write my c code in atmel studio upload it, everything is fine except the distance stable,like this: 968,968...968, 138,968.....968,138. i don't know how it come out ? i just know after i send a pulse message and wait for the echo, the power will be  low,high,low(for the distance 968) , i think the situation 138 is just high low, could anyone tell me why? thank you!

  if someone need the code , i will upload it .
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 108
Posts: 6615
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you using the Arduino pulseIn() function in Atmel Studio?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes i rewrite the code by myself.      

Code:
int getPulseWidth(int pingID) {
int timecount = 0;
int val = 0 ;
if(pingID == 7) {
val = PIND>>pingID; // Append signal value to val
 
while(val ==0) { // Loop until pin reads a high value

val = PIND>>pingID;
// USART_putint(val);
}


while(val == 1)
{
// USART_send('b');
  val = PIND>>pingID;
//  USART_putint(val);
++timecount;  // Count echo pulse time
}
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 108
Posts: 6615
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pulseIn() returns an unsigned long.  Perhaps you are overflowing your 16-bit integer.

Do you not care what the actual time is?  It looks like your pulse width is returned in arbitrary units.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pulseIn() returns an unsigned long.  Perhaps you are overflowing your 16-bit integer.

Do you not care what the actual time is?  It looks like your pulse width is returned in arbitrary units.

I think so. That might be the reason.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

because i want detect the number of people who come through the door, so i don't need the accurate distance , i just set a distance , when someone come in or out the data will be small than the distance what i set. i'm sorry i forget to show all of the program , it should return timecount  .
Logged

Pages: [1]   Go Up
Print
 
Jump to: