Skip to content

Instantly share code, notes, and snippets.

@andyinabox
Last active November 13, 2015 20:40
Show Gist options
  • Select an option

  • Save andyinabox/da115091d597e60e015b to your computer and use it in GitHub Desktop.

Select an option

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.

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 delayTimeOff;
int ledState;
int ledPin;
bool onMode;
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