Created
May 31, 2016 06:44
-
-
Save luciditee/5c5428b72778977503efc726ba2bc896 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "Useless Box" program code by Will Preston | |
// Made for class assignment with the Savannah College of Art & Design | |
// Advanced Physical Computing, Spring 2016. | |
#include <Servo.h> | |
// Defines are used in place of consts for globals because they compile | |
// to a more compact size in program memory. | |
// DEBUG_LED defines pin 13 as an area you can connect an LED to view | |
// when the program state changes (i.e. when the switch is thrown). | |
#define UB_SWITCH_READ A1 | |
#define UB_SWITCH_THRESHOLD 915 | |
#define UB_POLL_RATE 100 | |
#define UB_SLEEP_TIME 300 | |
#define UB_SERVO_PIN 6 | |
#define UB_DEBUG_LED 13 | |
#define UB_ARM_EXTENDED 120 | |
#define UB_ARM_HIDDEN 20 | |
#define UB_ARM_DELAY 600 | |
#define UB_BAUD 9600 | |
// Uncomment this for debug mode (LED and serial support). | |
// #define UB_DEBUG 1 | |
// Define program state machine. | |
enum UBState { | |
idle, moving, retracting | |
}; | |
// Declare program state and servo reference. | |
UBState state; | |
Servo arm; | |
// Runs at the start of the program. | |
void setup() { | |
// Initialize pins. | |
// We use an analog pin for the switch read because this code was designed | |
// with a 4-pack of AAA batteries in mind, which outputs +6VDC, meaning that | |
// reading it as digital data may result in inaccurate data as it expects | |
// a certain voltage and current (in other words, "always on," which is not | |
// true). | |
pinMode(UB_SWITCH_READ, INPUT); | |
// Debug pin and serial output init. | |
pinMode(UB_DEBUG_LED, OUTPUT); | |
Serial.begin(UB_BAUD); | |
// Program state init and servo init. | |
state = idle; | |
arm.attach(UB_SERVO_PIN); | |
} | |
// Called constantly by the call stub in crt1 (or arduino's equivalent) | |
void loop() { | |
// Poll switch value. | |
const int switchValue = analogRead(UB_SWITCH_READ); | |
// Check to alter state if we are idle. | |
if (state == idle) { | |
// Tell the servo to be at idle position here. | |
arm.write(UB_ARM_HIDDEN); | |
// If switch is pressed, change state. | |
if (switchValue >= UB_SWITCH_THRESHOLD) { | |
state = moving; | |
} | |
} else if (state == moving) { | |
// If we are moving, we need to set the servo position. | |
arm.write(UB_ARM_EXTENDED); | |
delay(UB_ARM_DELAY); // delay position | |
state = retracting; | |
} else if (state == retracting) { | |
// If we've extended the arm, we should switch it back. | |
arm.write(UB_ARM_EXTENDED); | |
delay(UB_ARM_DELAY); // delay position | |
state = idle; | |
} | |
// Write out debug values. | |
#ifdef UB_DEBUG | |
digitalWrite(UB_DEBUG_LED, switchValue); | |
Serial.println(switchValue); | |
#endif | |
// Delay predetermined poll rate time. | |
delay(UB_POLL_RATE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment