Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/*
This version works, but needs to be cleaned up.
The LCD Screen, Keyboard and Stepper motor are all driven off
the same thread. So when driving the motor the keyboard can be
bit sluggish.
You may need to adjust the keyboard debounce time.
*/
// MAP out all the Inputs and Outputs
//
// Arduino Duemilenove
// 14 digital IO (6PWM outputs)
// 6 Analog Inputs
// 32k Flash Memory
// 2k SRAM
// 1k EEPROM
// 16Mhz Clock Speed
//
/*
** IO for the LCD Display
*/
#define IOA_KEY_INPUT 0 // 5 Key input on Analogue input 0
/* IO_LCD_DB0 4 // LCD nibble output
IO_LCD_DB1 5
IO_LCD_DB2 6
IO_LCD_DB3 7
IO_LCD_RS 8
IO_LCD_ENABLE 9 */
#define IO_LCD_BL 10 // LCD Backlight Control
#define IO_SHOT 13 // Output to take shot on camera
#define IO_LIMIT_SWITCHES A1 // Input limit switches on AIO via resistor totom pole
#define IO_STEP_DIR 3 // Direction of Stepper
#define IO_STEP_STEP 2 // Step the Stepper Motor
#define IO_STEP_ENABLE 12 // enable the Stepper Motor
/* DEFINE the keypad input keys */
#define NUM_KEYS 5
#define KEY_NULL -1
#define KEY_RIGHT 0
#define KEY_UP 1
#define KEY_DOWN 2
#define KEY_LEFT 3
#define KEY_SELECT 4
#define KEY_DEBOUNC_MS 200
#define KEY_REPEAT_MS 200
#define LCD_SCREENSAVE_TIME 10000 // time to fade LCD with no input
#define STEPS_PER_MM 1260 // number of steps per mm movement
#define STROKE_MM 650 // full stroke of slider in mm
#define NUM_MENU 12 // Number of menu items
#define MENU_HOME 0 // Home menu position
#define MENU_INTERVAL 1 // Time interval between shots (this includes exposure
#define MENU_EXPOSURE 2 // Exposure time (must be less than interval)
#define MENU_TIME 3 // Time to travel full stroke
#define MENU_SHOTS 4 // Total number of shots
#define MENU_STROKE 5 // Total stroke of slider
#define MENU_POSITION 6 // Position on slider
#define MENU_DIRECTION 7 // Direction of travel
#define MENU_DOLLY 8 // dolly vs Step mode
#define MENU_POWER 9 // Power save mode
#define MENU_LCD 10 // Brightness of LCD screen
#define MENU_MANUAL 11 // Drive in Manual Mode
#define LCD_CHAR_WIDTH 16 // Width of LCD display
/********** Header Files ***********************/
#include <LiquidCrystal.h>
#include <stdio.h>
/********** GLOBAL Variables **********************/
boolean bDebug = 0;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Create object to control the LCD
int nMenu = 0;
char sMenu[NUM_MENU][LCD_CHAR_WIDTH + 1] = {
"Home",
"Interval",
"Exposure",
"Time (min)",
"Shots",
"Stroke (mm)",
"Position (mm)",
"Direction",
"Dolly-Step Mode",
"Power Save",
"LCD Brightness",
"Manual"};
boolean bIsPowerSave = true; // disable the motor when stopped to save power.
boolean bStepDirUP = true; // Stepper direction (UP/DOWN)
boolean bShutterOpen = false; // Camera taking shot
boolean bIsRunning = false; // is running
boolean bModeDolly = false; // in dolly mode vs step mode
int nSpeedMinutes = 60; // Speed, Minutes for stroke
long nCycleTimeMS = 50; // scan cycle time ms
long PreviousShotMS = 0;
int nShotInterval = 30; // Camera Shutter Interval
int nShotExposure = 1; // Camera Shutter Exposure
int nShotNumber = 0; // Current Shot number
int nNextShotNumber = 0; // Waiting for this shot number
int nMaxShots = 120; // Number of shots to take
long nStroke = STROKE_MM; // Full Stroke of the slide
long nPositionMM = 0; // position of the slider MM
long nPositionStep = 0; // position of the slider in steps
int nLCDBrightness = 255; // the brighness of the LCD screen
// minimum uSeconds between motor steps
unsigned int motor_min_step = 200; // fastest stepping speed
unsigned int motor_max_step = 3000;
/*
** FUNCTION: setup
**
** DESCRIPTION: Setup the hardware and software constructions. This
** function is only called once on powerup or reset.
**
*/
void
setup(void)
{
if (bDebug)
{
Serial.begin(9600);
}
// Initialise the Liquid Crystal Display
analogWrite(IO_LCD_BL, nLCDBrightness);
lcd.begin(16, 2);
lcd.print("Light Slider");
lcd.setCursor(0, 1);
lcd.print("Version 0.5a");
delay(1000); // pause to read version
// Initialise the Stepper Motor
pinMode(IO_STEP_DIR, OUTPUT);
pinMode(IO_STEP_STEP, OUTPUT);
pinMode(IO_STEP_ENABLE, OUTPUT);
// Initialise the Camera Remote
pinMode(IO_SHOT, OUTPUT);
// PATCH could use MsTimer2 for parallel threads and interrupts
}
/*
** FUNCTION: loop
**
** DESCRIPTION: Called in a loop to process the program.
**
**
*/
void
loop(void)
{
UserInterface();
UpdateDisplay();
CheckLimits();
if (bIsRunning)
{
DriveStepper();
FireCamera();
}
}
char*
GetDisplayName(int menu)
{
static char sBuf[LCD_CHAR_WIDTH + 1];
if (menu == MENU_HOME)
{
sprintf(sBuf, "%3s %3ds %3d/%3d", (bIsRunning) ? "ON": "OFF", nShotInterval, nShotNumber, nMaxShots);
}
else if (menu == MENU_TIME)
{
sprintf(sBuf, "Time%3ds %3d/%3d", nShotInterval, nShotNumber, nMaxShots);
}
else
{
strcpy(sBuf, sMenu[nMenu]);
}
StrPadLCD(sBuf);
return sBuf;
}
char*
GetDisplayProperty(int menu)
{
static char sBuf[LCD_CHAR_WIDTH + 1];
switch (menu)
{
case MENU_HOME:
case MENU_TIME:
sprintf(sBuf, "%s %3dmin %3dmm", (bStepDirUP) ? "U": "D", nSpeedMinutes, nPositionMM);
break;
case MENU_INTERVAL:
sprintf(sBuf, "%3d sec", nShotInterval);
break;
case MENU_EXPOSURE:
sprintf(sBuf, "%3d sec", nShotExposure);
break;
case MENU_SHOTS:
sprintf(sBuf, "%3d", nMaxShots);
break;
case MENU_POSITION:
sprintf(sBuf, "%3d", nPositionMM);
break;
case MENU_STROKE:
sprintf(sBuf, "%3d", nStroke);
break;
case MENU_DIRECTION:
strcpy(sBuf, (bStepDirUP) ? "UP": "DOWN");
break;
case MENU_DOLLY:
strcpy(sBuf, (bModeDolly) ? "DOLLY": "STEP");
break;
case MENU_POWER:
strcpy(sBuf, (bIsPowerSave) ? "SAVE": "OFF");
break;
case MENU_LCD:
sprintf(sBuf, "%3d", nLCDBrightness);
break;
case MENU_MANUAL:
sprintf(sBuf, "%3d", nPositionMM);
break;
default:
strcpy(sBuf, "INVALID DISPLAY");
break;
}
StrPadLCD(sBuf);
return sBuf;
}
char*
StrPadLCD(char* sBuf)
{
int i;
strlen(sBuf);
for (i = strlen(sBuf); i < LCD_CHAR_WIDTH; i++)
{
sBuf[i] = ' ';
}
sBuf[LCD_CHAR_WIDTH] = '\0';
return sBuf;
}
void
UpdateDisplay(void)
{
static char sLine1[LCD_CHAR_WIDTH + 1];
static char sLine2[LCD_CHAR_WIDTH + 1];
char* pLine1;
char* pLine2;
pLine1 = GetDisplayName(nMenu);
if (strcmp(pLine1, sLine1))
{
lcd.setCursor(0, 0);
lcd.print(pLine1);
strcpy(sLine1, pLine1);
}
pLine2 = GetDisplayProperty(nMenu);
if (strcmp(pLine2, sLine2))
{
lcd.setCursor(0, 1);
lcd.print(pLine2);
strcpy(sLine2, pLine2);
}
}
void
CmdProcess(int display, int cmd)
{
switch (display)
{
case MENU_HOME:
if (cmd == KEY_SELECT)
{
StopRunning();
}
else if (bIsRunning)
{
StopRunning();
}
else
{
StartRunning();
}
break;
case MENU_INTERVAL:
nShotInterval = CmdAdjust(nShotInterval, cmd, 1, 3600, 1, 1);
nMaxShots = ((long)nSpeedMinutes * 60) / nShotInterval;
if (nMaxShots > 999)
{
nMaxShots = 999;
}
break;
case MENU_EXPOSURE:
nShotExposure = CmdAdjust(nShotExposure, cmd, 1, 60, 1, 1);
break;
case MENU_TIME:
nSpeedMinutes = CmdAdjust(nSpeedMinutes, cmd, 3, 999, 10, nSpeedMinutes / 10 + 1);
nMaxShots = ((long)nSpeedMinutes * 60) / nShotInterval;
if (nMaxShots > 999)
{
nMaxShots = 999;
}
break;
case MENU_SHOTS:
nMaxShots = CmdAdjust(nMaxShots, cmd, 1, 999, 200, nMaxShots / 10 + 1);
nSpeedMinutes = (nMaxShots * nShotInterval) / 60;
break;
case MENU_STROKE:
nStroke = CmdAdjust(nStroke, cmd, 1, STROKE_MM, STROKE_MM, nStroke / 10 + 1);
break;
case MENU_POSITION:
nPositionMM = CmdAdjust(nPositionMM, cmd, 0, STROKE_MM, 0, nPositionMM / 10 + 1);
break;
case MENU_DIRECTION:
bStepDirUP = !bStepDirUP;
break;
case MENU_DOLLY:
bModeDolly = !bModeDolly;
break;
case MENU_POWER:
bIsPowerSave = !bIsPowerSave;
SetPowerSave();
break;
case MENU_LCD:
nLCDBrightness = CmdAdjust(nLCDBrightness, cmd, 20, 255, 50, 5);
analogWrite(IO_LCD_BL, nLCDBrightness);
break;
case MENU_MANUAL:
if (cmd == KEY_SELECT)
{
}
else
{
motor_pulse_steps(1600, 200, (cmd == KEY_LEFT));
}
break;
default:
DebugTrace("ERROR INVALID DISPLAY\n");
break;
}
}
int
CmdAdjust(int value, int cmd, int min, int max, int def, int inc)
{
switch (cmd)
{
case KEY_RIGHT:
value += inc;
if (value > max)
{
value = max;
}
break;
case KEY_LEFT:
value -= inc;
if (value < min)
{
value = min;
}
break;
case KEY_SELECT:
value = def;
break;
}
return value;
}
void
UserInterface(void)
{
int cmd;
cmd = GetCmd();
switch (cmd)
{
case KEY_UP:
if (--nMenu < 0)
{
nMenu = NUM_MENU - 1;
}
break;
case KEY_DOWN:
if (++nMenu >= NUM_MENU)
{
nMenu = 0;
}
break;
case KEY_RIGHT:
case KEY_LEFT:
case KEY_SELECT:
CmdProcess(nMenu, cmd);
break;
case KEY_NULL:
break;
default:
DebugTrace("ERROR INVALID CMD\n");
break;
}
}
int
GetCmd(void)
{
static unsigned long lastDebounceTime;
static unsigned long valid_key_time;
static int lastread = KEY_NULL;
int read;
int keyState;
keyState = KEY_NULL;
read = get_key();
if (read != lastread)
{
lastDebounceTime = millis();
}
lastread = read;
if (millis() - lastDebounceTime > KEY_DEBOUNC_MS)
{
keyState = read;
lastread = KEY_NULL;
}
if (keyState != KEY_NULL)
{
valid_key_time = millis();
}
// dim the screen if no input
if (millis() - valid_key_time > LCD_SCREENSAVE_TIME)
{
analogWrite(IO_LCD_BL, 20);
}
else
{
analogWrite(IO_LCD_BL, nLCDBrightness);
}
return keyState;
}
int
get_key(void)
{
static int adc_key_val[NUM_KEYS] = {30, 150, 360, 535, 760};
int k;
int input;
input = analogRead(IOA_KEY_INPUT);
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
return KEY_NULL;
}
void
StartRunning(void)
{
bIsRunning = true;
nShotNumber = 0;
nNextShotNumber = 0;
nPositionStep = (long) nPositionMM * STEPS_PER_MM;
PreviousShotMS = millis();
}
void
StopRunning(void)
{
bIsRunning = false;
}
void
DriveStepper(void)
{
if (bModeDolly)
{
motor_pulse_steps_dolly();
}
else
{
if (nShotNumber == nNextShotNumber)
{
long nSteps;
nSteps = (long) nStroke * STEPS_PER_MM / nMaxShots;
motor_pulse_steps_linear(nSteps);
nNextShotNumber++;
}
}
}
void
motor_pulse_steps_dolly(void) // PATCH this should be via interrupt to keep going
{
long nStep;
long nDelay;
long nStepPerSec;
nStepPerSec = (long) nStroke * STEPS_PER_MM / 60 / nSpeedMinutes;
nStep = nStepPerSec / ( 1000 / nCycleTimeMS);
nDelay = 1000 / nStep * nCycleTimeMS;
motor_pulse_steps(nStep, nDelay, bStepDirUP);
}
void
motor_pulse_steps(long nStep, long nDelay, boolean bDir)
{
int i;
digitalWrite(IO_STEP_ENABLE, LOW); // Enable the stepper motor
digitalWrite(IO_STEP_DIR, (bDir) ? HIGH: LOW); // set direction
for (i = 0; i < nStep; i++)
{
digitalWrite(IO_STEP_STEP, HIGH);
delayMicroseconds(1);
digitalWrite(IO_STEP_STEP, LOW);
delayMicroseconds(nDelay);
}
nPositionStep += (bDir) ? + nStep: - nStep;
nPositionMM = nPositionStep / STEPS_PER_MM;
SetPowerSave();
}
void
motor_pulse_steps_linear(unsigned long steps)
{
// moving individual motors with a linear speed ramp
// this speed ramp is used only to optimize torque,
// minimize motor noise, missed steps, and prevent
// excessive motion causes by a motor started and
// stopped at full-speed.
if (steps == 0)
return;
digitalWrite(IO_STEP_ENABLE, LOW); // Enable the stepper motor
digitalWrite(IO_STEP_DIR, (bStepDirUP) ? HIGH: LOW); // set direction
// shelf where motor speed ramp starts and stops
unsigned long shelf_lo = steps / 3;
unsigned long shelf_hi = shelf_lo * 2;
// we can't have our ramp rate be zero, which it would be
// if shelf_lo were >= the difference between min and max
// ramp rate. This happens when large moves (thousand+ steps)
// try to use 1/3rd shelf up and down. So, reduce shelf
// by ten percent until we get a reasonable range...
while( shelf_lo >= ( motor_max_step - motor_min_step ) ) {
shelf_lo = shelf_lo - ( (unsigned long) ( (float) shelf_lo * (float) 0.1 ) );
shelf_hi = steps - shelf_lo;
}
unsigned int delay_increment = ( motor_max_step - motor_min_step ) / shelf_lo;
unsigned int delay = 0;
for( long i = 0; i < steps ; i++ ) {
digitalWrite(IO_STEP_STEP, HIGH);
if( i < shelf_lo ) {
// we're ramping up to speed
delay = ( delay_increment * ( shelf_lo - i ) ) + motor_min_step;
}
else if( i > shelf_hi ) {
// we're ramping down from speed
delay = ( delay_increment * ( i - shelf_hi ) ) + motor_min_step;
}
else {
// wer're moving at maximum speed
delay = motor_min_step;
}
digitalWrite(IO_STEP_STEP, LOW);
delayMicroseconds(delay);
} // end for loop
nPositionStep += (bStepDirUP) ? + steps: - steps;
nPositionMM = nPositionStep / STEPS_PER_MM;
SetPowerSave();
}
void
SetPowerSave(void)
{
digitalWrite(IO_STEP_ENABLE, (bIsPowerSave) ? HIGH: LOW); // disable the power to stepper motor to save power.
}
void
FireCamera(void)
{
unsigned long ms = millis();
if (ms - PreviousShotMS > nShotInterval * 1000)
{
PreviousShotMS = ms;
bShutterOpen = true;
digitalWrite(IO_SHOT, HIGH); // Fire the Shutter
}
else if (bShutterOpen)
{
if (ms - PreviousShotMS > nShotExposure * 1000)
{
digitalWrite(IO_SHOT, LOW); // Release the Shutter
bShutterOpen = false;
if (++nShotNumber > nMaxShots)
{
DebugTrace("Maximum Shots\n");
StopRunning();
}
}
}
}
void
CheckLimits(void)
{
int limit;
limit = analogRead(IO_LIMIT_SWITCHES);
if (480 < limit && limit < 540) // SHOULD BE 512
{
DebugTrace("Limit RIGHT\n");
StopRunning();
// PATCH change this to only stop if going this direction or cannot start on a limit.
}
else if (limit < 50) // SHOULD BE 0
{
DebugTrace("Limit LEFT\n");
StopRunning();
}
else // SHOULD BE 682
{
}
}
void
DebugTrace(char* sMsg)
{
if (bDebug)
{
Serial.print(sMsg);
}
}
@mchazlitt

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.