Code for the Enlargertimer

    October 12th, 2014

    I finally had some time to clean up the code for this project a little. Here it is…

    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
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    
    #include <EEPROM.h>
    #include <Bounce.h>
    #include <MemoryFree.h>
    #include <LiquidCrystal.h>  // Note the calls to LCD brightness, which is custom to the VFD i've used.  Remove for normal LiquidCrystal library.
    #include <avr/wdt.h>
     
    #define VERSION 2
     
    /*
    Enlargertimer -
     
     An F-stop-based timer for a photographic enlarger.
     
     It presents a menu-style interface on a 2-line VFD covered in red lithographic tape to be dark-room safe.
     
     Primary input is via a single rotary encoder with push-to-click.
     Secondary input is via a foot peddle.  The hardware is wired for this, but the peddle hasn't been installed yet.  The software does not support it yet either.
     Primary output is to a 5v-drive Triac which handles the line-voltage enlarger light bulb.  It is driven by the AVR outputs directly.
     Audio feedback is via a piezo buzzer driven straight from the AVR outputs.  In the quiet darkroom environment this is loud enough.
     
     The menu system uses a set of arrays to define the menus and function pointers to define actions to be taken upon clicking.
     The display is also defined by fuction pointers to mode-specific display functions.
     
     Program flow is a single thread apart from the encoder rotation inputs, which are handled by ISRs.  The click input is handled in the normal program flow, not an ISR. 
    */
     
    #define TEST_STRIP 1
    #define PRINT 0
     
    #define RIGHT 0
    #define LEFT 1
     
    #define SET_BASE 1
    #define SET_STEPS 2
    #define SET_STEPSIZE 3
    #define SET_VFD_BRIGHT 4
    #define SET_STOPS 5
     
    int vfd_brightness = 0x03;
     
    enum PinAssignments {
      encoderPinA = 3,    // spin
      encoderPinB = 2,    // spin
      goButtonPin = 10,   // poke
      lightPin = 11,      // blink
      footButtonPin = 12, // stomp - hardware supports this, but the peddle is not yet installed.
      buzz = 13           // beep
    };
     
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
     
    Bounce goButton = Bounce(goButtonPin, 100);
     
    byte mode = 0;
    byte menupos = 0;
     
    // Good old twirly cursor.
    char twirlychar[] = {
      '/','-','\\','|'};
    byte twirly = 0;
    unsigned long twirlycount = 0;
     
    // menu text and size.  first element is the number of elements in each menu.
    // best make sure that the lengths of the other arrays match this one - 1...
    const char *menu0[] = {
      (char*)4, "Focus", "Test Strip", "Print", "Config"};
    const char *menu1[] = {
      (char*)1, "Stop Focusing"};
    const char *menu2[] = {
      (char*)6, "Exec", "Base", "Steps", "Stride", "", "Exit"};
    const char *menu3[] = {
      (char*)4, "Exec", "Base", "Stops", "Exit"};
    const char *menu4[] = {
      (char*)3, "Display", "Reset", "Exit"};
     
    const char **menu[] = {
      menu0, menu1, menu2, menu3, menu4};
     
    // Each menu entry can optionally jump to a new menu.
    // menutarget encodes which menu to jump to upon clicking.
    // 255 menus 'don't jump', instead only execute some other function.
    byte menutarget0[] = {
      1,2,3,4};        // main
    byte menutarget1[] = {
      0};              // focus
    byte menutarget2[] = {
      255,255,255,255,255,0};  // test strip
    byte menutarget3[] = {
      255,255,255,0};      // print
    byte menutarget4[] = {
      255,255,0};      // config
     
    byte *menutarget[] = {
      menutarget0,menutarget1,menutarget2,menutarget3,menutarget4};
     
    // Each menu entry can optionally execute an arbitrary function.
    // 0 means don't execute anything (usually there will be a menu jump instead)
    void (*menuaction0[])(void) = {
      0,print_steps,0,0};         // main
    void (*menuaction1[])(void) = {
      0,0,0};                    // focus
    void (*menuaction2[])(void) = {
      exec_strip,set_base,set_steps,set_stepsize,set_mode,0}; // test strip
    void (*menuaction3[])(void) = {
      exec_print,set_base,set_stops,0};                    // print
    void (*menuaction4[])(void) = {
      set_vfd_bright,reset,0};                           // config
     
    void (**menuaction[])(void) = {
      menuaction0, menuaction1, menuaction2, menuaction3, menuaction4};
     
    // The VFD has two lines, they are handled separately.
     
    // function to call to render the first line for each menu.  defaults to def_lineone();
    // Everything except test strip mode uses a default-format first line.
    void (*menulineone[])(void) = {
      0, // main
      0, // focus
      strip_lineone, // test strip
      0, // print
      0  // config
    };
     
    // function to call to render the second line for each menu.  defaults to def_linetwo();
    void (*menulinetwo[])(byte mode) = {
      0, // main
      start_focus, // focus
      strip_linetwo, // test strip
      print_linetwo, // print
      config_linetwo  // config
    };
     
    volatile unsigned int encoderPos = 0;  // a counter for the dial
    unsigned int lastReportedPos = 100000;   // change management
    static boolean rotating=false;      // debounce management
     
    // interrupt service routine states
    boolean A_set = false;
    boolean B_set = false;
     
    byte localized = 0;     // localized or not test strip
    int base_exp = 4;       // print base exposure in seconds
    int steps = 7;          // number of steps in a test strip
    float stepsize = 0.25f; // Size of the steps in stops
    float stops = 0.0f;     // Number of stops
    float exposure;         // cumulative exposure in seconds
     
    void setup() {
      Serial.begin(115200);  
      Serial.println(F("hello"));
      Serial.println(freeMemory());
     
      setup_lcd();
     
      pinMode(encoderPinA, INPUT_PULLUP); 
      pinMode(encoderPinB, INPUT_PULLUP); 
      pinMode(goButtonPin, INPUT_PULLUP);
      pinMode(lightPin, OUTPUT);
     
      attachInterrupt(0, doEncoderA, CHANGE);
      attachInterrupt(1, doEncoderB, CHANGE);
    }
     
    void loop() {
      goButton.update();
      byte goButtonState = goButton.read();
     
      rotating = true;  // reset the debouncer
     
      if(goButtonState == LOW || lastReportedPos != encoderPos) {
        // The motorized elevation control in the enlarger produces enough EFI to piss off the LCD controller and scramble the VFD.  
        // reset the lcd controller whenever we click or move the encoder to deal with with this (rather than figure out how to filter out the noise).
        lcd.begin(40, 2);
     
        if (lastReportedPos != encoderPos) {  // Encoder was rotated
          tone(buzz, 2000, 1);  // little click
          Serial.print("Index:");
          Serial.println(encoderPos, DEC);
     
          if(lastReportedPos > encoderPos) {
            if(menupos < (int)menu[mode][0] - 1) 
              menupos++;  
          } 
          else if(lastReportedPos < encoderPos) {
            if(menupos - 1 >= 0)
              menupos--;
          }
     
          Serial.print("menupos: ");
          Serial.println(menupos);
     
          lastReportedPos = encoderPos;
        }
     
        if(goButtonState == LOW) {  // Button is down.
          int clicktime = await_unclick();  // how long was it held down?
     
          if(clicktime > 5000) {  // 5 second long-click = reboot!
            tone(buzz, 1000, 1000);
            delay(1000);
            reset();
          } 
          else if(clicktime > 2000) {  // 2 second long click = Shortcut to set a 'normal' print exposure.
            if(mode == 3) {  // if printing...
              tone(buzz, 2000, 100);
              base_exp = 4;
              stops = 4;
            } 
            else {  // Or a mad low-beep to show nothing was done.
              tone(buzz, 250, 100); 
            }
          } 
          else if(clicktime > 1000) {  // 1 second long click = Shortcut to go to top level menu.
            tone(buzz, 1000, 100);
            mode = menupos = 0;
          } 
          else {  // normal click
            tone(buzz, 1000, 10);
            Serial.println("go");
     
            if(menuaction[mode][menupos]) {  // if the menu item clicked on has an action, execute it.
              (*menuaction[mode][menupos])();
            }
     
            if(menutarget[mode][menupos] != 255) {  // if the menu item clicked on has a menu target, switch to it.
              Serial.print(F("Target: "));
              Serial.println(menutarget[mode][menupos]);
              mode = menutarget[mode][menupos];
              menupos = 0;
            }  
            else {
              Serial.println("notarget");  
            }
          }
        }
     
        // update the display.
        lcd.clear();
     
        // display line 1
        if(menulineone[mode]) {
          (*menulineone[mode])();
        } 
        else {
          def_lineone(); 
        }
     
        Serial.println();
        delay(500);
      }
     
      // display line 2 - always display line two since it may have status updates to show even when there is no control interaction.
      if(menulinetwo[mode]) {
        (*menulinetwo[mode])(0);
      } 
      else {
        def_linetwo(0); 
      }
     
    }
     
    // Default first line display function
    // Display the menu.
    void def_lineone(const char *one) {
      for(int i = 1; i < (int)menu[mode][0] + 1; i++) {
        if(menupos + 1 == i) {  // arrows around the current menu item
          lcd.write((uint8_t)RIGHT);
          Serial.print('>');
        } 
        else {
          lcd.print(' ');
          Serial.print(' ');
        }
     
        if(*menu[mode][i] == 0) {  // If the menu entry is null at this position, print the argument instead.
          lcd.print(one);          // This is used by the test strip mode to display the Localized or Continuous setting.  It is a bit funky.
          Serial.print(one);      
        } 
        else {
          lcd.print(menu[mode][i]);  
          Serial.print(menu[mode][i]);      
        }
     
        if(menupos + 1 == i) {  // arrows around the current menu item
          lcd.write((uint8_t)LEFT);
          Serial.print('<');
        } 
        else {
          lcd.print(' ');
          Serial.print(' ');
        }
      }
    }
     
    void def_lineone(void) {
      def_lineone("");
    }
     
    void strip_lineone(void) {
      def_lineone(localized ? "Loc" : "Cont"); 
    }
     
    // line two default just shows the twirly.
    void def_linetwo(byte mode) {
      lcd.setCursor(0,1);
      //  lcd.print(freeMemory());
      //  lcd.print(' ');
      //  lcd.print(millis()); 
      twirl();
    }
     
    // This is in mid-refactor.  Focus mode was the original mode and so doesn't follow the normal pattern.
    void focus_linetwo(byte mode) {
      lcd.setCursor(0,1);
      //  lcd.print(freeMemory());
      //  lcd.print(" wow ");
      //  lcd.print(millis());   
      twirl();
    }
     
    // Print mode line two
    void print_linetwo(byte mode) {
      lcd.setCursor(0,1);
      lcd.print("Base: ");
     
      if(mode == SET_BASE) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(base_exp);
     
      if(mode == SET_BASE) {
        lcd.write((uint8_t)LEFT);
      }
     
      lcd.print(" Stops: ");
     
      if(mode == SET_STOPS) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(stops);
     
      if(mode == SET_STOPS) {
        lcd.write((uint8_t)LEFT);
      }
     
      lcd.print(" Exposure: ");
     
      lcd.print(base_exp * pow(2.0f, stops));
    }
     
    // test strip line two
    void strip_linetwo(byte mode) {
      lcd.setCursor(0,1);
      lcd.print("Base: ");
     
      if(mode == SET_BASE) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(base_exp);
     
      if(mode == SET_BASE) {
        lcd.write((uint8_t)LEFT);
      }
     
      lcd.print(" Steps: ");
     
      if(mode == SET_STEPS) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(steps);
     
      if(mode == SET_STEPS) {
        lcd.write((uint8_t)LEFT);
      }
     
      lcd.print(" Stride: ");
     
      if(mode == SET_STEPSIZE) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(stepsize);
     
      if(mode == SET_STEPSIZE) {
        lcd.write((uint8_t)LEFT);
      }
    }
     
    // config mode line two
    void config_linetwo(byte mode) {
      // Adjust the VFD brightness
      if(vfd_brightness >= 0 && vfd_brightness < 0x04)
        dim(vfd_brightness);
     
      lcd.setCursor(0,1);
      lcd.print("Brightness: ");
     
      if(mode == SET_VFD_BRIGHT) {
        lcd.write((uint8_t)RIGHT);
      }
     
      lcd.print(vfd_brightness);
     
      if(mode == SET_VFD_BRIGHT) {
        lcd.write((uint8_t)LEFT);
      }
     
      lcd.print(" Version: ");
      lcd.print(VERSION);
    }
     
    // is the math correct?  Print the computed steps to the serial console.
    void print_steps() {
      float last_exp = 0;
     
      for(int i = 0; i< steps; i++) {
        float this_exp = base_exp * pow(2.0f, (i / (1 / stepsize)));
     
        if(localized) {
          Serial.print(this_exp);
        } 
        else {
          Serial.print(this_exp - last_exp);      
        }
        Serial.print(' ');
     
        last_exp = this_exp;
      }
      Serial.println();
    }
     
    void print_print() {
     
    }
     
    void lamp_on(void) {
      Serial.println("lamp on");
      digitalWrite(lightPin, HIGH);
    }
     
    void lamp_off(void) {
      Serial.println("lamp off");  
      digitalWrite(lightPin, LOW);
    }
     
    // Interactively adjust an int variable.
    void adj_var(int *var, void (*linetwo)(byte), byte mode) {
      int loop = 1;
     
      (*linetwo)(mode);  // update the display
      delay(500);
     
      while(loop) {
        goButton.update();
        byte goButtonState = goButton.read();
     
        rotating = true;  // reset the debouncer
     
        if(goButtonState == LOW || lastReportedPos != encoderPos) {
          if (lastReportedPos != encoderPos) {
            Serial.print("Index:");
            Serial.println(encoderPos, DEC);
            if(lastReportedPos > encoderPos) {
              (*var)++;
            } 
            else if(lastReportedPos < encoderPos) {
              if(*var - 1 >= 0)
                (*var)--;
            }
     
            lastReportedPos = encoderPos;
            (*linetwo)(mode);  // update the display
          } 
          else if(goButtonState == LOW) {
            return;
          }
        }
      }
    }
     
    // Interactively adjust a float.
    void adj_var(float *var, void (*linetwo)(byte), byte mode) {
      int loop = 1;
     
      (*linetwo)(mode);  // update display.
      delay(500);
     
      while(loop) {
        goButton.update();
        byte goButtonState = goButton.read();
     
        rotating = true;  // reset the debouncer
     
        if(goButtonState == LOW || lastReportedPos != encoderPos) {
          if (lastReportedPos != encoderPos) {
            Serial.print("Index:");
            Serial.println(encoderPos, DEC);
            if(lastReportedPos > encoderPos) {
              switch(mode) {
              case SET_STOPS:  // if we are setting f stops, do it by stepsize
                (*var) += stepsize;
                break;
              default:
                (*var) += 0.01f;
                break;
              }
            } 
            else if(lastReportedPos < encoderPos) {
              switch(mode) {
              case SET_STOPS: // if we are setting f stops, do it by stepsize
                if(*var - stepsize >= 0)
                  (*var) -= stepsize;
                break;
              default:
                if(*var - 0.01f >= 0)
                  (*var) -= 0.01f;
                break;
              }
            }
     
            lastReportedPos = encoderPos;
            (*linetwo)(mode);  // update the display.
          } 
          else if(goButtonState == LOW) {
            return;
          }
        }
      }
    }
     
    void set_vfd_bright(void) {
      adj_var(&vfd_brightness, menulinetwo[mode], SET_VFD_BRIGHT);
    }
     
    // toggle the test strip mode
    void set_mode(void) {
      if(localized) {
        localized = 0; 
      } 
      else {
        localized = 1;
      } 
    }
     
    // set base exposure
    void set_base(void) {
      adj_var(&base_exp, menulinetwo[mode], SET_BASE);
      print_steps();
    }
     
    // set number of test strip steps
    void set_steps(void) {
      adj_var(&steps, menulinetwo[mode], SET_STEPS);
      print_steps();
    }
     
    // set step size
    void set_stepsize(void) {
      adj_var(&stepsize, menulinetwo[mode], SET_STEPSIZE);
      print_steps();
    }
     
    // set total number of stops
    void set_stops(void) {
      adj_var(&stops, menulinetwo[mode], SET_STOPS);
      print_print();
    }
     
    // print a test strip.
    void exec_strip(void) {
      unsigned long total_exp = 0;
      float last_exp = 0;
     
      for(int i = 0; i< steps; i++) {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Test Strip Step ");
        lcd.print(i+1);
        lcd.print(" of ");
        lcd.print(steps);
     
        float this_exp = base_exp * pow(2.0f, (i / (1 / stepsize)));
     
        Serial.print(this_exp - last_exp);
        Serial.print(' ');
     
        if(localized) {
          do_exposure(&total_exp, (this_exp) * 1000, TEST_STRIP);
        } 
        else {
          do_exposure(&total_exp, (this_exp - last_exp) * 1000, TEST_STRIP);
        }
     
        last_exp = this_exp;
     
        lcd.print(" Paused.");
        await_click();
      }
      Serial.println();
    }
     
    void exec_print(void) {
      unsigned long total_exp = 0;
     
      do_exposure(&total_exp, base_exp * pow(2.0f, stops) * 1000, PRINT);
    }
     
    void start_focus(byte wow) {
      byte goButtonState;
     
      do {
        Serial.println("wait");
        goButton.update();
        goButtonState = goButton.read();
      } 
      while(goButtonState == LOW);
     
      lamp_on();
     
      unsigned long from = millis();
     
      do {
        delay(1);
        lcd.setCursor(0,1);
     
        lcd.print("Total Exposure: ");
        lcd.print((millis() - from)/1000.0f);
     
        twirl();
     
        goButton.update();
        goButtonState = goButton.read();
        if(goButtonState == LOW) {
          break; 
        }
      }  
      while(1);
     
      lamp_off();
    }
     
    void end_focus() {  
    }
     
    float do_exposure(unsigned long *accumulated, unsigned long time, int test_strip) {
      lamp_on();
     
      byte goButtonState;
      int cancelled = 0;
      unsigned long so_far = 0;
      unsigned long from = millis();
      unsigned long until = millis() + (time);
      unsigned int last_chirp = 65534;
     
      clear_line_two();
     
      do {
        delay(1);
        lcd.setCursor(0,1);
        lcd.print("Remaining: ");  
        lcd.print(until - millis() < 100000 ? (until - millis())/1000.0f : 0); // hide wraparound issues.
     
        so_far = (millis() - from);
        lcd.print(" Total: ");
        lcd.print((*accumulated + so_far)/1000.0f);
     
        unsigned int chirp = (until - millis()) / 1000; 
     
        if(chirp == 65534) {
          last_chirp = chirp - 1; 
        }
     
        // chirp during the last 5  seconds.
        if(chirp <= 4 && chirp < last_chirp) {  
          tone(buzz, 1000, 1);
          last_chirp = (until - millis()) / 1000;
        }
     
        goButton.update();
        goButtonState = goButton.read();
        if(so_far > 500 && goButtonState == LOW) { // Print can be cancelled by clicking the button...but only after the first 500ms, since otherwise bouncing causes issues.
          for(int i=15000;i>1000;i-=1000) {  // beep the sad cancallation beep.
            tone(buzz, i, 100);
            delay(10);  
          }
          cancelled=1;
          break; 
        }
      }  
      while(millis() < until);
     
      lamp_off();
     
      *accumulated += so_far;
     
      if(test_strip) {
        tone(buzz, 1000, 100);
      } 
      else {
        if(!cancelled) {
          for(int i=1000;i<15000;i+=1000) {  // beep the happy success beep.
            tone(buzz, i, 100);
            delay(10);  
          }
        }
      }
     
      return time;
    }
     
    // wait for a click eventm
    void await_click() {
      byte goButtonState;
      do {
        goButton.update();
        goButtonState = goButton.read();
      } 
      while(goButtonState == HIGH);
    }
     
    // someone is holding down the button.  Display the number of seconds of hold down and await the end of the hold down.
    int await_unclick() {
      unsigned long start = millis();
      byte goButtonState;
      do {
        goButton.update();
        goButtonState = goButton.read();
        lcd.setCursor(39,1);
        lcd.print((millis() - start) / 1000);
      } 
      while(goButtonState == LOW);
     
      return(millis() - start);
    }
     
    void setup_lcd() {
      // Define two arrow-like characters for the currnt active menu item
      byte chars[][8]= {
        {
          B01000,
          B01100,
          B01110,
          B01111,
          B01110,
          B01100,
          B01000
        }
        ,
        {
          B00010,
          B00110,
          B01110,
          B11110,
          B01110,
          B00110,
          B00010
        }
        ,
        {
          0    }
      };
     
      for(int i = 0; chars[i][0]; i++) {
        lcd.createChar(i, chars[i]);  
      }
     
      lcd.begin(40, 2);
      lcd.clear();
     
      dim(0x03);
     
      lcd.print(VERSION);
     
      for(char i = 0; i<79; i++) {
        lcd.write(i);
      }
     
      delay(2000); 
    }
     
    // custom dim function based on the noritake VFD spec sheet.  LiquidCrystal library tweaked to allow this to work.
    void dim(int v) {
      lcd.command(LCD_FUNCTIONSET);
      lcd.write(v); 
    }
     
    void clear_line_two(void) {
      lcd.setCursor(0,1);
      for(int i = 0; i<40;i++) {
        lcd.print(' ');
      } 
    }
     
    // twirly!
    void twirl(void) {
      if(millis() - twirlycount > 100) {
        lcd.setCursor(39,1);
        lcd.print(twirlychar[twirly++%4]);
        twirlycount=millis();
      }  
    }
     
    // Oddly there is no clean way to reboot an arduino...this is better than nothing.
    void reset(void) {
      asm volatile("jmp 0");
    }
     
    // This is the guts of the rotary encoder code.  It works only okay.  Debouncing is hard.
    // Interrupt on A changing state
    void doEncoderA(){
      // debounce
      if ( rotating ) delay (1);  // wait a little until the bouncing is done
     
      // Test transition, did things really change? 
      if( digitalRead(encoderPinA) != A_set ) {  // debounce once more
        A_set = !A_set;
     
        // adjust counter + if A leads B
        if ( A_set && !B_set ) 
          encoderPos += 1;
     
        rotating = false;  // no more debouncing until loop() hits again
      }
    }
     
    // Interrupt on B changing state, same as A above
    void doEncoderB(){
      if ( rotating ) delay (1);
      if( digitalRead(encoderPinB) != B_set ) {
        B_set = !B_set;
        //  adjust counter - 1 if B leads A
        if( B_set && !A_set ) 
          encoderPos -= 1;
     
        rotating = false;
      }
    }

    A going away gift for a co-worker

    October 11th, 2014

    IMG_20140702_215906

    IMG_20140703_203702

    IMG_20140703_223002

    IMG_20140704_171225

    3D printer!

    October 11th, 2014

    A SeeMeCNC Rostock Max.

    IMG_20140429_122207

    IMG_20140429_144731

    IMG_20140429_155400

    IMG_20140429_164245

    IMG_20140429_184011

    IMG_20140429_203258

    IMG_20140430_180146

    IMG_20140430_211039

    IMG_20140501_201906

    IMG_20140502_203011

    IMG_20140502_215421

    IMG_20140502_224601

    IMG_20140502_225732

    IMG_20140502_230850

    IMG_20140503_102224

    IMG_20140503_132910

    IMG_20140503_161621

    IMG_20140504_194934

    IMG_20140510_151724

    Stairway lighting

    October 11th, 2014

    An arduino-mini clone, two ultrasonic distance sensors and a string of addressable leds.

    IMG_20140427_142720

    IMG_20140427_150929

    IMG_20140429_104852

    Wet plates!

    October 11th, 2014

    IMG_20140228_151907

    Print drying screens

    October 11th, 2014

    IMG_20140224_215336

    IMG_20140224_215408

    IMG_20140225_211308

    IMG_20140226_212102

    IMG_20140226_215126

    Second sink for the darkroom

    October 11th, 2014

    IMG_20140222_111142

    IMG_20140222_115917

    IMG_20140223_123010

    IMG_20140223_134324

    IMG_20140224_192808

    UV light box

    October 11th, 2014

    IMG_20140219_211027

    IMG_20140219_220202

    IMG_20140220_185120

    IMG_20140220_185836

    IMG_20140220_191052

    IMG_20140220_220851

    IMG_20140220_222836

    IMG_20140220_223435

    IMG_20140220_223156

    Darkroom B.

    October 11th, 2014

    This is my second dark room. Substantially larger than the last one.

    IMG_20131130_152428

    IMG_20131208_141227

    IMG_20131208_165810

    IMG_20131209_193922

    IMG_20131209_194722

    IMG_20131210_192950

    IMG_20131215_141939

    IMG_20131215_163348

    IMG_20131215_192307

    IMG_20131217_165052

    IMG_20131218_171242

    IMG_20131219_140922

    IMG_20131219_232101

    IMG_20131220_120419

    IMG_20131226_142531

    IMG_20131227_210506

    IMG_20131228_164510

    IMG_20131229_142315

    IMG_20131230_205131

    An SDR!

    October 11th, 2014

    An RTL-SDR, a Ham-it-Up up-converter and some antenna stuff.

    IMG_20130805_232951

    IMG_20130804_153146

    IMG_20130805_233002

    IMG_20130817_194051

    Looking for something?

    Use the form below to search the site:

    Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

    Visit our friends!

    A few highly recommended friends...

    Archives

    All entries, chronologically...