Last active
December 16, 2015 15:17
-
-
Save raphaelschaad/3f753a9367408cb84bbd to your computer and use it in GitHub Desktop.
Arduino code for the COCOON, my final project for MIT Media Lab How to Make (almost) Anything http://fab.cba.mit.edu/classes/863.15/section.CBA/people/Schaad/index.html
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
// | |
// cocoon_embedded.ino | |
// | |
// Created by Raphael Schaad on 2015-12-12. | |
// This is free and unencumbered software released into the public domain. | |
// | |
// Right motor breakout board (looking at it from the front) | |
const int R_BTN1 = 3; // left (cocoon down); PD3; Arduino Uno use pin 3; hello.arduino use pin 7 | |
const int R_BTN2 = 4; // right (cocoon up); PD4; Arduino Uno use pin 4; hello.arduino use pin 8 | |
const int R_MOTORCCW = 5; // OUT1 (retract cocoon); PD5; Arduino Uno use pin 5; hello.arduino use pin 5 | |
const int R_MOTORCW = 6; // OUT2 (expand cocoon); PD6; Arduino Uno use pin 6; hello.arduino use pin 6 | |
// Left motor breakout board (looking at it from the front) | |
const int L_BTN1 = 8; // left (cocoon up) | |
const int L_BTN2 = 9; // right (cocoon down) | |
const int L_MOTORCCW = 10; // OUT1 (retract cocoon) | |
const int L_MOTORCW = 11; // OUT2 (expand cocoon) | |
// Buttons breakout | |
// Note: only plugged in for debug purposes | |
const int BTNUP = 2; | |
const int BTNDOWN = 12; | |
// LED breakout | |
// Note: standard debug LED on Arduino UNO | |
const int LEDUSR = 13; | |
char chr = 0; | |
bool r_motorCWOn = false; | |
bool r_motorCCWOn = false; | |
bool l_motorCWOn = false; | |
bool l_motorCCWOn = false; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
pinMode(LEDUSR, OUTPUT); | |
pinMode(R_MOTORCCW, OUTPUT); | |
pinMode(R_MOTORCW, OUTPUT); | |
pinMode(L_MOTORCCW, OUTPUT); | |
pinMode(L_MOTORCW, OUTPUT); | |
// If this was just INPUT, digitalRead(BTN1) would *always* be LOW. | |
// With INPUT_PULLUP, digitalRead(BTN1) will be HIGH by default, and LOW when the button is pressed. | |
// https://www.arduino.cc/en/Reference/Constants | |
// https://www.arduino.cc/en/Tutorial/DigitalPins | |
pinMode(R_BTN1, INPUT_PULLUP); | |
pinMode(R_BTN2, INPUT_PULLUP); | |
pinMode(L_BTN1, INPUT_PULLUP); | |
pinMode(L_BTN2, INPUT_PULLUP); | |
pinMode(BTNUP, INPUT_PULLUP); | |
pinMode(BTNDOWN, INPUT_PULLUP); | |
// baud rate must match sender | |
Serial.begin(9600); | |
Serial.println("hello"); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
digitalWrite(LEDUSR, !digitalRead(BTNUP)); | |
digitalWrite(LEDUSR, !digitalRead(BTNDOWN)); | |
if (chr == 'd' || digitalRead(BTNUP) == LOW) { | |
// retract | |
Serial.println("D"); | |
r_motorCCWOn = true; | |
l_motorCWOn = true; | |
} else if (chr == 'u' || digitalRead(BTNDOWN) == LOW) { | |
// expand | |
Serial.println("U"); | |
r_motorCWOn = true; | |
l_motorCCWOn = true; | |
} | |
chr = 0; | |
// 0-255x | |
const int kMotorSpeedPWMUp = 82; | |
const int kMotorSpeedPWMDown = 50; | |
const int kMotorSpeedPWMBack = 45; | |
// sync both motors due to tolerances | |
const int r_kMotorSpeedPWMOffset = +7; | |
// up | |
int r_motorSpeedPWMCW = r_motorCWOn ? kMotorSpeedPWMUp + r_kMotorSpeedPWMOffset : 0; | |
analogWrite(R_MOTORCW, r_motorSpeedPWMCW); | |
// down | |
int r_motorSpeedPWMCCW = r_motorCCWOn ? kMotorSpeedPWMDown + r_kMotorSpeedPWMOffset : 0; | |
analogWrite(R_MOTORCCW, r_motorSpeedPWMCCW); | |
// down | |
int l_motorSpeedPWMCW = l_motorCWOn ? kMotorSpeedPWMDown : 0; | |
analogWrite(L_MOTORCW, l_motorSpeedPWMCW); | |
// up | |
int l_motorSpeedPWMCCW = l_motorCCWOn ? kMotorSpeedPWMUp : 0; | |
analogWrite(L_MOTORCCW, l_motorSpeedPWMCCW); | |
const int kButtonHitDelay = 40; | |
const int kMotorBackDelay = 10; | |
// down | |
if (digitalRead(R_BTN1) == LOW) { | |
Serial.println("R_BTN1"); | |
r_motorCCWOn = false; | |
} | |
// up | |
if (digitalRead(R_BTN2) == LOW) { | |
Serial.println("R_BTN2"); | |
r_motorCWOn = false; | |
analogWrite(R_MOTORCW, 0); | |
delay(kButtonHitDelay); | |
analogWrite(R_MOTORCCW, kMotorSpeedPWMBack); | |
delay(kMotorBackDelay); | |
} | |
// down | |
if (digitalRead(L_BTN1) == LOW) { | |
Serial.println("L_BTN1"); | |
l_motorCWOn = false; | |
analogWrite(L_MOTORCW, 0); | |
analogWrite(L_MOTORCCW, kMotorSpeedPWMBack); | |
delay(kMotorBackDelay); | |
} | |
// up | |
if (digitalRead(L_BTN2) == LOW) { | |
Serial.println("L_BTN2"); | |
l_motorCCWOn = false; | |
} | |
} | |
// Called in-between loop() when new data arrives on the hardware serial RX. | |
void serialEvent() { | |
// while (Serial.available()) { -- in example .read() is wrapped into this block but it works w/o it | |
// Might be multiple bytes | |
chr = (char)Serial.read(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment