Commit 7325f311 authored by Tim Soderstrom's avatar Tim Soderstrom

Added a motor & heat mode. Also bigfixes

parent 08aff03f
......@@ -36,13 +36,14 @@
#define DEVELOP_FILM 1
#define PREHEAT 2
#define RUN_MOTOR 3
#define MAX_OPTION 3
#define HEAT_AND_MOTOR 4
#define MAX_OPTION 4
// Tunable Defines
// Maximum number of dev steps a particular Dev Process can have.
#define MAX_DEV_STEPS 8
// How often to check the temperature probe
#define TEMP_UPDATE_INTERVAL 60
#define TEMP_UPDATE_INTERVAL 5
// Default Preheat Temperature (C)
#define DEFAULT_PREHEAT_TEMP 39
// Manual Temperature Adjustment Steps
......@@ -108,6 +109,7 @@ unsigned long pidWindowStartTime;
bool heaterState = OFF;
unsigned long previousMotorMillis = 0;
unsigned long previousCoastMillis = 0;
int motorSpeedCurrent = 0;
bool motorDirection = FORWARD;
// If we need to coast
bool motorCoast = true;
......@@ -216,6 +218,13 @@ void loop()
mode = 0;
break;
}
case HEAT_AND_MOTOR:
{
heatAndMotor();
mode = 0;
break;
}
// Main Menu
default:
{
......@@ -236,6 +245,11 @@ void loop()
button = wait("Select Mode", "Turn Rotary");
break;
}
case HEAT_AND_MOTOR:
{
button = wait("Select Mode", "Heat & Motor");
break;
}
default:
{
button = wait("Main Menu","");
......@@ -248,8 +262,8 @@ void loop()
case BUTTON_UP:
{
++option;
if(option > 2)
option = 0;
if(option > MAX_OPTION)
option = 1;
Serial.println(F("Button Up"));
Serial.print(F("Option: "));
Serial.println(option);
......@@ -258,7 +272,7 @@ void loop()
case BUTTON_DOWN:
{
--option;
if(option > 2)
if(option < 0 || option > MAX_OPTION)
option = MAX_OPTION;
Serial.println(F("Button Down"));
Serial.print(F("Option: "));
......@@ -279,5 +293,3 @@ void loop()
// Reset back to main menu
button = 0;
}
......@@ -153,97 +153,200 @@ void preheat()
}
}
void controlTemp()
void runMotor()
{
pid.Compute();
if (millis() - pidWindowStartTime > PID_WINDOW_SIZE_MILLIS)
pidWindowStartTime += PID_WINDOW_SIZE_MILLIS;
if (pidOutput > millis() - pidWindowStartTime)
{
if(heaterState != ON)
{
heaterState = ON;
//Serial.println("Turned Heater On");
}
digitalWrite(heater, HIGH);
}
else
byte rpm = MOTOR_RPM;
byte directionInterval = 5;
bool runMotor = true;
char buffer[32];
byte button=0;
sprintf(buffer, "RPM %02d : I %02d", rpm, directionInterval);
drawDisplay("Turning Rotary", buffer);
while(true)
{
if(heaterState != OFF)
if(runMotor)
controlMotor(rpm, directionInterval);
else
controlMotor(0, 0);
button = lcd.readButtons();
if(button)
{
heaterState = OFF;
//Serial.println("Turned Heater Off");
Serial.println("In button if");
switch(button)
{
case BUTTON_UP:
{
if(rpm < MOTOR_RPM)
++rpm;
button = 0;
break;
}
case BUTTON_DOWN:
{
if(rpm > MOTOR_MIN_RPM)
--rpm;
button = 0;
break;
}
case BUTTON_LEFT:
{
if(directionInterval > 1)
--directionInterval;
button = 0;
break;
}
case BUTTON_RIGHT:
{
if(directionInterval < 255)
++directionInterval;
button = 0;
break;
}
case BUTTON_SELECT:
{
if(runMotor)
{
drawDisplay("Turning Rotary", "Paused");
runMotor = false;
}
else
runMotor = true;
}
}
if(runMotor)
{
sprintf(buffer, "RPM %02d : I %02d", rpm, directionInterval);
drawDisplay("Running Motor", buffer);
}
}
digitalWrite(heater, LOW);
}
}
void controlMotor(byte speed, byte directionInterval)
void heatAndMotor()
{
if(directionInterval > 0)
unsigned long previousMillis = 0;
byte button = 0;
char currentTempChar[6];
char targetTempChar[6];
char topBuffer[32];
char bottomBuffer[32];
bool selectTemp = true;
bool runMotor = true;
byte rpm = MOTOR_RPM;
byte directionInterval = 5;
while(selectTemp)
{
// Coast on direction change
if(motorCoast)
controlTemp();
if(millis() - previousMillis >= SECONDS_MS * TEMP_UPDATE_INTERVAL || button)
{
analogWrite(motorSpeed, 0);
motorCoasting = true;
if (millis() - previousCoastMillis >= COAST_MOTOR_MILLIS && motorCoast)
{
//Serial.println("Done Coasting");
motorCoasting = false;
}
currentTemperature = collectTemperatures();
Serial.print("Current Temperature: ");
Serial.println(currentTemperature);
Serial.print("Target Temperature: ");
Serial.println(targetTemperature);
Serial.print("PID Output: ");
Serial.println(pidOutput);
dtostrf(currentTemperature, 5, 2, currentTempChar);
dtostrf(targetTemperature, 5, 2, targetTempChar);
sprintf(bottomBuffer, "%sC / %sC", currentTempChar, targetTempChar);
drawDisplay("Set Temp", bottomBuffer);
previousMillis = millis();
}
if(millis() - previousMotorMillis >= SECONDS_MS * directionInterval)
{
// Flag we need to coast the motor
// If we are coasting, do nothing
if(motorCoasting)
button = lcd.readButtons();
if(button & BUTTON_LEFT)
return;
if(button & BUTTON_UP)
targetTemperature += TEMP_ADJUSTMENT_PRECISION;
if(button & BUTTON_DOWN)
targetTemperature -= TEMP_ADJUSTMENT_PRECISION;
if(button & BUTTON_SELECT)
selectTemp = false;
}
// Rotary Part
while(true)
{
controlTemp();
if(runMotor)
controlMotor(rpm, directionInterval);
else
controlMotor(0, 0);
if(millis() - previousMillis >= SECONDS_MS * TEMP_UPDATE_INTERVAL || button)
{
currentTemperature = collectTemperatures();
dtostrf(currentTemperature, 5, 2, currentTempChar);
dtostrf(targetTemperature, 5, 2, targetTempChar);
sprintf(topBuffer, "%sC / %sC", currentTempChar, targetTempChar);
if(runMotor)
{
sprintf(bottomBuffer, "RPM %02d I %02d", rpm, directionInterval);
drawDisplay(topBuffer, bottomBuffer);
}
// If we are not currently coasting, but
else if(!motorCoast)
else
{
//Serial.println("Coast Requested");
previousCoastMillis = millis();
motorCoast = true;
drawDisplay(topBuffer, "Paused");
}
// Now Change Direction
else if(!motorCoasting)
previousMillis = millis();
}
button = lcd.readButtons();
if(button)
{
switch(button)
{
analogWrite(motorSpeed, toSpeed(speed));
changeDirection();
motorCoast = false;
previousMotorMillis = millis();
case BUTTON_UP:
{
if(rpm < MOTOR_RPM)
++rpm;
button = 0;
break;
}
case BUTTON_DOWN:
{
if(rpm > MOTOR_MIN_RPM)
--rpm;
button = 0;
break;
}
case BUTTON_LEFT:
{
if(directionInterval > 1)
--directionInterval;
button = 0;
break;
}
case BUTTON_RIGHT:
{
if(directionInterval < 255)
++directionInterval;
button = 0;
break;
}
case BUTTON_SELECT:
{
if(runMotor)
{
Serial.println("Pausing Motor");
drawDisplay(topBuffer, "Paused");
previousMotorMillis = 0;
previousCoastMillis = 0;
runMotor = false;
}
else
{
Serial.println("Running Motor");
previousMotorMillis = 0;
previousCoastMillis = 0;
runMotor = true;
}
break;
}
}
controlMotor(rpm, directionInterval);
sprintf(topBuffer, "%sC / %sC", currentTempChar, targetTempChar);
sprintf(bottomBuffer, "RPM %02d I %02d", rpm, directionInterval);
drawDisplay(topBuffer, bottomBuffer);
}
}
/* If directionInterval is zero, never change speed */
else
{
analogWrite(motorSpeed, toSpeed(speed));
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
motorDirection = FORWARD;
}
}
void changeDirection()
{
if(motorDirection == FORWARD)
{
//Serial.println("Motor: Reverse");
digitalWrite(motorX, LOW);
digitalWrite(motorY, HIGH);
motorDirection = REVERSE;
}
else
{
//Serial.println("Motor: Forward");
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
motorDirection = FORWARD;
}
}
......@@ -6,8 +6,6 @@ void updateDisplay(int totalSeconds, int temperature)
lcd.print(buffer);
}
byte wait(char line1[16], char line2[16])
{
byte button=0;
......@@ -40,76 +38,105 @@ double collectTemperatures()
return -255;
}
void runMotor()
void controlTemp()
{
byte rpm = MOTOR_RPM;
byte directionInterval = 5;
bool runMotor = true;
char buffer[32];
byte button=0;
pid.Compute();
if (millis() - pidWindowStartTime > PID_WINDOW_SIZE_MILLIS)
pidWindowStartTime += PID_WINDOW_SIZE_MILLIS;
if (pidOutput > millis() - pidWindowStartTime)
{
if(heaterState != ON)
{
heaterState = ON;
//Serial.println("Turned Heater On");
}
digitalWrite(heater, HIGH);
}
else
{
if(heaterState != OFF)
{
heaterState = OFF;
//Serial.println("Turned Heater Off");
}
digitalWrite(heater, LOW);
}
}
sprintf(buffer, "RPM %02d : I %02d", rpm, directionInterval);
drawDisplay("Turning Rotary", buffer);
while(true)
void controlMotor(byte speed, byte directionInterval)
{
if(directionInterval > 0)
{
if(runMotor)
controlMotor(rpm, directionInterval);
else
controlMotor(0, 0);
button = lcd.readButtons();
if(button)
// Coast on direction change
if(motorCoast)
{
Serial.println("In button if");
switch(button)
analogWrite(motorSpeed, 0);
motorCoasting = true;
if (millis() - previousCoastMillis >= COAST_MOTOR_MILLIS && motorCoast)
{
case BUTTON_UP:
{
if(rpm < MOTOR_RPM)
++rpm;
button = 0;
break;
}
case BUTTON_DOWN:
{
if(rpm > MOTOR_MIN_RPM)
--rpm;
button = 0;
break;
}
case BUTTON_LEFT:
{
if(directionInterval > 1)
--directionInterval;
button = 0;
break;
}
case BUTTON_RIGHT:
{
if(directionInterval < 255)
++directionInterval;
button = 0;
break;
}
case BUTTON_SELECT:
{
if(runMotor)
{
drawDisplay("Turning Rotary", "Paused");
runMotor = false;
}
else
runMotor = true;
}
Serial.println("Done Coasting");
motorCoasting = false;
}
if(runMotor)
}
// If we're not coasting, make sure we set the speed to whatever is being requested
else if (!motorCoast)
analogWrite(motorSpeed, toSpeed(speed));
if(millis() - previousMotorMillis >= SECONDS_MS * directionInterval)
{
// Flag we need to coast the motor
// If we are coasting, do nothing
if(motorCoasting)
{
}
// If we are not currently coasting, but
else if(!motorCoast)
{
Serial.println("Coast Requested");
previousCoastMillis = millis();
motorCoast = true;
}
// Now Change Direction
else if(!motorCoasting)
{
sprintf(buffer, "RPM %02d : I %02d", rpm, directionInterval);
drawDisplay("Running Motor", buffer);
Serial.println("Motor Running");
analogWrite(motorSpeed, toSpeed(speed));
changeDirection();
motorCoast = false;
previousMotorMillis = millis();
}
}
}
/* If directionInterval is zero, never change speed */
else
{
analogWrite(motorSpeed, toSpeed(speed));
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
motorDirection = FORWARD;
}
}
void changeDirection()
{
if(motorDirection == FORWARD)
{
Serial.println("Motor: Reverse");
digitalWrite(motorX, LOW);
digitalWrite(motorY, HIGH);
motorDirection = REVERSE;
}
else
{
Serial.println("Motor: Forward");
digitalWrite(motorX, HIGH);
digitalWrite(motorY, LOW);
motorDirection = FORWARD;
}
}
int toRPM(byte value)
{
return map(value, 0, 255, 0, MOTOR_RPM);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment