public
Created

  • Download Gist
Arduino Slider Control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/*
 
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.