/*
Step Tracker
*/
// variables that won't change
const int powerPin = 0;
const int buttonPin = 1;
const int tiltPin = 2;
const int ledPin = 4;
// variables that will change:
int buttonState = 0;
int tiltState = 0;
int systemOn = 0;
int currentTime = 0;
int peace = 0;
int step = 0;
int systemControl(String command);
void setup() {
// establish serial monitor connection
Serial.begin(9600);
// set up all input and output
pinMode(powerPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(tiltPin, INPUT);
pinMode(alarmPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set up cloud variables and functions
Particle.function("Knok", systemControl);
Particle.variable("system", systemOn);
Particle.function("Reset", resetStep);
Particle.variable("step", step);
//Particle.subscribe("Knok Knok", systemControl);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed
if (buttonState == HIGH) {
changeSystemStatus();
}
// start the system if system is off and button is pressed
if (systemOn == 1) {
tiltSensorOn();
digitalWrite(powerPin, HIGH);
} else {
digitalWrite(powerPin, LOW);
}
currentTime = millis();
while(millis() < currentTime + 100){
//wait 1 second
}
}
// read sensor data and respond correspondingly
void tiltSensorOn() {
// read the state of the tilt sensor value:
tiltState = digitalRead(tiltPin);
// alarm is tilt sensor give potisive feedback
if (tiltState == HIGH) {
// playAlarm();
step = step + 1;
if (peace == 1) {
Serial.println(step);
Particle.publish("Alert", String(step), PUBLIC);
}
peace = 0;
} else {
digitalWrite(ledPin, LOW);
digitalWrite(alarmPin, LOW);
if (peace == 0) {
// Serial.println("It's a peaceful day");
// Particle.publish("Status", "It's a peaceful day", PRIVATE);
}
peace = 1;
}
}
// change on/off status
void changeSystemStatus() {
if (systemOn == 0) {
systemOn = 1;
digitalWrite(powerPin, HIGH);
// Serial.println();
Serial.println("System: On");
// Serial.println("Security Check: On");
// Serial.println("Full Protection: On");
Serial.println();
Particle.publish("Status", "System: On", PRIVATE);
} else {
systemOn = 0;
digitalWrite(powerPin, LOW);
// Serial.println();
Serial.println("System: Off");
// Serial.println("Security Check: Off");
// Serial.println("Full Protection: Off");
Serial.println();
Particle.publish("Status", "System: Off", PRIVATE);
}
}
// control the system over the cloud
int systemControl(String command) {
// find out the state of the system
if (command == "ON") {
systemOn = 1;
return 1;
} else if (command == "OFF") {
systemOn = 0;
return 1;
} else {
return -1;
}
}
int resetStep(String command) {
if (command == "RESET") {
step = 0;
return 1;
} else {
return 1;
}
return -1;
}
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. .