Skip to content

Commit

Permalink
Update PARTS_LIST. Implement Printer functors with pass by reference …
Browse files Browse the repository at this point in the history
…instead of pass by pointer. Change order of function calls in if(wpb.pressed()) blocks. Fix bug where spinningStartTime was reinitialized with each loop() call in SPINNING mode.
  • Loading branch information
karinassuni committed Feb 16, 2016
1 parent cb52b86 commit cf34261
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 45 deletions.
13 changes: 6 additions & 7 deletions PARTS_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ Parts List

- [] Hitachi LCD
- [] LED Push Button, for changing operating modes
- [] Potentiometer, for setting time and speed
- Small potentiometer, for adjusting LCD contrast
- 2x potentiometers
- 1 for setting time and speed
- 1 for adjusting LCD contrast

- Transistor [2N6388]
- controls motor
- for controlling motor speed
- operates in "Active-Region Safe Operating Area"
- operates safely below absolute maximum ratings for continuous and peak collector current, and for collector-emitter voltage
- 220 Ohm resistor
- 330 Ohm resistor
- limits maximum current at base of transistor (current that is supplied by pin 10) to levels safe for the Arduino
- 4x 0.1 uF (bypass/filter/decoupling) capacitor, ceramic [NTE CML104M50]
- 0.1 uF (bypass/filter/decoupling) capacitor, ceramic [NTE CML104M50]
- 1 to denoise the potentiometer
- 3 to absorb noise created by the motor, so it does not interfere with the LCD
- https://www.pololu.com/docs/0J15/9
- 1000 uF capacitor [Fry's brand]
- from the motor's power supply to the emitter
- prevents noise generated by motor's inductance from getting to other parts of the circuit
Expand Down
20 changes: 8 additions & 12 deletions Printers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

struct PercentPrint {

void operator() () {}

template <typename Number, class Stream>
void operator() (const Number value, Stream* streamPtr) {
void operator() (const Number value, Stream& stream) {

streamPtr->print(value);
streamPtr->print(F("%"));
stream.print(value);
stream.print(F("%"));

}

};

struct FSecsPrint {

void operator() () {}

template <typename Number, class Stream>
void operator() (const Number seconds, Stream* streamPtr) {
void operator() (const Number seconds, Stream& stream) {

streamPtr->print(seconds/60);
streamPtr->print(F(":"));
streamPtr->print((seconds%60 < 10 ? F("0") : F("")));
streamPtr->print(seconds%60);
stream.print(seconds/60);
stream.print(F(":"));
stream.print((seconds%60 < 10 ? F("0") : F("")));
stream.print(seconds%60);

}

Expand Down
45 changes: 19 additions & 26 deletions dremelfuge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Uploaded to an Arduino UNO, with MCU ATmega328P-PU.
Arduino UNO specs: https://www.arduino.cc/en/Main/ArduinoBoardUno
ATmega328P-PU datasheet: http://www.atmel.com/Images/doc8161.pdf
Sketch uses 4,414 bytes (13%) of program storage space. Maximum is 32,256 bytes.
Global variables use 88 bytes (3%) of dynamic memory. Maximum is 2,048 bytes.
Sketch uses 4,398 bytes (13%) of program storage space. Maximum is 32,256 bytes.
Global variables use 88 bytes (4%) of dynamic memory. Maximum is 2,048 bytes.
*/

#include <LiquidCrystal.h> // library inside of Arduino.app, use <> DIRECTIVE
Expand All @@ -20,7 +20,7 @@ Global variables use 88 bytes (3%) of dynamic memory. Maximum is 2,048 bytes.

namespace {

const uint8_t MOTOR_PIN = 10; // connected to base of transistor
const uint8_t MOTOR_PIN = 10; // connected to base of transistor
const uint8_t POT_PIN = A5;

// White PushButton
Expand All @@ -30,7 +30,7 @@ namespace {
LEDButton wpb = LEDButton(WPB_INPUT_PIN, WPB_LED_PIN, WPB_DEBOUNCE_DELAY);

// LCD
LCDPrinter lcd = LCDPrinter(12, 11, 5, 4, 3, 6); // numbers of the interface pins
LCDPrinter lcd = LCDPrinter(12, 11, 5, 4, 3, 6); // numbers of the interface pins
const uint8_t LCD_COLUMNS = 20;
const uint8_t LCD_ROWS = 4;

Expand All @@ -47,17 +47,18 @@ namespace {
set, instead of taking up memory.
*/

// Default, set-up UI
const char title[] PROGMEM = " Dremel Centrifuge";
constexpr char setTime[] PROGMEM = "Set time: ";
constexpr char setSpeed[] PROGMEM = "Set speed: ";
constexpr char pushTo[] PROGMEM = " Push to ";

const char pushStart[] PROGMEM = "Start!";
const char nullValue[] PROGMEM = "---";

// UI associated with SPINNING mode
// SPINNING mode UI
constexpr char finishedIn[] PROGMEM = "Finished in: ";
const char pushStop[] PROGMEM = "Stop! ";
const char nullValue[] PROGMEM = "---";
const char pushStop[] PROGMEM = "Stop! ";

// Value decoration--very small, storing in Flash would be less efficient
// due to the extra time and extra code needed to access Flash memory
Expand Down Expand Up @@ -105,7 +106,7 @@ inline void selectPrint(const T value, Functor& printfn,
lcd.setCursor(col, row);

lcd.print( UI::selected[0] );
printfn(value, &lcd);
printfn(value, lcd);
lcd.print( UI::selected[1] );

// Clear hanging digits
Expand All @@ -119,7 +120,7 @@ inline void deselectPrint(const T value, Functor& printfn,

lcd.setCursor(col, row);

printfn(value, &lcd);
printfn(value, lcd);

// Clear hanging digits
lcd.print(F(" "));
Expand All @@ -145,8 +146,7 @@ void setup() {
using namespace UI;
lcd.changeLine_P(title, line::Title);
lcd.changeLine_P(setTime, line::Time);
lcd.changeLine_P(setSpeed, line::Speed);
lcd.print_P(nullValue);
lcd.changeLine_P(setSpeed, line::Speed); lcd.print_P(nullValue);
lcd.changeLine_P(pushStart, line::Instructions);

}
Expand All @@ -163,25 +163,22 @@ void loop() {
};

static Mode currentMode = Mode::SETTING_TIME;
static unsigned long setDuration; // in milliseconds
static unsigned long setDuration; // in milliseconds
static unsigned long spinningStartTime;
static uint8_t motorSpeed; // [0, 255] for analogWrite

switch(currentMode) {

case Mode::SETTING_TIME: {

setDuration = map(analogRead(POT_PIN), 0, 1024, 0, 900); // Max time is 15 minutes
setDuration = map(analogRead(POT_PIN), 0, 1024, 0, 900); // Max time is 15 minutes

selectPrint(setDuration, fsecs, UI::setTimeIndex, line::Time);

if(wpb.pressed()) {

deselectPrint(setDuration, fsecs, UI::setTimeIndex, line::Time);

// Convert readable secs to calculatable ms, since time-printing done
setDuration *= 1000;

setDuration *= 1000; // convert for calculations (printing done)
deselectPrint(setDuration, fsecs, UI::setTimeIndex, line::Time);
currentMode = Mode::SETTING_SPEED;

}
Expand All @@ -190,7 +187,6 @@ void loop() {

}


case Mode::SETTING_SPEED: {

motorSpeed = map(analogRead(POT_PIN), 0, 1024, 0, 255);
Expand All @@ -201,34 +197,31 @@ void loop() {
if(wpb.pressed()) {

deselectPrint(motorSpeedPercent, percent, UI::speedIndex, line::Speed);
currentMode = Mode::SPINNING;
changeUI(UI::finishedIn, UI::pushStop);
currentMode = Mode::SPINNING;
spinningStartTime = millis();

}

break;

}


case Mode::SPINNING: {

// analogWrite/PWM must be done continuously
analogWrite(MOTOR_PIN, motorSpeed);

spinningStartTime = millis();

// Store in a variable so calculation is only done once
const unsigned long timeLeft =
(setDuration - (millis() - spinningStartTime))/1000;
const unsigned long timeLeft = (setDuration - (millis() - spinningStartTime))/1000;

selectPrint(timeLeft, fsecs, UI::finishTimeIndex, line::Time);

if(wpb.pressed() || timeLeft <= 0) {

digitalWrite(MOTOR_PIN, LOW);
currentMode = Mode::SETTING_TIME;
changeUI(UI::setTime, UI::pushStart);
currentMode = Mode::SETTING_TIME;

}

Expand Down

0 comments on commit cf34261

Please sign in to comment.