0

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?

3
  • Why are you defining those variables in the header file globally? Why aren't those variables member variables in the class? Commented May 9, 2016 at 0:22
  • A) you don't need classes to multitask, B) your problem is not related to classes. Commented 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? Commented May 9, 2016 at 1:11

2 Answers 2

3

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() {....
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for such fast response. I've moved the variables to Work.cpp file. Also made changes to the definition of Update() method as @Saad_Qureshi suggested. Everything works fine now!
1

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 () {....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.