Skip to content

Commit

Permalink
updated spin arduino code for android app
Browse files Browse the repository at this point in the history
  • Loading branch information
ttseng committed Jul 27, 2016
1 parent 167f1f8 commit a2b5fdc
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions Arduino/spin.ino
Expand Up @@ -6,25 +6,24 @@
Stepper Motor Code Modified from BILDR: http://bildr.org/2011/06/easydriver/
Author: Tiffany Tseng
Last Updated: 10/19/2015
Changed motor_speed for faster spins
Last Updated: 07/26/2016
Added FSKModem responses for Android app
*/

#include <Stepper.h>
#include <SoftModem.h>
#include <ctype.h>

#define DIR_PIN 2
#define STEP_PIN 9
#define BTN_PIN 12

#define LED_PIN 13
#define LED_PIN_1 A2
#define LED_PIN_2 A3
#define LED_PIN_3 A4
#define LED_PIN_4 A5

#define PHONE_PIN 4

SoftModem modem;

Expand All @@ -33,9 +32,15 @@ boolean cancelled = false;
int numRotations = 15;// should be 15
int currentRotation = 0;
char val;
float rotAmt = 240;
float rotAmt = 240; // the number of degrees the stepper should turn for each increment
float motor_speed = 0.2;

int buttonState = HIGH;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 20;
boolean spinCreated = false;

void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
Expand All @@ -44,45 +49,50 @@ void setup() {
pinMode(LED_PIN_2, OUTPUT);
pinMode(LED_PIN_3, OUTPUT);
pinMode(LED_PIN_4, OUTPUT);
pinMode(PHONE_PIN, INPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
Serial.begin(9600);
modem.begin();
}

void loop(){

if(rotating == false){
// keep two of the LED lights on when the turntable is powered
digitalWrite(LED_PIN_3, HIGH);
digitalWrite(LED_PIN_1, HIGH);
}

if(modem.available ()) {
// RECEIVED SIGNAL FROM IPHONE
int input = modem.read ();
Serial.print(input);
Serial.println(input);

if(input == 253){
if(input == 253 || input == 93){
// begin spin - flash LEDs
Serial.print(" start ");
modem.write(0xFC); // send confirmation signal to app
Serial.println(" start ");
modem.write(0xFC); // send confirmation signal to app (252)
flashLEDsOnce();
currentRotation = 0;
rotating = true;
cancelled = false;
delay(1000);
}else if(input==251){
}else if(input==251 || input == 91){
// stop arduino
rotating = false;
cancelled = true;
spinCreated = false;
currentRotation = 0;
Serial.print(" stop - writing stop signal");
modem.write(0xFE);
}else if(input == 234){
// setup start received
Serial.println(" stop - writing stop signal");
modem.write(0xFE); // 254
}else if(input == 234 || input == 74){
// setup signal received (for one-time setup when user first downloads app)
flashLEDsOnce();
modem.write(0xEC);
rotateDeg(-rotAmt/2, motor_speed);
rotateDeg(rotAmt/2, motor_speed);
}else if(input == 255){
// spin has been created -> (used for enabling remote shutter button)
spinCreated = true;
}
}

Expand Down Expand Up @@ -138,25 +148,51 @@ void loop(){
takePhoto();
}
}

// EASY BUTTON STUFF

int reading = digitalRead(BTN_PIN);
if(reading != lastButtonState){

lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH && spinCreated) {
Serial.println("write start to app");
modem.write(0xFC); // send confirmation signal to app
flashLEDsOnce();
currentRotation = 0;
rotating = true;
cancelled = false;
delay(1000);
}
}
}

lastButtonState = reading;

}

void singleRotation(){
rotating = true;
ledsOn();
// Serial.println("motor turning");
rotateDeg(-rotAmt, motor_speed);
// Serial.println("motor stopped");
ledsOff();
takePhoto();
}

void takePhoto(){
// snap a photo
// Serial.println("start delay");
// time it takes to capture photo and add imageview to app
// time it takes to capture photo and add imageview in app
delay(1950);
// Serial.println("end delay");
}

void rotate(int steps, float speed){
Expand Down

0 comments on commit a2b5fdc

Please sign in to comment.