//declare measure values: time, wheel diameter, and variables to store calculation results
double diameterBan = 431.8; //in mm
double diameterKiprok = 100; //in mm
double speedkBan; //store speed in km/h value
double secBan; //store second value of the interval in which the magnet does a full roation
double secKiprok; //store second value of the interval in which the magnet does a full roation
double minuteBan; //store minute value of the interval in which the magnet does a full roation
double minuteKiprok; //store minute value of the interval in which the magnet does a full roation
double rpmBan; //store rpm ban value
double rpmKiprok; //store rpm kiprok value
int speedoValue; //store rounded value of speedo
int rpmValue; //store rounded value of rpm
//declare the pins for the interrupts that the hall sensor magnet / proxymity sensor will connect
double prevHallBan = 0;
double prevHallKiprok = 1;
float pi = 3.14; //pi value
int hour = 3600; //store hour value
int stopo = 5; //declare stop button pin value
int starto = 6; //declare start button pin value
int pauseo = 7; //declare pause button pin value
int i = 0; //variable to store bool
int laptot; //store total lap value
int currlap; //store current lap value
int tpsval;
unsigned long Laps[100]; //store lap array capacity
unsigned long currentTime = 0; //store current time value
unsigned long stopTime = 0; //store stop time value
unsigned long pauseTime = 0; //store pause time value
unsigned long timer = 0; //store timer value
unsigned long milliSecondsInSecond = 1000; //store millisecond value of seconds
unsigned long milliSecondsInMinute = 60000; //store millisecond value of minutes
//boolean function for logic gates during button conditions
bool started = false;
bool paused = false;
bool stopped = false;
bool startPressed = false;
bool pausePressed = false;
bool stopPressed = false;
bool printed = false;
String condition; //store condition of timer
void setup() {
Serial.begin(9600);
attachInterrupt(0, speedCalc, RISING); //digital pin 2, void name "speedCalc", change voide during rising impulse
attachInterrupt(1, rpmCalc, RISING); //digital pin 3, void name "rpmCalc", change voide during rising impulse
pinMode(starto, INPUT_PULLUP); //attach input pullup to the digital pin
pinMode(stopo, INPUT_PULLUP); //attach input pullup to the digital pin
pinMode(pauseo, INPUT_PULLUP); //attach input pullup to the digital pin
}
void loop() {
// Get current time via millis
currentTime = millis();
// Start Button (mulai hitungan)
if(digitalRead(starto) == 0 && !startPressed && !printed){
if(!started)
started = true;
else if(started){
started = false;
Laps[i] = timer;
i++;
}
startPressed = true;
}else if(digitalRead(starto) == 1 && startPressed){
startPressed = false;
}
// Stop button
if(digitalRead(stopo) == 0 && !stopPressed && !started){
if(!stopped)
stopped = true;
else if(stopped){
stopped = false;
}
stopPressed = true;
}else if(digitalRead(stopo) == 1 && stopPressed){
stopPressed = false;
}
// if stopped
if(!started){
stopTime = millis() - pauseTime; // get elapsed time when stop
paused = false;
}else{
// Pause button
if(digitalRead(pauseo) == 0 && !pausePressed){
if(!paused)
paused = true;
else if(paused)
paused = false;
pausePressed = true;
}else if(digitalRead(pauseo) == 1 && pausePressed){
pausePressed = false;
}
}
if(!paused){ // if not paused
timer = millis() - stopTime - pauseTime; // subtract with elapsed time when stop and paused
// computeHMS(timer);
}else if(paused && started){ // paused
pauseTime = millis() - (timer + stopTime); // get elapsed time when paused
}
// Set condition string
if(started){
if(paused)
condition = "PAUSED"; //if condition is paused, display pause
else
condition = "START"; //if condition is started, display start
}else if(!started){
condition = "STOP"; //if condition is stopped, display stop
}
//if timer is stopped, display elapsed time per lap, and total time + total laps
if(stopped){
if(!printed){
totalDisplay();
}
if(stopped){
if(printed){
}
}
//display speedo value, rpm value, timer value
}else{
if(currentTime - prevHallBan < 3000){ //if hall sensor recieves impulse for less than 3 seconds, the display will show the corresponding speed value
Serial.print("\t");Serial.print(speedoValue);Serial.print(" ");
}else if(currentTime - prevHallBan>= 3000){ //if hall sensor does not receive impulse for more than 3 seconds, the display will show 0
Serial.print(" ");Serial.print("\t0");Serial.print(" ");
}
//previous time
computeHMS(Laps[i-1]);
//Timer protocol
computeHMS(timer);
Serial.print("\tTimer State: ");Serial.println(condition);
tpsval = analogRead(A0) - 131;
float eff = (tpsval/863.0 - 131.0) * 100;
int jizz = eff / 1.0;
Serial.print(jizz);
printed = false;
}
// Delay
delay(100);
}
//BELOW ARE THE NECESSARY VOID FUNCTIONS THAT ARE CALLED ABOVE
// Speedometer function
void speedCalc() {
secBan = (millis() - prevHallBan) / 1000;
prevHallBan = millis();
minuteBan = secBan/60;
rpmBan = 1/minuteBan;
speedkBan = ((pi * diameterBan * rpmBan)/1000) * 6/100;
speedoValue = round(speedkBan);
}
// RPMmeter function GANTI RUMUS NYA
void rpmCalc() {
secKiprok = (millis() - prevHallKiprok) / 1000;
prevHallKiprok = millis();
minuteKiprok = secKiprok / 60;
rpmKiprok = 1/minuteKiprok;
rpmValue = round(rpmKiprok);
}
// Compute timer
void computeHMS(unsigned long duration) {
int intMinutes;
int intSeconds;
float floatMinutes;
float floatSeconds;
intMinutes = 0;
intSeconds = 0;
if (duration >= 1000) {
floatSeconds = duration / milliSecondsInSecond % 60;
intSeconds = floatSeconds;
floatMinutes = duration / milliSecondsInMinute % 60;
intMinutes = floatMinutes;
}
if (intMinutes < 10) {
Serial.print("\t0");
}
Serial.print(intMinutes);
Serial.print(":");
if (intSeconds < 10) {
Serial.print("0");
}
Serial.print(intSeconds);
}
void totalDisplay(){ //protocol for displaying total elapsed time per lap and total time
Serial.print("-------------------------------------\n");
for(int j = 0; j < i; j++){
Serial.print("Lap"); Serial.print(j+1);Serial.print(":");
computeHMS(Laps[j]);
Serial.println(" ");
printed = true;
}
Serial.print("-------------------------------------\n");
for(int j = 0; j < i; j++){
laptot += Laps[j];
currlap = i;
}
Serial.print("Total Laps "); Serial.print(currlap);
Serial.print(": ");
computeHMS(laptot);
}