NEWBIE ON DIGITAL INPUT
#1
Posted 25 January 2011 - 06:02 PM
#2
Posted 25 January 2011 - 07:23 PM
#3
Posted 26 January 2011 - 07:15 AM
Check out the button tutorial on the Arduino site. http://www.arduino.c...Tutorial/Button
thank's for reply. I've seen this tutorial yesterday after posting my problem.
Only 1 question : When you pin the 5 V on arduino is it an input (you are giving 5v to arduino ) or it is an output (arduino gives you the 5 v ) ?
#4
Posted 26 January 2011 - 07:31 AM
#5
Posted 26 January 2011 - 09:16 AM
Attached Files
#6
Posted 26 January 2011 - 09:21 AM
#7
Posted 26 January 2011 - 11:10 AM
Hi Randall,
Do you have a pull-down resistor in your switch circuit?
Try to wire the switch to the GND pin and to D10 instead. Use the internal pull-up resistor ("ResistorMode.PullUp") when you create the InterruptPort. Does that work?
Chris
WOW! not it works!
only problem is that sometimes events triggers 2 or 3 times .
I've tried also to not use interrupt mode but read directly button state (code below) and sees a little better.
So what is better using INTERRUPT or read BUTTON STATE?
Really thank for your patience!
OutputPort luce = new OutputPort(Pins.ONBOARD_LED, false);
bool buttonState = true;
byte counter = 49;
while (true)
{
if (buttonState != button.Read() )
{
if (buttonState == false)
{
inTray = new byte[3];
inTray[0] = counter ;
inTray[1] = 13;
inTray[2] = 10;
port.Write(inTray, 0, inTray.Length);
buttonState = true;
counter++ ;
if (counter == 59)
{ counter = 49; }
}
else
{
buttonState = false;
}
}
}
#8
Posted 26 January 2011 - 12:33 PM
WOW! not it works!
only problem is that sometimes events triggers 2 or 3 times .
A physical problem with buttons/switches is something called DEBOUNCE. In essence the make-break cycle is not clean and instead of getting a single event it registers as multiple events. Look into filtering the input either in hardware or software. I saw a software option somewhere with interrupts.
But take everything I have said with a rock size chunk of salt as I have only had my Netduino for 2 days and still learning. Some of the experts I'm sure will set you right. Cheers and Happy Australia Day.
#9
Posted 26 January 2011 - 07:11 PM
#10
Posted 30 January 2011 - 12:20 PM
1. Debounces by ignoring any further interrupts within 500ms. (Tune this if you need to catch quicker presses.)
2. Adds two events OnPress and OnLongPress. For long press it checks every 500ms and if the button's still down after 3s it fires OnLongPress.
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using System.Threading; namespace Fred.Useful { /// <summary> /// A debounced button wired to an input pin. /// </summary> /// <remarks> /// Ignores clicks within 200ms /// Has simple OnPress and OnLongPress events (3s) /// /// </remarks> public class DebouncedButton { const int DEBOUNCE_TIME = 200; const int HOLD_SAMPLE_TIME = 500; const int HOLD_COUNT = 6; InterruptPort _button; DateTime _lastButtonPress = DateTime.Now; Timer _timer; public delegate void PressedDelegate(); int _holdCounter; public DebouncedButton(Cpu.Pin pin) { _button = new InterruptPort(pin, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow); _button.OnInterrupt += button_Pressed; _timer = new Timer(timer_Fired, null, Timeout.Infinite, Timeout.Infinite); } public PressedDelegate OnPress; public PressedDelegate OnLongPress; private void button_Pressed(uint data1, uint data2, DateTime time) { // Debounce - ignore multiple presses within 200ms if (time < _lastButtonPress.AddMilliseconds(DEBOUNCE_TIME)) return; _lastButtonPress = time; _holdCounter = 0; // Set up timer for OnLongPress if (OnLongPress != null) _timer.Change(HOLD_SAMPLE_TIME, HOLD_SAMPLE_TIME); // Fire event if (OnPress != null) OnPress(); } /// <summary> /// Check whether the button's still being held down /// </summary> /// <param name="state"></param> public void timer_Fired(object state) { if (_button.Read()) { // Button released _timer.Change(Timeout.Infinite, Timeout.Infinite); return; } Debug.Print("Holding x" + _holdCounter.ToString()); // Keep sampling. Button's not been held downlong enough if (++_holdCounter < HOLD_COUNT) return; // Fire event if (OnLongPress != null) OnLongPress(); // Stop timer _timer.Change(Timeout.Infinite, Timeout.Infinite); } } }
- EnergySmithe likes this
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users