I want work with HC-SR04, but i donât understand how write correct code.
How code the Tring and Echo contacts
Maybe exist class looks like for Parallax ping, where you just connect this
library, and use very simple functions to read data from sensor
You will need to start with the beginners guide book and then search codeshare for examples. Once you have a specific question, let us know and the community is always helpful.
I just realized that this a distance sensor. We have a new module that uses this sensor. You can just get ours and it will pug and play with any gadgeteer mainboard. Or get our drivers when it is published and roll your own hardware setup. This should be available in few weeks.
Depends on how universal he want his driver to be (not all pins on Panda II are interrupt capable). I would definitely use InterruptPort on echo pin in my projects.
Edit. Actually it reminded me that in my other driver I try to use InterruptPort first and if it fails I fall back to InputPort and polling: http://www.tinyclr.com/codeshare/entry/131
Same approach can be used with HC-SR04 and it would work no matter if pin has interrupts or not.
@ Bublik, if it helps a code snippet that i use to run the HC-SRO4. Itâs a bit different in that I measure the duration of the return pulse using the ADC rather than an interrupt or pin capture, but it works fine for my purposes. The drawback is that the time resolution on the measurement is about 100 microseconds which translates to about 1-2 cm. I had some problems trying to use an interrupt or pin capture, but if anyone seeâs a better way, Iâd be interested in seeing suggestions. Iâm looking at Architectâs codeshare.
This snippet just produces the sensor distance measurement in cm in the variable âroundedCMâ I used that variable to control some motor logic, but you can do what you want with it. Help you, I hope this does.
* start of UT section
* set up the HC-SRO4 trigger pulse with a PWM pin set to 10 us pulse every 50 ms
*/
GHIElectronics.NETMF.Hardware.PWM ut_trig = new PWM(PWM.Pin.PWM3);
ut_trig.Set(false);
ut_trig.SetPulse(50000000, 10000);
//Set up ADC
int k = 0; // k is used for counting during ADC and analysis of ADC array data
int j = 0; // counter for measuring length of HC-SRO4 ranging pulse
int val = 0; // val is the number of ADC periods the UT signal was high - (could also just use j for val)
// Int64 ticks0 = 0; // ticks0 marks the start of channel AD0.0 conversion - can be used to verify period
// Int64 ticks1 = 0; //ticks1 marks the end of channel AD0.0 conversion in ~ 100ns/tick
AnalogIn ai = new AnalogIn(AnalogIn.Pin.Ain0); //instantiate ADC pin 0
// setup variables
ai.SetLinearScale(0, 3300);
int[] ai_vals = new int[1001]; // ai_vals is the ADC storage array for AD0.0
// section 1 - The actual data acquisition
while (true)
{
// Debug.Print("start ADC Loop " + DateTime.Now.Ticks.ToString());
k = 0; j = 0; val = 0;
for (k = 0; k <= 600; k++) // quickly collect data points
{
ai_vals[k] = ai.Read();
}
// Debug.Print("ADC Loop Stopped " + DateTime.Now.Ticks.ToString());k = 0;
for (k = 1; k <= 599; k++) // quickly anaalyze data points
{
// count how many points above 3000
if (ai_vals[k] > 3000)
{
j++;
}
else
{
// ai_vals data point is below 3000 so check to see if a prior >3000 block was counted - if so get out of loop
if (ai_vals[k - 1] > 3000)
{
k = 600;
}
}
// Debug.Print(val.ToString()); // + " " + val1.ToString());
}
// Debug.Print("end of print loop " + DateTime.Now.Ticks.ToString());
// Debug.Print("end of analysis loop " + DateTime.Now.Millisecond.ToString());
val = j;
// calculate a distance from the number of data points above 3000
int roundedCM = (int)(val * 0.097 * 34 / 2);
Debug.Print("Distance: " + roundedCM.ToString() + " cm");
//Insert code to do somethings with roundedCM info here
}
in this lines:
GHIElectronics.NETMF.Hardware.PWM ut_trig = new PWM((PWM.Pin)FEZ_Pin.PWM.Di3);
AnalogIn ai = new AnalogIn (FEZ_Pin.AnalogIn.An0);
This is full code
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
namespace sonar
{
public class Program
{
public static void Main()
{
GHIElectronics.NETMF.Hardware.PWM ut_trig = new PWM((PWM.Pin)FEZ_Pin.PWM.Di3);
ut_trig.Set(false);
ut_trig.SetPulse(50000000, 10000);
//Set up ADC
int k = 0; // k is used for counting during ADC and analysis of ADC array data
int j = 0; // counter for measuring length of HC-SRO4 ranging pulse
int val = 0; // val is the number of ADC periods the UT signal was high - (could also just use j for val)
// Int64 ticks0 = 0; // ticks0 marks the start of channel AD0.0 conversion - can be used to verify period
// Int64 ticks1 = 0; //ticks1 marks the end of channel AD0.0 conversion in ~ 100ns/tick
AnalogIn ai = new AnalogIn (FEZ_Pin.AnalogIn.An0); //instantiate ADC pin 0
// setup variables
ai.SetLinearScale(0, 3300);
int[] ai_vals = new int[1001]; // ai_vals is the ADC storage array for AD0.0
// section 1 - The actual data acquisition
while (true)
{
// Debug.Print("start ADC Loop " + DateTime.Now.Ticks.ToString());
k = 0; j = 0; val = 0;
for (k = 0; k <= 600; k++) // quickly collect data points
{
ai_vals[k] = ai.Read();
}
// Debug.Print("ADC Loop Stopped " + DateTime.Now.Ticks.ToString());k = 0;
for (k = 1; k <= 599; k++) // quickly anaalyze data points
{
// count how many points above 3000
if (ai_vals[k] > 3000)
{
j++;
}
else
{
// ai_vals data point is below 3000 so check to see if a prior >3000 block was counted - if so get out of loop
if (ai_vals[k - 1] > 3000)
{
k = 600;
}
}
// Debug.Print(val.ToString()); // + " " + val1.ToString());
}
// Debug.Print("end of print loop " + DateTime.Now.Ticks.ToString());
// Debug.Print("end of analysis loop " + DateTime.Now.Millisecond.ToString());
val = j;
// calculate a distance from the number of data points above 3000
int roundedCM = (int)(val * 0.097 * 34 / 2);
Debug.Print("Distance: " + roundedCM.ToString() + " cm");
//Insert code to do somethings with roundedCM info here
}
}
}
@ BUBLIK - I can help with a few things but Iâm not sure which of your last two posts youâd like to focus on - one uses the ADC for timing and the other an interrupt.
What is the error message you receive and what does the intellisense message in Visual Studio tell you if you are hovering over an underlined error? My first guess would be to check that you have added all the references, as I donât see âusing GHIElectronics.NETMF.Harddware;â and you will need that for either version of your code.
Iâm also a bit puzzled about how you are setting up the trigger pulse in your last post. It looks like you are sending a a 10 ms pulse bracketed by two 2ms delays. I believe the recommended trigger signal is 10 microsenod pulse no faster than every 50 ms. That is what I used in the code I provided you earlier using PWM. Specifically,
GHIElectronics.NETMF.Hardware.PWM ut_trig = new PWM((PWM.Pin)FEZ_Pin.PWM.Di3);
ut_trig.Set(false);
ut_trig.SetPulse(50000000, 10000);
Error 2 âMicrosoft.SPOT.Hardware.OutputPortâ does not contain a constructor that takes no arguments â1â C Users/Yuriy/Desktop/111/sonar/sonar/sonar/Program.cs 14 48 sonar
and thir error for other code:
Error 5 Could not find the name of a type or namespace âAnalogInâ (missing a using directive or an assembly reference?) C: \ Users \ Yuriy \ Desktop \ 111 \ sonar2 \ sonar2 \ sonar2 \ Program.cs 26 31 sonar
You have to look at the documentation about the classes you are using. It has information about class constructors and parameters used in each constructor (if more than one).
For example there are two constructors for OutputPort:
These errors can be easily avoided by reading the documentation first. Otherwise this is going to be a very long thread.