From d0cdce22e0f31d15c7028e5e312add60b98c253f Mon Sep 17 00:00:00 2001 From: Josh Siegle Date: Fri, 15 Jun 2012 13:02:38 -0400 Subject: [PATCH] Updated include files for Arduino 1.0 --- LCD.ino | 123 ++++++++++++++++++++++++++++++++++++++ debugCommands.ino | 47 +++++++++++++++ memory.ino | 28 +++++++++ servo.ino | 73 +++++++++++++++++++++++ twister.ino | 149 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 420 insertions(+) create mode 100644 LCD.ino create mode 100644 debugCommands.ino create mode 100644 memory.ino create mode 100644 servo.ino create mode 100644 twister.ino diff --git a/LCD.ino b/LCD.ino new file mode 100644 index 0000000..3fe6ffb --- /dev/null +++ b/LCD.ino @@ -0,0 +1,123 @@ +// Tetrode Twister Control Software +// Copyright (C) 2011 Josh Siegle +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// Functions for writing to the LCD screen + +void startupScreen(int a, int b) { + + lcd.print(" Tetrode Twister "); + + lcd.setCursor(0,1); + + lcd.print("for Arduino v"); + lcd.print(a); + lcd.print("."); + lcd.print(b); + + delay(3000); + +} + +void showTurns() { + + if (inputs.isUpdated || twister.isUpdated) { + + lcd.clear(); + lcd.print("FWD REV COUNT"); + lcd.setCursor(0,1); + + printTurns(inputs.fwdTurns); + printTurns(inputs.revTurns); + lcd.print(" "); + printTurns(twister.totalTurns); + + inputs.isUpdated = false; + twister.isUpdated = false; + } +} + +void allDone() { + + lcd.clear(); + lcd.print(" Fire up that "); + lcd.setCursor(0,1); + lcd.print(" heat gun! "); + +} + +void printTurns(int nTurns) { + + if (nTurns < 100 && nTurns >= -9) { + lcd.print(" "); + } + if (nTurns < 10 && nTurns >= 0) { + lcd.print(" "); + } + lcd.print(nTurns); + lcd.print(" "); + +} + +void resumeDialog() { // allow user to cancel restart + + boolean resume = true; + + int remainingSeconds = 5; + + lcd.clear(); + lcd.print("Twisting error"); + lcd.setCursor(0,1); + lcd.print("CONTINUE in "); + lcd.print(remainingSeconds); + + unsigned long theTime = millis(); + + while (remainingSeconds > 0) { + while (millis() - theTime < 1000) { + inputs.check(twister.isTurning, verPCB); + if (inputs.buttonState == LOW) { + remainingSeconds = 0; + resume = false; + EEPROM.write(0,0); + EEPROM.write(1,0); + delay(500); + break; + } + } + lcd.setCursor(12,1); + remainingSeconds--; + lcd.print(remainingSeconds); + theTime = millis(); + } + + if (resume) { + twister.totalTurns = remainingTurns; + twister.isTurning = true; + twister.isUpdated = true; + + switch (turnDirection) { + case 0: + twister.isTurningFWD = true; + break; + case 1: + twister.isTurningFWD = false; + break; + } + } + +} + + diff --git a/debugCommands.ino b/debugCommands.ino new file mode 100644 index 0000000..cfd3a6e --- /dev/null +++ b/debugCommands.ino @@ -0,0 +1,47 @@ +// Tetrode Twister Control Software +// Copyright (C) 2011 Josh Siegle +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +void printPhotocellInfo() { + + for (int p = 0; p < 2; p++) { + + Serial.print("Photocell "); + Serial.print(p); + Serial.print(" "); + Serial.print(photocells.values[p]); + Serial.print(" "); + Serial.println(photocells.thresh[p]); + + } + +} + +void printVariables() { + + Serial.print(twister.totalTurns); + Serial.print(" "); + Serial.print(inputs.fwdTurns); + Serial.print(" "); + Serial.print(inputs.revTurns); + Serial.print(" "); + Serial.print(inputs.buttonState); + Serial.print(" "); + Serial.print(photocells.counter); + Serial.print(" "); + +} + diff --git a/memory.ino b/memory.ino new file mode 100644 index 0000000..268f764 --- /dev/null +++ b/memory.ino @@ -0,0 +1,28 @@ +// Tetrode Twister Control Software +// Copyright (C) 2011 Josh Siegle +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +void updateEEPROM() { + + if (twister.totalTurns != remainingTurns) { + EEPROM.write(0,twister.totalTurns); + } + + if (twister.isTurningFWD != !turnDirection) { + EEPROM.write(1,!twister.isTurningFWD); + } + +} diff --git a/servo.ino b/servo.ino new file mode 100644 index 0000000..c3c910b --- /dev/null +++ b/servo.ino @@ -0,0 +1,73 @@ +// Tetrode Twister Control Software +// Copyright (C) 2011 Josh Siegle +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +void updateServo() { + + // settings for rotation speed + const int fwdspeed = 1000; + const int stopped = 1500; + const int revspeed = 2000; + + // set rotation status + if (!twister.isTurning) { + myservo.writeMicroseconds(stopped); + } + else { + if (twister.isTurningFWD) { + myservo.writeMicroseconds(fwdspeed); + } + else { + myservo.writeMicroseconds(revspeed); + } + } + + // check for threshold crossings + if (twister.totalTurns >= inputs.fwdTurns && twister.isTurningFWD) { + myservo.writeMicroseconds(stopped); + + buzzer.high(); + delay(500); + + twister.isTurningFWD = false; + + // This is where it's fucking up! + } else if (twister.totalTurns <= inputs.fwdTurns - inputs.revTurns && twister.isTurning && !twister.isTurningFWD) { + + myservo.writeMicroseconds(stopped); + + buzzer.low(); + + twister.isTurning = false; + twister.isTurningFWD = true; + + twister.totalTurns = 0; + + delay(1000); + + allDone(); + + delay(5000); + + inputs.isUpdated = true; + + updateEEPROM(); + + } + +} + + + diff --git a/twister.ino b/twister.ino new file mode 100644 index 0000000..9b107a7 --- /dev/null +++ b/twister.ino @@ -0,0 +1,149 @@ + // Tetrode Twister Control Software +// Copyright (C) 2011 Josh Siegle +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + + +// ------------------------------ // +// SOFTWARE VERSION NUMBER: +int ver[2] = { + 3, 1}; +// ------------------------------ // +// PCB VERSION NUMBER: +int verPCB = 1; + +// -------------------------------------------------------------------- // +/* DEBUG MODE? + + If debugMode is set to 'true', photocell values and thresholds + will be printed to the serial port. These can be visualized + using the accompanying Processing sketch, 'PhotocellVisualization.pde' + + If printState is set to 'true', total turns, forward turns, reverse turns, + button state, and counter state will be printed to the serial port. + + */ + +boolean debugMode = false; +boolean printState = false; + +/* SAVE STATE? + + If saveState is set to true, the Arduino will save its current status + after each turn. The state can then be restored if the Arduino loses + power or is reset while turning. + +*/ + +boolean saveState = false; + +// -------------------------------------------------------------------- // + +// BEGIN ACTUAL CODE: + +// Includes +#include +#include +#include +#include "TwisterClasses.h" + +// Initialize servo and LCD +Servo myservo; +const int servoPin = 9; +LiquidCrystal lcd(12,11,5,4,3,2); + +// Initialize twister classes + +//Board 1.1 +Buzzer buzzer(6); // Board 1.1 = 6, Board 1.0 = 7 +Inputs inputs(7,0,1); // Board 1.1 = 7, Board 1.0 = 6 + +StateTracker twister; +Photocells photocells(2, 3); + +// EEPROM variables +int remainingTurns; +int turnDirection; + +void setup() +{ + + // start up servo and LCD screen + myservo.attach(servoPin); + lcd.begin(16,2); + + if (debugMode || printState) { + Serial.begin(9600); + } + + // Read from EEPROM to determine if an error occurred during the last twist: + if (saveState) { + remainingTurns = EEPROM.read(0); + turnDirection = EEPROM.read(1); + } + + //if (remainingTurns == 0 || remainingTurns == 255) { + startupScreen(ver[0],ver[1]); // Display the welcome message + + //} // NOT CURRENTLY IMPLEMENTED: + // if Arduino loses power or is reset while turning, + // the program will read stored values from EEPROM + // and keep turning where it left off. + + //else { // restart turning + // resumeDialog(); //Display the interactive dialog + //} + +} + +void loop() +{ + inputs.check(twister.isTurning, verPCB); + + if (twister.isTurning) { // only update photocells if twister is spinning + int turns = photocells.update(twister.isTurningFWD,millis()); + + if (turns) { + twister.totalTurns += turns; + twister.isUpdated = true; // necessary to update LCD + + if (printState) { + printVariables(); + } + } + } + + showTurns(); // update LCD screen + + if (inputs.buttonState == LOW) { // check for button press + twister.respondToButton(); + delay(250); + } + + updateServo(); + + if (saveState) { + updateEEPROM(); + } + + if (debugMode) { + printPhotocellInfo(); + } + +} + + + + +