Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Manual mode including manual grinding time #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ void Display::printDoubleDose() {
display->display();
}

void Display::printManualMode(double time){
display->clearDisplay();
display->setTextColor(WHITE);
display->setTextSize(2);
display->setCursor(30,10);
display->print("Manual");
display->println();
time = time < 0 ? 0.0 : time;
time = time < 100 ? time : 99.9;
display->setCursor(34,34);
display->print(time, time < 10 ? 2 : 1);

display->setTextSize(2);
display->setCursor(90, 34);
display->print("s");

display->println();
display->display();
}

void Display::printTime(double time) {
display->clearDisplay();

Expand Down Expand Up @@ -88,4 +108,4 @@ void Display::printStatistics(int numberSingles, int numberDoubles) {

display->println();
display->display();
}
}
2 changes: 1 addition & 1 deletion src/Display.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

class Adafruit_SSD1306;

class Display
Expand All @@ -11,6 +10,7 @@ class Display
Display();
void printSingleDose();
void printDoubleDose();
void printManualMode(double time);
void printTime(double time);
void printStatistics(int numberSingles, int numberDoubles);
};
1 change: 1 addition & 0 deletions src/Dosage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Dosage
public:
Dosage();
bool singleDoseSelected = false;
bool manualModeSelected = false;
double singleDoseTime = SINGLEDOSE_DEFAULT_TIME;
double doubleDoseTime = DOUBLEDOSE_DEFAULT_TIME;
void writeToEEPROM();
Expand Down
4 changes: 2 additions & 2 deletions src/RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ RotaryEncoder::RotaryEncoder() {
}

bool RotaryEncoder::wasTurnedLeft() {
if (encoder->read() < 0 - ENC_TOL) {
if (encoder->read() > 0 + ENC_TOL) {
encoder->write(0);
return true;
}
return false;
}

bool RotaryEncoder::wasTurnedRight() {
if (encoder->read() > 0 + ENC_TOL) {
if (encoder->read() < 0 - ENC_TOL) {
encoder->write(0);
return true;
}
Expand Down
75 changes: 57 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ Grinder *grinder;

// State Machine
enum States {NORMAL, SET_DOSE, GRINDING, STATS};
// Modes
enum Modes {SINGLE, DOUBLE, MANUAL};

uint8_t state = NORMAL;
uint8_t mode = DOUBLE;
double manualGrindTime =0.0;


void setup() {
Serial.begin(9600);
Expand All @@ -30,6 +36,7 @@ void loop() {
switch(state)
{
case NORMAL:
grinder->off();
// set new dose
if (encoder->wasPressed()) {
state = SET_DOSE;
Expand All @@ -47,39 +54,71 @@ void loop() {
state = STATS;
break;
}

// select dosage
if (encoder->wasTurnedLeft()) {
dosage->singleDoseSelected = true;
} else if (encoder->wasTurnedRight()) {
dosage->singleDoseSelected = false;

switch(mode){
case(SINGLE):
if(encoder->wasTurnedRight())mode=DOUBLE;
break;
case(DOUBLE):
if(encoder->wasTurnedLeft())mode=SINGLE;
if(encoder->wasTurnedRight())mode=MANUAL;
break;
case(MANUAL):
if(encoder->wasTurnedLeft())mode=DOUBLE;
break;
}


// display dosage
if (dosage->singleDoseSelected) {
if (mode==SINGLE) {
dosage->singleDoseSelected=true;
dosage->manualModeSelected=false;
manualGrindTime=0.0;
display->printSingleDose();
} else {
display->printDoubleDose();
} else if(mode==DOUBLE) {
dosage->singleDoseSelected=false;
dosage->manualModeSelected=false;
manualGrindTime=0.0;
display->printDoubleDose() ;
} else if(mode==MANUAL) {
dosage->singleDoseSelected=false;
dosage->manualModeSelected=true;
display->printManualMode(manualGrindTime*1.0);
}


break;

case SET_DOSE:
if (encoder->wasTurnedLeft()) {
dosage->singleDoseTime -= dosage->singleDoseSelected ? 0.1 : 0.0;
dosage->doubleDoseTime -= dosage->singleDoseSelected == false ? 0.1 : 0.0;
} else if (encoder->wasTurnedRight()) {
dosage->singleDoseTime += dosage->singleDoseSelected ? 0.1 : 0.0;
dosage->doubleDoseTime += dosage->singleDoseSelected == false ? 0.1 : 0.0;
if(dosage->manualModeSelected==false){
if (encoder->wasTurnedLeft()) {
dosage->singleDoseTime -= dosage->singleDoseSelected ? 0.1 : 0.0;
dosage->doubleDoseTime -= dosage->singleDoseSelected==false ? 0.1 : 0.0;
} else if (encoder->wasTurnedRight()) {
dosage->singleDoseTime += dosage->singleDoseSelected ? 0.1 : 0.0;
dosage->doubleDoseTime += dosage->singleDoseSelected==false ? 0.1 : 0.0;
}
dosage->writeToEEPROM();
display->printTime(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime);
}

dosage->writeToEEPROM();
display->printTime(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime);

state = NORMAL;
break;

case GRINDING:
if(dosage->manualModeSelected){
int startTime = millis()/1000.0;
while(grinder->startBtnPressed()){
grinder->on(1);
display->printTime(millis()/1000.0-startTime);

}
manualGrindTime+=millis()/1000.0-startTime;
grinder->off();
state = NORMAL;
break;
}else

grinder->increaseShotCounter(dosage->singleDoseSelected);

grinder->on(dosage->singleDoseSelected ? dosage->singleDoseTime : dosage->doubleDoseTime);
Expand Down