Skills Dev IV: Working with Outputs - Motors and Movement Yulin
Made by Yulin Chen
Made by Yulin Chen
Created: November 20th, 2024
I try to build a customized Track buses system which we receive a motor alert when the time is nearly until the next bus.
Bus Track Version 1:
In this version, firstly, you can set the minute you want to be reminded by the motor from 3 to 20 minutes (by adjusting the "bustimeleftset"):
And then this time will be divided into three phases:
In the first 50% time, the motor will perform a slow speed (default :500) (you can adjust)
In the 50% to 30% time, the motor will perform a medium speed (default :300) (you can adjust, but is limited to be smaller than the first one)
In the 30% to 10% time, the motor will perform a fast speed (default :100) (you can adjust, but is limited to be smaller than the second one)
In addition, you can adjust the motor frequency in every phase by adjusting the value "motortimes" and how many times
Tips: The time every phase remain after motor performs will be calculate by the function: minite*timelef*phaseX - motortimes*delayX*2
which can automatically make up the time to every phase should last.
// Include Particle Device OS APIs
#include "Particle.h"
int solPin = D2;
bool startt = false;
int motortimes = 5;
int delay1 = 500;
int delay2 = 300;
int delay3 = 100;
int timelef = 5; //5,3,1 import when only have 5 minites 50%-20%-10%
int phase1 = 0.5;
int phase2 = 0.3;
int phase3 = 0.1;
int minite = 60000;
//you can set the time reminder how many minites left you want to be reminded
//according to the percentage to change the frequency
void setup(){
Serial.begin(9600);
pinMode( solPin, OUTPUT);
Particle.function("Start to Count", start);
Particle.function("SetDelay1", delay11);
Particle.function("SetDelay2", delay22);
Particle.function("SetDelay3", delay33);
Particle.function("motortimes",motortime);
Particle.function("bustimeleftset",lefttime);
}
void loop(){
if (startt){
for(int i = 0; i < motortimes; i++){
Serial.println("on");
digitalWrite(solPin, HIGH);
delay(delay1); //focus on delay
Serial.println("off");
digitalWrite(solPin, LOW);
delay(delay1);
// in 1-0.5
}
delay(minite*timelef*phase1 - motortimes*delay1*2);
for(int j = 0; j < motortimes; j++){
Serial.println("on");
digitalWrite(solPin, HIGH);
delay(delay2); //focus on delay
Serial.println("off");
digitalWrite(solPin, LOW);
delay(delay2);
} // in 0.5-0.3
delay(minite*timelef*phase2 - motortimes*delay2*2);
for(int l = 0; l < motortimes; l++){
Serial.println("on");
digitalWrite(solPin, HIGH);
delay(delay3); //focus on delay
Serial.println("off");
digitalWrite(solPin, LOW);
delay(delay3);
} // in 0.3-0.1
delay(minite*timelef*phase3 - motortimes*delay3*2);
}
startt = false;
}
int start(String cmd){
startt = true;
return 1;
}
int delay11(String cmd){
Serial.println("SetDelay1"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>1000)return -1;
delay1 = constrain(value, 0, 1000);
return 1;
}
int delay22(String cmd){
Serial.println("SetDelay2"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>1000)return -1;
if(value>delay1)return -1;
delay2 = constrain(value, 0, 1000);
return 1;
}
int delay33(String cmd){
Serial.println("SetDelay3"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>1000)return -1;
if(value>delay2)return -1;
delay3 = constrain(value, 0, 1000);
return 1;
}
int motortime(String cmd){
Serial.println("motortimes"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>5)return -1;
motortimes = constrain(value, 0, 5);
return 1;
}
int lefttime(String cmd){
Serial.println("bustimeleftset"+ cmd);
int value = cmd.toInt();
if(value<3)return -1;
if(value>20)return -1;
timelef = constrain(value, 3, 20);
return 1;
}
Click to Expand
Bus Track Version 2:
In this version, fewer thing you can adjust but can affect the whole process.
Compared to version 1, you can adjust the number of phases you need for the alert from 2 to 10 (default: 3)
And the speed of the motor in different phase will perform differently according to the phase number you set by multiply (phasenum/i)
Tips: The time every phase remain after motor performs will be calculate by the function: pow (1/2, i+1) *timelef*minite - (delay1*(phasenum/i)) which can automatically make up the time to every phase should last.
// Include Particle Device OS APIs
#include "Particle.h"
int solPin = D2;
bool startt = false;
int motortimes = 5;
int delay1 = 500;
int timelef = 5;
int phasenum = 3;
int minite = 60000;
//you can set the time reminder how many minites left you want to be reminded
//according to the percentage to change the frequency
//(1/2)**n
void setup(){
Serial.begin(9600);
pinMode( solPin, OUTPUT);
Particle.function("Start to Count", start);
Particle.function("SetDelay1", delay11);
Particle.function("motortimes",motortime);
Particle.function("bustimeleftset",lefttime);
Particle.function("phasenumm",phasenum1);
}
void loop(){
if (startt){
for(int i = 0; i < phasenum; i++){
for(int j = 0; j < motortimes; j++){
Serial.println("on");
digitalWrite(solPin, HIGH);
delay(delay1* (phasenum/i));
Serial.println("off");
digitalWrite(solPin, LOW);
delay(delay1* (phasenum/i));
}
delay(pow(1/2,i+1)*timelef*minite - (delay1*(phasenum/i)));
}
}
startt = false;
}
int start(String cmd){
startt = true;
return 1;
}
int delay11(String cmd){
Serial.println("SetDelay1"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>1000)return -1;
delay1 = constrain(value, 0, 1000);
return 1;
}
int motortime(String cmd){
Serial.println("motortimes"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>5)return -1;
motortimes = constrain(value, 0, 5);
return 1;
}
int lefttime(String cmd){
Serial.println("bustimeleftset"+ cmd);
int value = cmd.toInt();
if(value<0)return -1;
if(value>20)return -1;
timelef = constrain(value, 0, 20);
return 1;
}
int phasenum1(String cmd){
Serial.println("phasenumm"+ cmd);
int value = cmd.toInt();
if(value<2)return -1;
if(value>10)return -1;
phasenum = constrain(value, 2, 10);
return 1;
}
Click to Expand
~