*Always use a resistor! Dont imitate my bad habit!
force sensor schem.PNG 103.22KB 179 downloads
force sensor schem 2.PNG 41.62KB 188 downloads
force sensor test.jpg 790.22KB 138 downloads
Magic! Oh right the code....
AnalogInput input = new AnalogInput(Pins.GPIO_PIN_A0); PWM led = new PWM(Pins.GPIO_PIN_D10); input.SetRange(0, 100); while (true) { led.SetDutyCycle((uint)input.Read()); Thread.Sleep(1); }
To enable event firing use this class I created [since analog port doesnt have built in event stuff]
using System; using Microsoft.SPOT; using SecretLabs.NETMF.Hardware; using Microsoft.SPOT.Hardware; using System.Threading; namespace Sensors { public delegate void ChangedValue(int oldValue, int newValue, DateTime time); public class Force { public event ChangedValue ValueChanged; Thread updater; AnalogInput input; int oldVal = -1; public int ignoreChangeLessThan = 0; public Force(Cpu.Pin analogPin) { input = new AnalogInput(analogPin); } public void SetRange(int min, int max) { input.SetRange(min, max); } public int Read() { return input.Read(); } public void UpdateConstantly(bool update) { if (update) { updater = new Thread(new ThreadStart(Update)); oldVal = input.Read(); updater.Start(); } else { if (updater != null) updater.Suspend(); } } private void Update() { while (updater.IsAlive) // as long as we havent stoped the thread... { int newVal = input.Read(); // get the current value if ((newVal < oldVal - ignoreChangeLessThan) || (newVal > oldVal + ignoreChangeLessThan)) { if(ValueChanged != null) ValueChanged(oldVal, newVal, DateTime.Now); // if the value has changed more than the filter value then fire the event oldVal = newVal; // now this is the new value to base a change off of. } } } } }
Use:
static Force f = new Force(Pins.GPIO_PIN_A0); public static void Main() { f.ValueChanged += new ChangedValue(f_ValueChanged); f.UpdateConstantly(true); Thread.Sleep(Timeout.Infinite); // just keep on waiting... forever } static void f_ValueChanged(int oldValue, int newValue, DateTime time) { Debug.Print("Old: " + oldValue + " || New: " + newValue); //f.UpdateConstantly(false); // stop the updating using that line }
When ignoreChangeLessThan is 0 you'll get tons of fires, i suggest a setting of 3 depending on your range or sensor you'll have to fiddle with values.
Sample output (tiny part of it) with ignoreChangeLessThan being 0:
Old: 810 || New: 823
Old: 823 || New: 836
Old: 836 || New: 847
Old: 847 || New: 855
Old: 855 || New: 865
Old: 865 || New: 873
Old: 873 || New: 878
Old: 878 || New: 886
Old: 886 || New: 892
Old: 892 || New: 899
Old: 899 || New: 903
Old: 903 || New: 907
Old: 907 || New: 911
enjoy