Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 2 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Hand tools and fabrication machines | ||||||
|
So, this is my first real project. The code might not be too clear and some shortcuts were done, but it works. I'm still in the learning phase.
I made this counter to store the total playing time of a vinyl player cartridge. Cartridges are expensive and, if you're not playing records regularly, estimating total playing time is difficult.
As the stylus wears slowly, the sound is also changing slowly and your ears get use to it. On all cartridges, it is not possible to replace just the stylus (which can be replaced quickly for testing), so it will be handy to know the total playing time.
This project can be used to count time for something else by replacing the photo sensor by closing contact. The display is attached on the back of a Nano with 1.5 mm thick gel tape.
Pin 3 HIGH starts the counter and LOW stops the counter. Playing time is added to total time and saved to Eeprom. Resetting Eeprom can be done with pin 4 on HIGH for 10 sec. Total time will be updated on the next start/stop of the counter.
Thingiverse: www.thingiverse.com/thing:2537637
// Counter starts by input pin(3) = HIGH
// Eeprom will be erased after input pin (4) HIGH for 10sec.
// Totaltime will be updated after next counter start/stop.
#include <EEPROM.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
#include <secTimer.h>
secTimer myTimer;
long seconds=0; //seconds
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
#define SSD1306_128_32
SSD1306AsciiAvrI2c oled;
//------------------------------------------------------------------------------
int button; // start_1
int zeroing; // Eeprom zeroing
int zeroing_bit;
int cycle;
int off; // display on/off
int flash=1; // display on bit
int cleared; // Tot nytn refresh bitti jos s alle 10
int curr_minutes;
int addrwr = 0;
int done; // Eeprom zeroing done one time, reset by button
long address = 0;
byte fourwr;
byte threewr;
byte twowr;
byte onewr;
long four;
long three;
long two;
long one;
long aikatotal;
long tothrs; //total time h
long totmin; //total time min
long totsec; //total time s
void setup() {
Serial.begin(9600);
oled.begin(&Adafruit128x32, I2C_ADDRESS);
oled.setFont(Adafruit5x7);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(13, OUTPUT);
oled.setContrast(0);
aikatotal=(EEPROMReadlong(0));
cycle=1;
off=0;
}
void loop(){
addrwr=0;
button = digitalRead(3);
zeroing = digitalRead(4);
if(zeroing == HIGH && done==0){
oled.setCursor(0, 0);
oled.setRow(1);
oled.set1X();
oled.print("Eeprom clear");
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 10s");
delay(1000);
oled.clear();
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(1);
oled.set1X();
oled.print("Eeprom clear");
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 9s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 8s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 7s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 6s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 5s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 4s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 3s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 2s");
delay(1000);
}
if (digitalRead(4)==HIGH){
oled.setCursor(0, 0);
oled.setRow(2);
oled.set1X();
oled.print("Press button 1s");
delay(1000);
}
//delay(5000);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
// zeroing_bit=digitalRead(4);
if (digitalRead(4)==HIGH){
aikatotal=0;
EEPROMWritelong(addrwr, aikatotal);
addrwr+=4;
oled.setCursor(0, 0);
oled.setRow(3);
oled.set1X();
oled.print("Done!!");
done=1;
delay(2000);
oled.clear();
}
else{
oled.setCursor(0, 0);
oled.setRow(3);
oled.set1X();
oled.print("Clear CANCELLED");
delay(3000);
oled.clear();
}
}
if (button == HIGH && cycle==0){
myTimer.startTimer();
cycle=1;
off=0;
done=0;
digitalWrite(13, HIGH);
}
if(button == LOW && cycle==1){
myTimer.stopTimer();
cycle=0;
delay(2000);
digitalWrite(13, LOW);
aikatotal=aikatotal+((curr_minutes*60)+seconds);
tothrs=aikatotal/3600;
totmin=((aikatotal-(tothrs*3600))/60);
totsec=((aikatotal-(tothrs*3600))-(totmin*60));
curr_minutes=0;
myTimer.startTimer();
myTimer.stopTimer();
flash=1;
if(off==0){
oled.setCursor(0, 0);
oled.setRow(3);
oled.set1X();
oled.print("Now Playing:");
oled.print(" ");
}
EEPROMWritelong(addrwr, aikatotal);
addrwr+=4;
if(totsec <=10 && cleared==0){
oled.clear();
cleared=1;
}
if(totsec > 1 && cleared==1){
cleared=0;
}
}
seconds=myTimer.readTimer();
if(seconds==60){
curr_minutes++;
myTimer.stopTimer();
myTimer.startTimer();
if(off==0){
oled.setCursor(0, 0);
oled.setRow(3);
oled.set1X();
oled.print("Now Playing:");
oled.print(" ");
}
}
if (off==0){
oled.setCursor(0, 0);
oled.setRow(1);
oled.set1X();
oled.print("Total:");
oled.print(tothrs);
oled.print("h ");
oled.print(totmin);
oled.print("min ");
oled.print(totsec);
oled.print("s");
oled.setCursor(0, 0);
oled.setRow(3);
oled.set1X();
oled.print("Now Playing:");
oled.print(curr_minutes);
oled.print("min ");
oled.print(seconds);
oled.print("s");
}
if(seconds==60){
oled.clear();
}
if(flash==1){
delay(5000);
flash=0;
off=1;
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
oled.clear();
}
}
void EEPROMWritelong(int addrwr, long aikatotal)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte fourwr = (aikatotal & 0xFF);
byte threewr = ((aikatotal >> 8) & 0xFF);
byte twowr = ((aikatotal >> 16) & 0xFF);
byte onewr = ((aikatotal >> 24) & 0xFF);
//Write the 4 bytes into the eeprom memory.
EEPROM.write(addrwr, fourwr);
EEPROM.write(addrwr + 1, threewr);
EEPROM.write(addrwr + 2, twowr);
EEPROM.write(addrwr + 3, onewr);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
}
long EEPROMReadlong(long address)
{
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(address);
long three = EEPROM.read(address + 1);
long two = EEPROM.read(address + 2);
long one = EEPROM.read(address + 3);
//Return the recomposed long by using bitshift.
return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
//------------------------------------------------------------------------------
Comments