Time Tracker/old

/*
  */

// include the library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"


// Pins for LCD
#define PIN_LCD_D7 12
#define PIN_LCD_D6 13
#define PIN_LCD_D5 14
#define PIN_LCD_D4 15
#define PIN_LCD_RS 21
#define PIN_LCD_EN 20

// Input pin from laser cutter mobo
#define LASER_PIN 0

// Button connected here will reset the counter
#define RESET_PIN 4

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7);

int lastPos;
unsigned long time_total=0;
unsigned long time_start=0;
unsigned long time_last=0;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print(" Laser Odometer ");
  
  pinMode(LASER_PIN, INPUT);
  digitalWrite(LASER_PIN, HIGH); // enable internal pullup
  
  pinMode(RESET_PIN, INPUT);
  digitalWrite(RESET_PIN, HIGH);
  
  lastPos = EEPROM.read(0);
  if(lastPos == 0x11) {
    EEPROM_readAnything(0x11, time_total);
  } else if(lastPos == 0x22) {
    EEPROM_readAnything(0x22, time_total);
  } else {
    lastPos = 0x22;
    time_total = 0;
  }
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  
  if(digitalRead(RESET_PIN) == 0) {
    time_total = 11400000L;
    EEPROM_writeAnything(0x11, time_total);
    EEPROM_writeAnything(0x22, time_total);
  }
  
  unsigned long time_tmp = time_total;
  if(time_last > 0 && time_start > 0) {
    time_tmp += (time_last - time_start);
  }    
  
  if(digitalRead(LASER_PIN) == 0) { // 0 --> laser is firing
    if(time_start == 0) {
      time_start = time_last = millis();
    } else {
      time_last = millis();
    }
  } else if(time_start > 0) {
    if(millis() - time_last > 10000) {
      time_total += time_last - time_start;
      time_last = time_start = 0;
    } else if(millis() - time_last > 1000) {      
      if(lastPos == 0x11) {
        EEPROM_writeAnything(0x22, time_tmp);
        EEPROM.write(0, 0x22);
        lastPos = 0x22;
      } else {
        EEPROM_writeAnything(0x11, time_tmp);
        EEPROM.write(0, 0x11);
        lastPos = 0x11;
      }
    }
  }
  

  lcd.print(time_tmp/1000);
  
  lcd.setCursor(14,1);
  if(time_start > 0) {
    switch((time_tmp/200) % 4) {
      case 0: lcd.print('|'); break;
      case 1: lcd.print('/'); break;
      case 2: lcd.print('-'); break;
      case 3: lcd.print('/'); break;
    }
  } else {
    lcd.print(" ");
  }
}


*”’Category:”'[[Category:Laser]] [[Category:Laser User Information‏‎s]]

Leave a Reply

Your email address will not be published. Required fields are marked *