Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Force Sensor - Easy Example and Special Class


  • Please log in to reply
4 replies to this topic

#1 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 26 April 2011 - 01:02 AM

I know some people wanted more analog examples and here it is! This was much easier than I thought, force sensors are not that complicated. These are called force sensitive resistors as well. Here is is:

*Always use a resistor! Dont imitate my bad habit!
Attached File  force sensor schem.PNG   103.22KB   179 downloads

Attached File  force sensor schem 2.PNG   41.62KB   188 downloads

Attached File  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

:D enjoy

Attached Files



#2 ULight.Me

ULight.Me

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationMilton, Ontario

Posted 22 June 2011 - 01:44 AM

I know some people wanted more analog examples and here it is! This was much easier than I thought, force sensors are not that complicated. These are called force sensitive resistors as well. Here is is:

Attached File  force sensor schem.PNG   103.22KB   179 downloads

Attached File  force sensor schem 2.PNG   41.62KB   188 downloads

Attached File  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);
            }

See easy! :D enjoy


How would you do this using event handling and interrupt port?

#3 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 22 June 2011 - 03:54 AM

How would you do this using event handling and interrupt port?


It is an analog pin so it doesnt have an event structure built in... I'll have a class posted up there in a sec that will allow you to do this.

Update: class is up there, let me know if it helps

#4 u1054371

u1054371

    New Member

  • Members
  • Pip
  • 1 posts

Posted 28 June 2011 - 09:20 AM

Can you upload the source code for this project? Thanks for all, good job.

#5 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 28 June 2011 - 07:43 PM

Can you upload the source code for this project?

Thanks for all, good job.


I attached a zip of the project.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.