In this tutorial we will be going over how you can program a button to distinguish a long press versus a short press.
Requirements- Wia Dot One Buy yours here
- Wia Button Module Buy yours here
- Micro USB Cable Buy yours here
- Account on Wia
- Account at WiaYou will need to be registered and /or logged in to your Wia account athttps://www.wia.io/.
- Set up your Dot OneYou will need to have a set up Dot One and you can find the tutorial on how to do that Here.
- Set up your code projectNow you will need to create a space and then a code project on your Wia Dashboard
Insert the Button module on top of the Wia Dot One. Take care to connect the module pointing in the correct direction as shown on the picture above. You will notice that the rounded corners of the boards correspond to each other when inserted correctly.
Now connect the Wia Dot One to your computer with the micro USB cable. A solid red light should appear on the board. We are finished connecting everything we need!
Writing CodeThe following code distinguishes between short presses, long presses and hold presses of buttons. For our code we decided that a short press is less than 300 milliseconds while a long press is between 300 milliseconds and 2000 milliseconds and a hold is greater than 2000 milliseconds. You can adjust these values as you see fit by changing the variables at the top of the code “shortPress” and “longPress” follow along through the comments in the code to understand what is going on.
/*This code distinguises between long and short presses of a button to give your buttons more use! */
#include <WiFi.h>
#include <Wia.h>
const int button = 17;
int pressed = 0;
int released = 0;
int timeElapsed;
int shortPress = 300; // this means that all short presses must be less that 300ms
int longPress = 2000; // this means that all long presses must be less that 2000ms but greater than 300ms
Wia wiaClient = Wia();
void setup()
{
WiFi.begin();
delay(2500);
pinMode(button, INPUT_PULLDOWN);
}
void loop()
{
if(digitalRead(button) == HIGH)
{
pressed = millis(); // this finds the start time of when the button is pressed
while(digitalRead(button) == HIGH)
{// for as long as the button is pressed this will loop and the released time is recorded
released = millis();
}
timeElapsed = released - pressed; // this calculates the time the button was pressed for
if(timeElapsed < shortPress)
{/* if the time the button was pressed is less than the time defined to be a short press then we create an event "shortPress" */
wiaClient.createEvent("shortPress");
}
else if(timeElapsed > shortPress && timeElapsed <longPress)
{/* if it is greater than shorPress but less than longPress's upper bound, then we can conclude it was a long press and create an event "longPress"*/
wiaClient.createEvent("longPress");
}
else
{// if it is greater than the upper bound of long press than we can consider it a hold
wiaClient.createEvent("holdPress");
}
delay(100);// this delay keeps you from registering two short presses by accident
}
}
Then deploy your code to your button and you’re all set! Congratulations you have made a single button have three different purposes!
If you want to learn more about how to use your new button skills with Wia’s IoT devices check out some of our other projects and tutorials here.
Comments