I need to use classes in Arduino to be able to multitask. I've found a class example in here but I wanted to keep my main code clean, so I've decided to put the class in .h and .cpp files. After a bit of googling this is what I've came up with: Work.h file:
/*
* Work.h
*
* Created on: 2016-05-09
* Author: Secret
*/
#ifndef WORK_H_
#define WORK_H_
int ledPin; // the number of the LED pin
unsigned long OnTime; // milliseconds of on-time
unsigned long OffTime; // milliseconds of off-time
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
class Work {
public:
Work(int pin, long on, long off);
void Update();
};
#endif /* WORK_H_ */
Work.cpp file:
/*
* Work.cpp
*
* Created on: 2016-05-09
* Author: Secret
*/
#include "Work.h"
#include <Arduino.h>
Work::Work(int pin, long on, long off) {
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update() {
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if ((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) {
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
} else if ((ledState == LOW)
&& (currentMillis - previousMillis >= OffTime)) {
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
When I tried compiling, I got errors that in Work.cpp, method Update() I've got multiple definitions of OnTime, OffTime, ledState, previousMillis.
What am I doing wrong here and how to solve this?