/* Sources:
* Simple Stepper Motor Control Exaple Code
* by Dejan Nedelkovski, www.HowToMechatronics.com
*/
// This value will store the last time we published an event
long lastPublishedAt = 0;
// this is the time delay before we should publish a new event from
// this device
int publishAfter = 1000;
// DO NOT CHANGE THIS
String sharedEventName = "diot/2019/smartstudio/";
// device specific event name
String deviceEventName = "napTech/";
//Boolean 'closed' is used to store whether the blinds are:
//opened (false)
//or closed (true)
bool closed = true;
//Boolean 'napping' is used to store whether someone is napping on the mattress:
//no one is napping (false)
//or someone is napping (true)
bool napping = false;
//static value 'stepsToChange' is used to determine the steps or time
//it takes to open or close blinds
int stepsToChange = 3000;
const int stepPin = D3; //pin to pulse for steps
const int dirPin = D2; //pin to change step direction
const int pressPin = D6;
void setup(){
// Set pins as outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
// We'll want to subscribe to an event thats fairly unique
// From the Particle Docs
// A subscription works like a prefix filter.
// Basically this will match any event that starts with 'diot/2019/smartstudio/'
// This is a feature we'll useto figure out if our event comes from
// this device or another (see publishMyEvent below)
Particle.subscribe( sharedEventName , handleSharedEvent );
publishMyEvent("","napTechStarted");
openCurtain();
}
//Particle loop auto-generated
void loop() {
}
void publishMyEvent(String eventID, String data)
{
// Remember that a device can publish at rate of about 1 event/sec,
// with bursts of up to 4 allowed in 1 second.
// Back to back burst of 4 messages will take 4 seconds to recover.
// So we want to limit the amount of publish events that happen.
// check that it's been 10 secondds since our last publish
if( lastPublishedAt + publishAfter < millis() )
{
// Remember our subscribe is matching "db2018/paired/"
// We'll append the device id to get more specific
// about where the event came from
// System.deviceID() provides an easy way to extract the device
// ID of your device. It returns a String object of the device ID,
// which is used to identify your device.
// This will build 'diot/2019/smartstudio/myEvent/A13231245345A078'
String eventName = sharedEventName + deviceEventName + eventID +System.deviceID();
// now we have something like "diot/2019/paired/0123456789abcdef"
// and that corresponds to this devices info
// then we share it out
Particle.publish( eventName, data);
// And this will get shared out to all devices using this code
// we just pubished so capture this.
lastPublishedAt = millis();
}
}
// Our event handlde requires two bits of information
// This gives us:
// A character array that consists of the event name
// A character array that contains the data published in the event we're responding to.
void handleSharedEvent(const char *event, const char *data)
{
// Now we're getting ALL events published using "db2018/paired/"
// This includes events from this device.
// So we need to ignore any events that we sent.
// Let's check the event name
String eventName = String( event ); // convert to a string object
// This gives us access to a bunch of built in methods
// Like indexOf()
// Locates a character or String within another String.
// By default, searches from the beginning of the String,
// but can also start from a given index,
// allowing for the locating of all instances of the character or String.
// It Returns: The index of val within the String, or -1 if not found.
// We can use this to check if our event name contains the
// id of this device
String deviceID = System.deviceID();
if( eventName.indexOf( deviceID ) != -1 ){
// if we get anything other than -1
// the event came from this device.
// so stop doing stuff
return;
}
//if the event didnt come from this device
else if( eventName.indexOf( "isNapping" ) > -1 ){
publishMyEvent("napping", (String)closed);
}
else if( eventName.indexOf( deviceEventName ) > -1 ){
// A my event triggered
// and event says open
if( eventName.indexOf( "open" ) > -1 ){
//publish an event that allows one to check what triggered the opening
publishMyEvent("opening_", (String)event+":"+data);
startNap();
}
//Event Closes the curtain
else if( eventName.indexOf( "close" ) > -1 ){
//publish an event that allows one to check what triggered the closing
publishMyEvent("closing_", (String)event+":"+data);
stopNap();
}
}
}
//Start the
void startNap(){
//close the curtains
closeCurtain();
//some one is napping
napping = true;
publishMyEvent("napping", (String)napping);
}
//Stop the nap
void stopNap(){
//open the curtains
openCurtain();
//no one is napping
napping = false;
publishMyEvent("napping", (String)napping);
}
bool openCurtain(){
//if the curtains are not already closed
if(closed){
//set the direction to be counter clockwise
stepperFWD();
//step for the predetermined amount of time
for(int i = 0; i < stepsToChange; i++){
motorStep();
}
//wait for one second
delay(1000);
}
//set the boolean closed accordingly
closed = false;
return closed;
}
//close the curtains
bool closeCurtain(){
//if the curtains are not already closed
if(!closed){
//set the direction to be counter clockwise
stepperREV();
//step for the predetermined amount of time
for(int i = 0; i < stepsToChange; i++){
motorStep();
}
//wait for one second
delay(1000);
}
//set the boolean closed accordingly
closed = true;
return closed;
}
//getter function for closed
bool isCurtainClosed(){
return closed;
}
//getter function napping
bool isNapping(){
return napping;
}
//have the stepper motor take one step
void motorStep(){
digitalWrite(stepPin, HIGH);
delay(1);//delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delay(1);//delayMicroseconds(500);
}
//change the stepper direction to forward
//direction to open curtain
void stepperFWD(){
digitalWrite(dirPin, HIGH);
}
//change the stepper direction to reverse
//direction to close curtain
void stepperREV(){
digitalWrite(dirPin, LOW);
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .