/*
* Project Balance
* Description: A scale that shows work/life balance.
* Author: Corey Williams, Sarthak Giri, Hanyuan Zhang
* Date: 2/4/2018
*/
int servoPin = A4; //Assign a pin for the servo motor.
Servo myServo; //Initiate servo.
float workHours = 0.0; //Assign a variable for the number of hours worked in a day.
int servoPos = 100;
int balPos = 0;
void setup() {
myServo.attach(servoPin); //Assign the servo motor to a pin.
Particle.function("servo", servoControl); //Assign a function to change the motor position to the particle cloud.
Particle.variable("servoPos", &servoPos, INT); //Assign servo position as a variable to the particle cloud.
Particle.function("hours", timeToMove); //Assign a function to prototype the balance movement.
myServo.write (servoPos); //Sets the servo position so that the balance arm is horizontal.
Serial.begin(9600); //Start the serial monitor if needed.
}
void loop() { //All actions happen in the particle cloud so nothing is needed here.
}
int servoControl (String command) {
int newPos = command.toInt(); //Turn input string into an integer.
servoPos = constrain(newPos, 0, 180); //Contrain between 0 and 180 degrees.
myServo.write (servoPos); //Move the motor.
return servoPos;
}
int calcPos(float workHours, int servoPos) { //Calculate the direction that the servo should rotate.
if (workHours >= 8.5) { //If you worked longer than 8.5 hours in a day...
servoPos = servoPos + 2; //Rotate the servo 2 degrees toward "work".
Serial.println("Working too much.");
Serial.println(servoPos);
}
if (workHours <= 7.5) { //If you worked less than 7.5 hours in a day...
servoPos = servoPos - 2; //Rotate the servo 2 degrees toward "life".
Serial.println("Partying too much.");
Serial.println(servoPos);
}
if (workHours < 8.5 && workHours > 7.5) { //If you worked between 7.5 hours and 8.5 hours in a day...
if (servoPos >= 102) { //Bring the arm more horizontal if it is leaning toward "work".
Serial.println("Back to balance.");
servoPos = servoPos - 2;
Serial.println(servoPos);
}
if (servoPos <= 98) { //Bring the arm more horizontal if it is leaning toward "life".
Serial.println("Back to balance.");
servoPos = servoPos + 2;
Serial.println(servoPos);
}
}
return servoPos;
}
int timeToMove (String command) {
float workHours = command.toFloat(); //Convert an input string into a float.
Serial.println(workHours);
balPos = calcPos(workHours, servoPos); //Calculate which direction the servo should move.
servoPos = constrain(balPos, 70, 130); //Keep the balance arm from moving too far.
myServo.write(servoPos); //Move the servo.
return servoPos;
}
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. .