I've followed along with this Library Tutorial, and I thought I organized my library in the exact same manner, but I keep getting error messages.
Errors:
Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"
C:\Users\abeth\Documents\Arduino\sketch_TimerLibraryTest\sketch_TimerLibraryTest.ino: In function 'void setup()':
sketch_TimerLibraryTest:7:9: error: request for member 'set' in 'timer', which is of non-class type 'Timer()'
timer.set();
^~~
C:\Users\abeth\Documents\Arduino\sketch_TimerLibraryTest\sketch_TimerLibraryTest.ino: In function 'void loop()':
sketch_TimerLibraryTest:11:9: error: request for member 'start' in 'timer', which is of non-class type 'Timer()'
timer.start();
^~~~~
sketch_TimerLibraryTest:12:9: error: request for member 'print' in 'timer', which is of non-class type 'Timer()'
timer.print();
^~~~~
exit status 1
request for member 'set' in 'timer', which is of non-class type 'Timer()'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
.ino:
#include <Timer.h>
Timer timer();
void setup() {
Serial.begin(9600);
timer.set();
}
void loop() {
timer.start();
timer.print();
}
.cpp:
#include "Arduino.h"
#include "Timer.h"
Timer::Timer(){
printInterval = 1000;
previousTime = 0;
}
void Timer::set(){
secondsTime = 0;
minutesTime = 0;
hoursTime = 0;
}
void Timer::set(int sec){
secondsTime = sec;
minutesTime = 0;
hoursTime = 0;
}
void Timer::set(int sec, int mins){
secondsTime = sec;
minutesTime = mins;
hoursTime = 0;
}
void Timer::set(int sec, int mins, long hrs){
secondsTime = sec;
minutesTime = mins;
hoursTime = hrs;
}
void Timer::start(){
unsigned long currentTime = millis();
if(currentTime - previousTime >= printInterval){
previousTime = currentTime;
secondsTime = secondsTime + 1;
if(secondsTime >= 60){
secondsTime = secondsTime - 60;
minutesTime = minutesTime + 1;
}
if(minutesTime >= 60){
minutesTime = minutesTime - 60;
hoursTime = hoursTime + 1;
}
}
void Timer::print(){
Serial.print(hoursTime);
Serial.print(":");
if(minutesTime < 10){
Serial.print(0);
Serial.print(minutesTime);
}else{
Serial.print(minutesTime);
}
Serial.print(":");
if(secondsTime < 10){
Serial.print(0);
Serial.print(secondsTime);
}else {
Serial.print(secondsTime);
}
}
void Timer::reset(){
secondsTime = 0;
minutesTime = 0;
hoursTime = 0;
}
void Timer::count(unsigned long interval){
intervalOther = interval;
unsigned long currentOther = millis();
unsigned long previousOther = 0;
if(currentOther - previousOther >= intervalOther){
previousOther = currentOther;
countState = true;
}else{
countState = false;
}
}
.h:
#ifndef Timer_h
#define Timer_h
#include "Arduino.h"
class Timer
{
public:
void set();
void set(int sec);
void set(int sec, int mins);
void set(int sec, int mins, long hrs);
void start();
void print();
void reset();
void count(unsigned long interval);
bool countState;
private:
long printInterval;
unsigned long previousTime;
int secondsTime;
int minutesTime;
int hoursTime;
unsigned long currentTime;
};
#endif