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?
-
Why are you defining those variables in the header file globally? Why aren't those variables member variables in the class?Some programmer dude– Some programmer dude2016年05月09日 00:22:06 +00:00Commented May 9, 2016 at 0:22
-
A) you don't need classes to multitask, B) your problem is not related to classes.Stack Exchange Broke The Law– Stack Exchange Broke The Law2016年05月09日 00:35:11 +00:00Commented May 9, 2016 at 0:35
-
A) as far as I've read, I do need classes to multitask properly. Check the article I've linked. B) How is it not related to classes if I'm writing a C++ class for Arduino?Justin– Justin2016年05月09日 01:11:14 +00:00Commented May 9, 2016 at 1:11
2 Answers 2
That error means you have those variables appearing in multiple translation units. By default, non-const
global variables have external linkage.
To make it internal, you can qualify it with static
. Or you can simply move the definitions to your translation unit (.cpp file)
Qualify with static
in Work.h
static int ledPin; // the number of the LED pin
static unsigned long OnTime; // milliseconds of on-time
static unsigned long OffTime; // milliseconds of off-time
static int ledState; // ledState used to set the LED
static unsigned long previousMillis; // will store last time LED was updated
class Work {
public:
Work(int pin, long on, long off);
void Update();
};
Preferably, move them to your .cpp files since you aren't using them in your Work.h file.
EDIT
The error resulted from your Update()
function in the .cpp file.
void Update() { ....
^^ that defined a new function, and is using those global variables... I believe you wanted to do
void Work::Update() {....
1 Comment
Since the method Update() is a part of class Work, it should be defined within the scope of Work class like this:
void Work :: Update () {....