Another Wrold
Published © GPL3+

Rubik Timer

Basic Rubik Timer

IntermediateWork in progress3 hours71
Rubik Timer

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
4-digit 7-segment LED Display TM1637
DIYables 4-digit 7-segment LED Display TM1637
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1
itr9909
×2
Resistor 4.75k ohm
Resistor 4.75k ohm
×2
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 100 ohm
Resistor 100 ohm
×2
Resistor 220 ohm
Resistor 220 ohm
×2
LED (generic)
LED (generic)
×2
Battery, 3.7 V
Battery, 3.7 V
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering
Breadboard, 170 Pin
Breadboard, 170 Pin
Breadboard, 400 Pin
Breadboard, 400 Pin

Story

Read more

Code

Rubik Timer

C/C++
#include <EEPROM.h>
#include <TimerOne.h>
#include <avr/pgmspace.h>
#include "TM1637.h"
#define ON 1
#define OFF 0

int8_t TimeDisp[] = {0x00, 0x00, 0x00, 0x00};
unsigned char ClockPoint = 1;
unsigned char Update;
unsigned char microsecond_10 = 0;
unsigned char second;
unsigned char _microsecond_10 = 0;
unsigned char _second;
unsigned int eepromaddr;
boolean Flag_ReadTime;
int state;

#define CLK 16//pins definitions for TM1637 and can be changed to other ports
#define DIO 17
TM1637 tm1637(CLK, DIO);
int L;
int R;

void setup()
{
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(15, INPUT);
  tm1637.init();
  Timer1.initialize(10000);//timing for 10ms
  Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR
  tm1637.set(BRIGHT_DARKEST);
  state = 0;
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  L = 960;
  R = 970;
}
void loop()
{
  stopwatchReset();
  while (1)
  {
    if (analogRead(7) < R && analogRead(0) < L && state == 0)
    {
      while (analogRead(7) < R && analogRead(0) < R)
      {
        digitalWrite(6, HIGH);
        digitalWrite(5, HIGH);
        delay(100);
        if (analogRead(7) > R || analogRead(0) > L)
        {
          digitalWrite(5, LOW);
          digitalWrite(6, LOW);
          stopwatchStart();
          state = 1;
          Serial.println(state);
          break;
        }
      }
    }
    if (analogRead(7) < R && analogRead(0) < L && state == 1)
    {
      stopwatchPause();
      Serial.println(state);
      state = 2;
      Serial.println(state);
      delay(500);
    }
    if (digitalRead(15) == HIGH && (state == 2 || state == 1))
    {
      stopwatchReset();
      state = 0;
      Serial.println(state);
    }
    if (analogRead(7) < R && state == 1)
    {
      digitalWrite(6, HIGH);
    }
    if (analogRead(0) < L && state == 1)
    {
      digitalWrite(6, HIGH);
    }
    if (analogRead(7) < R && analogRead(0) < L && state == 2)
    {
      digitalWrite(6, HIGH);
    }

    //    case 'S':stopwatchStart();Serial.println("Start timing...");break;
    //    case 'P':stopwatchPause();Serial.println("Stopwatch was paused");break;
    //    case 'L':readTime();break;
    //    case 'W':saveTime();Serial.println("Save the time");break;
    //    case 'R':stopwatchReset();Serial.println("Stopwatch was reset");break;
    if (Update == ON)
    {
      TimeUpdate();
      tm1637.display(TimeDisp);
    }
    else
    {
      digitalWrite(6, LOW);
    }
  }
}





void TimingISR()
{
  microsecond_10 ++;
  Update = ON;
  if (microsecond_10 == 100) {
    second ++;
    if (second == 60)
    {
      second = 0;
    }
    microsecond_10 = 0;
  }
  ClockPoint = (~ClockPoint) & 0x01;
  if (Flag_ReadTime == 0)
  {
    _microsecond_10 = microsecond_10;
    _second = second;
  }
}
void TimeUpdate(void)
{
  if (ClockPoint)tm1637.point(POINT_ON); //POINT_ON = 1,POINT_OFF = 0;
  else tm1637.point(POINT_ON);
  TimeDisp[2] = _microsecond_10 / 10;
  TimeDisp[3] = _microsecond_10 % 10;
  TimeDisp[0] = _second / 10;
  TimeDisp[1] = _second % 10;
  Update = OFF;
}
void stopwatchStart()//timer1 on
{
  Flag_ReadTime = 0;
  TCCR1B |= Timer1.clockSelectBits;
}
void stopwatchPause()//timer1 off if [CS12 CS11 CS10] is [0 0 0].
{
  TCCR1B &= ~(_BV(CS10) | _BV(CS11) | _BV(CS12));
}
void stopwatchReset()
{
  stopwatchPause();
  Flag_ReadTime = 0;
  _microsecond_10 = 0;
  _second = 0;
  microsecond_10 = 0;
  second = 0;
  Update = ON;
}
void saveTime()
{
  EEPROM.write(eepromaddr ++, microsecond_10);
  EEPROM.write(eepromaddr ++, second);
}
void readTime()
{
  Flag_ReadTime = 1;
  if (eepromaddr == 0)
  {
    Serial.println("The time had been read");
    _microsecond_10 = 0;
    _second = 0;
  }
  else {
    _second = EEPROM.read(-- eepromaddr);
    _microsecond_10 = EEPROM.read(-- eepromaddr);
    Serial.println("List the time");
  }
  Update = ON;
}

Library I used.

I try manyๆ library and This Library is so good nah.

Credits

Another Wrold
1 project • 1 follower
Contact

Comments

Please log in or sign up to comment.