Skip to content

Instantly share code, notes, and snippets.

@andyinabox
Last active November 13, 2015 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyinabox/da115091d597e60e015b to your computer and use it in GitHub Desktop.
Save andyinabox/da115091d597e60e015b to your computer and use it in GitHub Desktop.

Separate Class File example

First I made a new file called "blinky.h" in the Arduino IDE with the "New Tab" option. You might have to save the project before doing this to make sure it gets saved in the right place.

Make a new tab in Arduino IDE

When you save the project it should have this structure:

BlinkyClass/
- BlinkyClass.pde
- blinky.h

The .h file had to use a different structure than the main file (see below). See the Arduino Library Tuturial for more info.

#ifndef Morse_h
#define Morse_h
#include "Arduino.h"
// classes should always be defined first!
class Blinky {
unsigned long t;
unsigned long curTime;
int delayTimeOn;
int ledState;
int ledPin;
public:
Blinky(int pin, int timeOn) {
ledPin = pin;
delayTimeOn = timeOn;
pinMode(ledPin, OUTPUT);
}
void upd8() {
t = millis();
if(t - curTime >= delayTimeOn) {
curTime = t;
if(ledState == LOW) {
digitalWrite(ledPin, HIGH);
ledState = HIGH;
} else {
digitalWrite(ledPin, LOW);
ledState = LOW;
}
}
}
};
#endif
#include "blinky.h"
Blinky led1(13, 1000);
Blinky led2(12, 500);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
led1.upd8();
led2.upd8();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment