Created
July 20, 2015 13:00
-
-
Save stonehippo/001a0c3a86ce68ac5ef3 to your computer and use it in GitHub Desktop.
Diorama controller
This file contains hidden or 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
/* | |
* Cantina Entryway Interactive Diorama | |
* | |
* The diorama currently consists of two motor-driven elements, a pair of rotating | |
* arms. This sketch manages the movement of those arms. | |
* | |
* Each arm is attached to a DC gearmotor, which are driven via a SN745510 Dual | |
* H-bridge. This IC allows for reversing the motors. | |
* | |
* Copyright (C) 2015 Cantina. All rights reserved. | |
* | |
*/ | |
const boolean ROTATION_CLOCKWISE = true; | |
const boolean ROTATION_COUNTERCLOCKWISE = false; | |
// Test controls | |
const int REVERSING_BUTTON_PIN = 4; | |
// MOTOR #1: The Sun Arm | |
/* | |
* NOTE - SEP1 - application | |
* The enable pin needs to have PWM so that we can use it to control motor speed. | |
*/ | |
const int SUN_ENABLE_PIN = 11; | |
/* | |
* NOTE - SIP1 - potential improvement | |
* IF the circuit included a single gate inverter IC, like a 74HC1G04, we could | |
* reduce the motor reversing pins by 1 per motor. The inverter turns a HIGH signal | |
* low and vice versa, which is perfect for this use, as it's what this code is | |
* doing with two pins per motor. | |
*/ | |
const int SUN_IN1_PIN = 12; // used to determine motor rotation direction | |
const int SUN_IN2_PIN = 13; // used to determine motor rotation direction | |
const int SUN_SPEED = 127; // can be 0-255 | |
boolean sunRotation = ROTATION_CLOCKWISE; | |
// MOTOR #2: The Moon Arm | |
/* | |
* NOTE - MEP1 - reference | |
* See SEP1 | |
*/ | |
const int MOON_ENABLE_PIN = 9; | |
/* | |
* NOTE - MIP1 - reference | |
* See SIP1 | |
*/ | |
const int MOON_IN1_PIN = 8; // used to determine motor rotation direction | |
const int MOON_IN2_PIN = 7; // used to determine motor rotation direction | |
const int MOON_SPEED = 127; // can be 0-255 | |
boolean moonRotation = ROTATION_COUNTERCLOCKWISE; | |
void setup() | |
{ | |
pinMode(SUN_ENABLE_PIN, OUTPUT); | |
pinMode(SUN_IN1_PIN, OUTPUT); | |
pinMode(SUN_IN2_PIN, OUTPUT); | |
pinMode(MOON_ENABLE_PIN, OUTPUT); | |
pinMode(MOON_IN1_PIN, OUTPUT); | |
pinMode(MOON_IN2_PIN, OUTPUT); | |
pinMode(REVERSING_BUTTON_PIN, INPUT_PULLUP); | |
// Enable serial for debug (may switch this to wifi comms later) | |
Serial.begin(115200); | |
} | |
void loop() | |
{ | |
// If the button is pressed, reverse from our normal directions | |
if (digitalRead(REVERSING_BUTTON_PIN) == LOW) { | |
sunRotation = ROTATION_COUNTERCLOCKWISE; | |
moonRotation = ROTATION_CLOCKWISE; | |
} else { | |
sunRotation = ROTATION_CLOCKWISE; | |
moonRotation = ROTATION_COUNTERCLOCKWISE; | |
} | |
setMotor(SUN_ENABLE_PIN, SUN_IN1_PIN, SUN_IN2_PIN, SUN_SPEED, sunRotation); | |
setMotor(MOON_ENABLE_PIN, MOON_IN1_PIN, MOON_IN2_PIN, MOON_SPEED, moonRotation); | |
} | |
void setMotor(int enablePin, int in1Pin, int in2Pin, int speed, boolean reverse) | |
{ | |
analogWrite(enablePin, speed); | |
/* | |
* NOTE - SET1 - reference | |
* See S1P1 | |
*/ | |
digitalWrite(in1Pin, ! reverse); | |
digitalWrite(in2Pin, reverse); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment