10. Daddy is coming home app for iPhone - Spark Core
In Spark Core Maker kit, I have Servo motor. It’s easy to control using Servo class as follows.
Servo servo; servo.attach(A0); // Attach servo to A0 int pos = servo.read(); // Get current position servo.write(pos + 10); // Move to new position
I think I can raise a flag using it and control it via iPhone app. See 6 seconds video below!
Spark Core Code
// ON/OFF Servo Motor // http://docs.spark.io/firmware/#libraries-servo #include <stdarg.h> Servo servo; int led = D0; #define PRINTF_BUFFER_SIZE 128 void Serial_printf(const char* fmt, ...) { char buff[PRINTF_BUFFER_SIZE]; va_list args; va_start(args, fmt); vsnprintf(buff, PRINTF_BUFFER_SIZE, fmt, args); va_end(args); Serial.println(buff); } bool isComingHome = false; int coming_home(String command) { isComingHome = true; return 0; } void setup() { // Use serial port for debug print Serial.begin(9600); // Expose coming_home api Spark.function("coming_home", coming_home); // Setup Servo servo.attach(A0); resetPosition(); pinMode(led, OUTPUT); digitalWrite(led, LOW); } void resetPosition() { int pos = servo.read(); for (int i = pos; i > 3; i--) { servo.write(i); delay(50); } } void loop() { if (isComingHome) { for (int i = 0; i < 50; i++) { digitalWrite(led, HIGH); delay(50); digitalWrite(led, LOW); delay(50); servo.write(i * (90.0 / 50.0)); } isComingHome = false; for (int i = 0; i < 50; i++) { digitalWrite(led, HIGH); delay(50); digitalWrite(led, LOW); delay(50); } resetPosition(); } }
iPhone Code
import UIKit class ViewController: UIViewController { let device_id = "your_device_id" let access_token = "your_access_token" override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor(red: 0.145, green: 0.145, blue: 0.145, alpha: 1.0) let screenWidth = UIScreen.mainScreen().bounds.size.width let screenHeight = UIScreen.mainScreen().bounds.size.height self.view.addSubview(createCircle(200, color:UIColor(red:0, green:0, blue:0, alpha: 1.0))) self.view.addSubview(createCircle(195, color:UIColor(red: 0.145, green: 0.145, blue: 0.145, alpha: 1.0))) self.view.addSubview(createCircle(185, color:UIColor(red:0, green:0, blue:0, alpha: 1.0))) self.view.addSubview(createCircle(180, color:UIColor(red:0.808, green:0.906, blue:0.271, alpha:1.0))) let button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton button.frame = CGRectMake( (screenWidth - 200) / 2.0, (screenHeight - 200) / 2.0, 200, 200) button.setTitle("Coming home!", forState: UIControlState.Normal) button.titleLabel!.font = UIFont.boldSystemFontOfSize(22) button.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside) self.view.addSubview(button) } func pressed(sender: UIButton!) { var request = NSMutableURLRequest(URL: NSURL(string: "https://api.spark.io/v1/devices/\(device_id)/coming_home")) var session = NSURLSession.sharedSession() request.HTTPMethod = "POST" request.HTTPBody = "access_token=\(access_token)".dataUsingEncoding(NSUTF8StringEncoding) var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in println("Response: \(response)") var strData = NSString(data: data, encoding: NSUTF8StringEncoding) println("Body: \(strData)") }) task.resume() } private func createCircle(radius: CGFloat, color: UIColor) -> UIView { let screenWidth = UIScreen.mainScreen().bounds.size.width let screenHeight = UIScreen.mainScreen().bounds.size.height let x = (screenWidth - radius) / 2.0 let y = (screenHeight - radius) / 2.0 let circle = UIView(frame: CGRectMake(x, y, radius, radius)) circle.alpha = 0.5 circle.layer.cornerRadius = radius / 2.0 circle.backgroundColor = color return circle } }