Pages: [1]   Go Down
Author Topic: Time taking device  (Read 1228 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm building a device for time taking at cross country ski races. I have already come a long way but I still have a problem; when I try to enter the time, after pressing ONE key, the rest of the hours/minutes are filled out at random.

The code is available here: https://sourceforge.net/projects/titagremo/ but will even be posted here soon.

Image of the connections:

Please help, i think the problem is in the setTime function, but I don't have a clue what the problem really is,
JanD

EDIT: The code uses the following libraries: QueueList, Utility, LiquidCrystal and Bounce. Just search for them on google.
« Last Edit: January 28, 2011, 11:39:24 am by JanD » Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code part 1:
Code:
#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();
}

Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code part 2:
Code:

boolean setTime()
{
    // Assume the device was turned on at 12:00, and the user set the time to 14:00.
    // In this case, millis() will return 2h*60min*60sec*1000ms = 7,200,000 milliseconds.
    // 14h*60min*60sec*1000ms = 50,400,000 milliseconds is what we want for the current time
    // Thus, the reference time, to be added to any further time entries, is
    // 50,400,000 - 7,200,000 = 43,200,000, which is 12:00, which is the time the device was
    // turned on.

    // The case in which the device was turned on before midnight: Assume it was turned on at 22:00,
    // and the user set the time to 14:00. In this case, millis() will return 16h*60*60*1000 = 57,600,000,
    // and the entered time will be 14h*60*60*1000 = 50,400,000. The reference time will thus be -7,200,000.
    // Now assume that the device calls timeNow() at 15:00 in order to record a finish time.
    // millis() will return 17h*60*60*1000 = 61,200,000. timeNow() will thus return
    // -7,200,000 + 61,200,000 = 54,000,000 which is exactly what is expected for 15:00.
   
    lcd.clear();
    lcd.print("Enter time");
    lcd.setCursor(0,1);
    lcd.print("00:00");
#ifdef TEST_WITH_DEBUG
    Serial.println("In setTime function");
#endif
   
    int firstDigit;
    do{
      firstDigit = getInt();
      if(firstDigit == CLEAR)
        return false;
      if(firstDigit != ENTER && firstDigit != CLEAR && firstDigit != NOTHING && firstDigit != 3 && firstDigit != 4 && firstDigit != 5 && firstDigit != 6 && firstDigit != 7 && firstDigit != 8 && firstDigit != 9)
        break;
    }while(true);
   
    lcd.clear();
    lcd.print("Enter time");
    lcd.setCursor(0,1);
    lcd.print(firstDigit);
    lcd.print("0:00");
   
    int secondDigit;
    do{
      secondDigit = getInt();
      if(secondDigit == CLEAR)
        return false;
      if(secondDigit != ENTER && secondDigit != CLEAR && secondDigit != NOTHING){
        if(firstDigit == 2){
          if(secondDigit == 5 && secondDigit != 6 && secondDigit != 7 && secondDigit != 8 && secondDigit != 9);
          else break;
        }
        else break;
      }
    }while(true);
   
    lcd.clear();
    lcd.print("Enter time");
    lcd.setCursor(0,1);
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(":00");
   
    int thirdDigit;
    do{
      thirdDigit = getInt();
      if(thirdDigit == CLEAR)
        return false;
      if(thirdDigit != ENTER && thirdDigit != CLEAR && thirdDigit != NOTHING && thirdDigit != 6 && thirdDigit != 7 && thirdDigit != 8 && thirdDigit != 9)
        break;
    }while(true);
   
    lcd.clear();
    lcd.print("Enter time");
    lcd.setCursor(0,1);
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(":");
    lcd.print(thirdDigit);
    lcd.print("0");
   
    int fourthDigit;
    do{
      fourthDigit = getInt();
      if(fourthDigit == CLEAR)
        return false;
      if(fourthDigit != ENTER && fourthDigit != CLEAR && fourthDigit != NOTHING)
        break;
    }while(true);
   
    lcd.clear();
    lcd.print("Enter time");
    lcd.setCursor(0,1);
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(":");
    lcd.print(thirdDigit);
    lcd.print(fourthDigit);
   
    int fifthDigit;
    do{
      fifthDigit = getInt();
      if(fifthDigit == CLEAR)
        return false;
      if(fifthDigit == ENTER)
        break;
    }while(true);
   
    unsigned long millisNow = millis(); // store now so that we don't miss milliseconds while doing the following computation
   
    int enteredTime = (firstDigit * 1000) + (secondDigit * 100) + (thirdDigit * 10) + (fourthDigit * 1);
    unsigned long enteredTimeMillis = (enteredTime/1000)*36000000 +
                                      ((enteredTime%1000)/100)*3600000 +
                                      ((enteredTime%100)/10)*600000 +
                                      (enteredTime%10)*60000;

    referenceTime = enteredTimeMillis-millisNow;

    // PENDING(jan) Output the new reference time on the display for testing?
   
    showTime();
   
    return true;
}

void showTime()
{
  // show the time at xxxxxxxxxxxxxxxx
  //                  xxxx24:59:59xxxx
  lcd.clear();
  lcd.setCursor(4,1);
  unsigned long timeNoW = timeNow();
  const char* timeToPrint = millisToDisplayTime(timeNoW);
  lcd.print(timeToPrint);
  lcd.home();
 
  //delay(10);
}

inline void newTime()
{
    addTime(timeNow());
}

inline void stopButton1Pressed()
{
  newTime();
}

void newBib(int firstDigit){
  int bib = 0;
 
  showTime();
  lcd.print("Add bib: ");
  lcd.print(firstDigit);
 
  //Read the second digit
  int secondDigit;
  do{
    secondDigit = getInt();
    if(secondDigit == ENTER){
      bib = firstDigit;
      addBib(bib);
      return;
    }
    else if(secondDigit == CLEAR)
      return;
    if(secondDigit != ENTER && secondDigit != CLEAR && secondDigit != NOTHING)
      break;
     
    showTime();
    lcd.print("Add bib: ");
    lcd.print(firstDigit);
  }while(secondDigit == NOTHING);
 
 
  //Read the third digit
  int thirdDigit;
  do{
    thirdDigit = getInt();
    if(thirdDigit == ENTER){
      firstDigit = firstDigit * 10;
      bib = firstDigit + secondDigit;
      addBib(bib);
      return;
    }
    else if(thirdDigit == CLEAR)
      break;
     
    showTime();
    lcd.print("Add bib: ");
    lcd.print(firstDigit);
    lcd.print(secondDigit);
  }while(thirdDigit == NOTHING);
 
  //Read the fourth digit
  int fourthDigit;
  do{
    fourthDigit = getInt();
    if(fourthDigit == ENTER){
      firstDigit = firstDigit * 100;
      secondDigit = secondDigit * 10;
      bib = firstDigit + secondDigit + thirdDigit;
      addBib(bib);
      return;
    }
    else if(fourthDigit == CLEAR)
      break;
     
    showTime();
    lcd.print("Add bib: ");
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(thirdDigit);
  }while(fourthDigit == NOTHING);
 
  //Read the fifth digit
  int fifthDigit;
  do{
    fifthDigit = getInt();
    if(fifthDigit == ENTER){
      firstDigit = firstDigit * 1000;
      secondDigit = secondDigit * 100;
      thirdDigit = thirdDigit * 10;
      bib = firstDigit + secondDigit + thirdDigit + fourthDigit;
      addBib(bib);
      return;
    }
    else if(fifthDigit == CLEAR)
      break;
     
    showTime();
    lcd.print("Add bib: ");
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(thirdDigit);
    lcd.print(fourthDigit);
  }while(fifthDigit == NOTHING);
 
  //Read the fifth digit
  int sixthDigit;
  do{
    sixthDigit = getInt();
    if(sixthDigit == ENTER){
      firstDigit = firstDigit * 10000;
      secondDigit = secondDigit * 1000;
      thirdDigit = thirdDigit * 100;
      fifthDigit = fifthDigit * 10;
      bib = firstDigit + secondDigit + thirdDigit + fourthDigit + fifthDigit;
      addBib(bib);
      return;
    }
    else if(sixthDigit == CLEAR)
      break;
     
    showTime();
    lcd.print("Add bib: ");
    lcd.print(firstDigit);
    lcd.print(secondDigit);
    lcd.print(thirdDigit);
    lcd.print(fourthDigit);
    lcd.print(fifthDigit);
  }while(sixthDigit == NOTHING);
 
  addBib(65536);
}
Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code part 3:
Code:

/* Previous keypad.cpp: */

int getInt(){
  if (key1.update()) {
    if (!key1.read()) {
return 1;
    }
  }
  if (key2.update()) {
    if (!key2.read()) {
return 2;
    }
  }
  if (key3.update()) {
    if (!key3.read()) {
return 3;
    }
  }
  if (key4.update()) {
    if (!key4.read()) {
return 4;
    }
  }
  if (key5.update()) {
    if (!key5.read()) {
return 5;
    }
  }
  if (key6.update()) {
    if (!key6.read()) {
return 6;
    }
  }
  if (key7.update()) {
    if (!key7.read()) {
return 7;
    }
  }
  if (key8.update()) {
    if (!key8.read()) {
return 8;
    }
  }
  if (key9.update()) {
    if (!key9.read()) {
return 9;
    }
  }
  if (key0.update()) {
    if (!key0.read()) {
return 0;
    }
  }
  if (keyE.update()) {
    if (!keyE.read()) {
return ENTER;
    }
  }
  if (keyC.update()) {
    if (!keyC.read()) {
return CLEAR;
    }
  }
  return NOTHING;/*
  if(digitalRead(KEY1) == LOW) return 1;
  else if(digitalRead(KEY2) == LOW) return 2;
  else if(digitalRead(KEY3) == LOW) return 3;
  else if(digitalRead(KEY4) == LOW) return 4;
  else if(digitalRead(KEY5) == LOW) return 5;
  else if(digitalRead(KEY6) == LOW) return 6;
  else if(digitalRead(KEY7) == LOW) return 7;
  else if(digitalRead(KEY8) == LOW) return 8;
  else if(digitalRead(KEY9) == LOW) return 9;
  else if(digitalRead(KEY0) == LOW) return 0;
  else if(digitalRead(KEYE) == LOW) return ENTER;
  else if(digitalRead(KEYC) == LOW) return CLEAR;
  else return NOTHING;*/
}

/* Previous "communication.cpp" */

unsigned int lastBib;
unsigned long lastTime;

boolean sendResult( unsigned int bib, unsigned long time ){
    if(Serial.available() == false)
        return false;

    if(Serial.peek() != 0x3F)
        return false;

    if(Serial.peek() != 0x0D)
        return false;

    if(bib > 10){
        Serial.print("Amr00000");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else if(bib > 100){
        Serial.print("Amr0000");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else if(bib > 1000){
        Serial.print("Amr000");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else if(bib > 10000){
        Serial.print("Amr00");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else if(bib > 100000){
        Serial.print("Amr0");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else if(bib > 1000000){
        Serial.print("Amr");
        Serial.print(char(bib));
        Serial.print(millisToTime(time));
        Serial.print("pijc");
        Serial.print(0x0D);
        lastTime = time;
        lastBib = bib;
        delay(1);
        if(Serial.peek() == '+'){
            Serial.read();
            Serial.read();
            return true;
        }
        else if(Serial.peek() != '+'){
            Serial.read();
            Serial.read();
            return false;
        }
    }
    else return false;
}

void updateSerial(){
    char data = Serial.read();
    char data2 = Serial.read();
    if(data2 != 0x0D || data2 != '-') return;
    if(data == '?'){
        sendAllToComputer();
    return;
    }
    if(data == '?' && lastBib != 0 && lastTime != 0){
        sendResult(lastBib, lastTime);
        return;
    }
    else if(data == '+'){
        lastBib = 0, lastTime = 0;
        return;
    }
    else if(data == '!'){
        Serial.read();
        Serial.read();
        Serial.read();
        Serial.read();
        Serial.read();
        return;
    }
    return;
}

// Formats the time specified as mmm:ss.mmm
const char* millisToTime( unsigned long time )
{
    // mmm:ss.mmm
    // 0123456789

    // the m's and s's will be overwritten below
    static char result[11] = "mmm:ss.mmm";

    unsigned int milliseconds = time % 1000;
    unsigned long remainder = time / 1000;
    unsigned int seconds = remainder % 60;
    unsigned long minutes = remainder / 60;

    // convert milliseconds
    result[9] = char(milliseconds%10)+'0';
    result[8] = char((milliseconds/10)%10)+'0';
    result[7] = char((milliseconds/100)%10)+'0';

    // convert seconds
    result[5] = char(seconds%10)+'0';
    result[4] = char((seconds/10)%10)+'0';

    // convert minutes
    result[2] = char(minutes%10)+'0';
    result[1] = char((minutes/10)%10)+'0';
    result[0] = char((minutes/100)%10)+'0';

    return &result[0];
}


// Formats the time specified as hh:mm:ss
const char* millisToDisplayTime( unsigned long time )
{
    // hh:mm:ss
    // 01234567

    // the m's and s's will be overwritten below
    static char result[9] = "hh:mm:ss";

    unsigned int milliseconds = time % 1000;
    unsigned long remainder = time / 1000;
    unsigned int seconds = remainder % 60;
    unsigned long minutes = remainder / 60;
    unsigned int hours = minutes / 60;
    // fix minutes
    minutes -= (hours*60);

    // convert seconds
    result[7] = char(seconds%10)+'0';
    result[6] = char(seconds/10)+'0';

    // convert minutes
    result[4] = char(minutes%10)+'0';
    result[3] = char(minutes/10)+'0';

    // convert hours
    result[1] = char(hours%10)+'0';
    result[0] = char(hours/10)+'0';

    return &result[0];
}


/* Previous "Titagremo.cpp": */

QueueList<unsigned long> timesPendingList; // keeps the times when the Finish button was pressed
QueueList<BibTimePair> resultsList; // keeps bibs and associated times


void addTime( unsigned long time )
{
    timesPendingList.push( time );
}

void addBib( unsigned int bib )
{
  // Take the oldest pending time out of the pending list, associate it with the bib, and stick that pair into the results list.
    if( timesPendingList.isEmpty() ) {
        // Ooops, we are trying to add a bib even though there is no pending time
       
        /*showTime();
        lcd.home();
        lcd.print("No time!");
       
        delay(1000);*/
       
        return;
    }
   
    showTime();
    lcd.home();
    lcd.print("Added bib ");
    lcd.print(bib);
   
    delay(1000);
 
    unsigned long pendingTime = timesPendingList.pop();

    BibTimePair newPair( bib, pendingTime );
    newPair.setSent( sendResult( bib, pendingTime ) );
    resultsList.push( newPair );
}

void sendAllToComputer()
{
    // PENDING(kalle) It might be safer to keep a copy of the list; write to flash? After this operation, the list is destroyed.
    while( !resultsList.isEmpty() ) {
        BibTimePair pair = resultsList.pop();
        if( !sendResult( pair.bib(), pair.time() ) )
            ; // PENDING(kalle) Big bummer, we couldn't send, and the data is gone. Maybe stick it into a not-yet-sent list
    }
}

So, what was (finally) the whole code,
JanD
Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for that. The links to the libraries are:

QueueList: http://www.arduino.cc/playground/Code/QueueList
Utility: http://www.arduino.cc/playground/Code/Utility
LiquidCrystal: Included then downloading the Arduino IDE, http://www.arduino.cc/en/Reference/LiquidCrystal
Bounce: http://www.arduino.cc/playground/Code/Bounce

A schematic(sorry if it isn't the very good):

If you have problems reading the code I can post only the snippets there I think the error can be.

Jan
Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I know more specific there the problem is now:
Code:
int getInt(){
  if (key1.update()) {
    if (!key1.read()) {
return 1;
    }
  }
  if (key2.update()) {
    if (!key2.read()) {
return 2;
    }
  }
  if (key3.update()) {
    if (!key3.read()) {
return 3;
    }
  }
  if (key4.update()) {
    if (!key4.read()) {
return 4;
    }
  }
  if (key5.update()) {
    if (!key5.read()) {
return 5;
    }
  }
  if (key6.update()) {
    if (!key6.read()) {
return 6;
    }
  }
  if (key7.update()) {
    if (!key7.read()) {
return 7;
    }
  }
  if (key8.update()) {
    if (!key8.read()) {
return 8;
    }
  }
  if (key9.update()) {
    if (!key9.read()) {
return 9;
    }
  }
  if (key0.update()) {
    if (!key0.read()) {
return 0;
    }
  }
  if (keyE.update()) {
    if (!keyE.read()) {
return ENTER;
    }
  }
  if (keyC.update()) {
    if (!keyC.read()) {
return CLEAR;
    }
  }
  return NOTHING;
}

The thing that should happen is that each key is updated (key# are objects of type Bounce) and if it has changed and the is now LOW (or false, or 0) the number is returned. But it seams as if it just falls through the if statements. Thats the problem here!!!???

Jan
Logged

Sweden
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Error no longer error!!!!
So. Now I have a new problem: When I try to press a key to add a bib there is nothing displayed on the LCD. What's happening?

Relevant code snippets:
Code:
void loop(){
  sendAllToComputer();
  updateSerial();
  showTime();
 
  if(numberNow != 0)
    lcd.print(numberNow);
 
  /*timeAbc = millis();
  timeAbcAdd = timeAbc + 1000;
 
  while(timeAbc != timeAbcAdd);*/delay(1000);
}

Code:
void keypadButtonPressed(){
  detachInterrupt(1);
  int newInt = processKeypad();
  if(newInt != NOTHING && newInt != ENTER && newInt != CLEAR){
    newInt = numbers[digitForNumbers];
    digitForNumbers--;
    lcd.print(newInt);
    numberNow = (numbers[0] * 1) + (numbers[1] * 10) + (numbers[2] * 100) + (numbers[3] * 1000) + (numbers[4] * 10000);
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
  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;
    numberNow = 0;
    digitForNumbers = 4;
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
  else if(newInt == CLEAR){
    numbers[0] = 0;
    numbers[1] = 0;
    numbers[2] = 0;
    numbers[3] = 0;
    numbers[4] = 0;
    numbers[5] = 0;
    numberNow = 0;
    digitForNumbers = 4;
    showTime();
    attachInterrupt(1, keypadButtonPressed, FALLING);
    return;
  }
}

Code:
int processKeypad(){
  for(int keys = 0; keys < 12; keys++){
    int valueNow = digitalRead(keypadPins[keys]);
    if(valueNow != lastKeypadStatus[keys] && valueNow == LOW){
      lastKeypadStatus[keys] = valueNow;
      return numbersOnKeys[keys];
    }
    lastKeypadStatus[keys] = valueNow;
  }
  return NOTHING;
}

Code:
void showTime(){
  // show the time at xxxxxxxxxxxxxxxx
  //                  xxxx24:59:59xxxx
  lcd.clear();
  lcd.setCursor(4,1);
  unsigned long timeNoW = timeNow();
  const char* timeToPrint = millisToDisplayTime(timeNoW);
  lcd.print(timeToPrint);
  lcd.home();
}

Please help me once again.

Jan
Logged

Pages: [1]   Go Up
Jump to: