Code part 1:
#include <QueueList.h>
#include <Utility.h>
#include <LiquidCrystal.h>
#include <Bounce.h>
#include <avr/pgmspace.h>
#include <avr/power.h>
char* softwareVersion = "0.3";
//Comment the next line out to have normal operation
#define TEST_WITH_DEBUG
/* Previous "BibTimePair.h": */
class BibTimePair
{
public:
BibTimePair( unsigned int bib, unsigned long time ) {
m_bib = bib;
m_time = time;
m_sent = false;
}
void setSent( boolean b ) { m_sent = b; }
unsigned int bib() const { return m_bib;}
unsigned long time() const { return m_time; }
boolean sent() const { return m_sent; }
private:
unsigned int m_bib;
unsigned long m_time;
boolean m_sent;
};
/* Previous "communication.h": */
boolean sendResult( unsigned int bib, unsigned long time );
void updateSerial();
const char* millisToTime( unsigned long time );
const char* millisToDisplayTime(unsigned long time);
/* Previous "Titagremo.h": */
void addTime( unsigned long time );
void addBib( unsigned int bib );
void sendAllToComputer();
unsigned long timeNow();
void showTime();
void newTime();
void stopButton1Pressed();
void keypadButtonPressed();
boolean setTime();
void newBib(int firstDigit);
unsigned long referenceTime;
char lcdPins[] = { 6, 5, 4, 3, 2, 1};
LiquidCrystal lcd = LiquidCrystal(lcdPins[0], lcdPins[1], lcdPins[2], lcdPins[3], lcdPins[4], lcdPins[5]);
int keypadPins[12] = { 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
#ifndef TEST_WITH_DEBUG
#define NORMAL_OPERATION
#endif
#define KEY1 23
#define KEY2 27
#define KEY3 31
#define KEY4 22
#define KEY5 26
#define KEY6 30
#define KEY7 21
#define KEY8 25
#define KEY9 29
#define KEY0 24
#define KEYE 28
#define KEYC 20
#define ENTER 10
#define CLEAR 11
#define NOTHING 13
#define GETKEY1 key1.read()
#define GETKEY2 key2.read()
#define GETKEY3 key3.read()
#define GETKEY4 key4.read()
#define GETKEY5 key5.read()
#define GETKEY6 key6.read()
#define GETKEY7 key7.read()
#define GETKEY8 key8.read()
#define GETKEY9 key9.read()
#define GETKEY0 key0.read()
#define GETKEYE keyE.read()
#define GETKEYC keyC.read()
Bounce key1 = Bounce(KEY1, 5);
Bounce key2 = Bounce(KEY2, 5);
Bounce key3 = Bounce(KEY3, 5);
Bounce key4 = Bounce(KEY4, 5);
Bounce key5 = Bounce(KEY5, 5);
Bounce key6 = Bounce(KEY6, 5);
Bounce key7 = Bounce(KEY7, 5);
Bounce key8 = Bounce(KEY8, 5);
Bounce key9 = Bounce(KEY9, 5);
Bounce key0 = Bounce(KEY0, 5);
Bounce keyE = Bounce(KEYE, 5);
Bounce keyC = Bounce(KEYC, 5);
int getInt();
const byte triggerPins PROGMEM = 10;
const byte ledPin PROGMEM = 0;
const byte backlightPin PROGMEM = 14;
const int digitMultiplier[] PROGMEM = { 0,10,100,1000,10000,100000,1000000};
void setup(){
#ifdef TEST_WITH_DEBUG
//Init the Serial communication
Serial.begin(9600);
Serial.println("Serial inited");
#endif
#ifdef NORMAL_OPERATION
//Init the Serial communication
Serial.begin(1200);
#endif
//Init the LCD with 16 columns and 2 rows
lcd.begin(16, 2);
#ifdef TEST_WITH_DEBUG
Serial.println("LCD inited");
#endif
//Init the Keypad pins
foreach(keypadPins, 12, pinMode, INPUT); //set the keypad pins to inputs
//foreach(keypadPins, 12, digitalWrite, HIGH); //turn on the internal Pull-Up resistors
#ifdef TEST_WITH_DEBUG
Serial.println("Keypad pins inited");
#endif
//Init the LED pin and set the LED low (on)
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
#ifdef TEST_WITH_DEBUG
Serial.println("LED inited");
#endif
//Init the backlight (for the LCD) pin and set it to PWM200
pinMode(backlightPin, OUTPUT);
analogWrite(backlightPin, 200);
#ifdef TEST_WITH_DEBUG
Serial.println("LCD backlight inited");
#endif
//Init the trigger pins
pinMode(triggerPins, INPUT);
#ifdef TEST_WITH_DEBUG
Serial.println("Trigger pins inited");
#endif
//Init the interrupt
attachInterrupt(0, stopButton1Pressed, RISING);
#ifdef TEST_WITH_DEBUG
Serial.println("Interrupt for the trigger pins attached");
#endif
//set the power modes
power_adc_disable(); //Disable ADC
power_spi_disable(); //Disable SPI
power_twi_disable(); //Disable I2C (TWI)
#ifdef TEST_WITH_DEBUG
Serial.println("Disabled ADC, SPI and I2C");
#endif
//Show a beginning in the LCD
lcd.clear();
lcd.print("TiTaGreMo v.");
lcd.print(softwareVersion);
lcd.setCursor(0,1);
lcd.print(" (C): Dalheimer ");
#ifdef TEST_WITH_DEBUG
Serial.println("Printed beginning");
#endif
//Wait 1 second
delay(5000);
//Clear the display
lcd.clear();
#ifdef TEST_WITH_DEBUG
Serial.println("Waited 1s and then cleared the LCD");
#endif
//Set the time until it returns false
while( !setTime() );
#ifdef TEST_WITH_DEBUG
Serial.println("Wait for time to be set");
#endif
attachInterrupt(1, keypadButtonPressed, FALLING);
#ifdef TEST_WITH_DEBUG
Serial.println("Interrupt for keypad attached");
#endif
//Show the time
showTime();
#ifdef TEST_WITH_DEBUG
Serial.println("Time displayed. setup() is now completed");
#endif
}
char numbers[5] = {0,0,0,0,0};
char digitForNumbers = 4;
int numberNow;
int timeAbc;
int timeAbcAdd;
void loop(){
sendAllToComputer();
updateSerial();
showTime();
if(numberNow != 0)
lcd.print(numberNow);
/*timeAbc = millis();
timeAbcAdd = timeAbc + 1000;
while(timeAbc != timeAbcAdd);*/delay(1000);
}
void keypadButtonPressed(){
detachInterrupt(1);
int newInt = getInt();
if(newInt != NOTHING && newInt != ENTER && newInt != CLEAR){
newInt = numbers[digitForNumbers];
digitForNumbers--;
lcd.print(newInt);
}
else if(newInt == ENTER){
addBib( (numbers[0] * 1) + (numbers[1] * 10) + (numbers[2] * 100) + (numbers[3] * 1000) + (numbers[4] * 10000) );
numbers[0] = 0;
numbers[1] = 0;
numbers[2] = 0;
numbers[3] = 0;
numbers[4] = 0;
numbers[5] = 0;
digitForNumbers = 4;
}
else if(newInt == CLEAR){
numbers[0] = 0;
numbers[1] = 0;
numbers[2] = 0;
numbers[3] = 0;
numbers[4] = 0;
numbers[5] = 0;
digitForNumbers = 4;
showTime();
}
numberNow = (numbers[0] * 1) + (numbers[1] * 10) + (numbers[2] * 100) + (numbers[3] * 1000) + (numbers[4] * 10000);
attachInterrupt(1, keypadButtonPressed, FALLING);
return;
}
/* Previous "bua.cpp": */
unsigned long timeNow()
{
// referenceTime is the milliseconds the Arduino clock is off from the "milliseconds since midnight"
return referenceTime+millis();
}