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.

Revisions

  1. andyinabox revised this gist Nov 13, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion blinky.h
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,6 @@ class Blinky {
    int delayTimeOn;
    int ledState;
    int ledPin;
    bool onMode;

    public:
    Blinky(int pin, int timeOn) {
  2. @daytona1 daytona1 revised this gist Nov 13, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion blinky.h
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ class Blinky {
    unsigned long t;
    unsigned long curTime;
    int delayTimeOn;
    int delayTimeOff;
    int ledState;
    int ledPin;
    bool onMode;
  3. @daytona1 daytona1 revised this gist Nov 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Separate Class File example

    First I made a new file called "blinky.h" in the Arduino IDE.
    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](https://dl.dropboxusercontent.com/u/2100102/sfpc/new_tab.png)

  4. @daytona1 daytona1 created this gist Nov 13, 2015.
    16 changes: 16 additions & 0 deletions BlinkyClass.pde
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    #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();
    }
    15 changes: 15 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # Separate Class File example

    First I made a new file called "blinky.h" in the Arduino IDE.

    ![Make a new tab in Arduino IDE](https://dl.dropboxusercontent.com/u/2100102/sfpc/new_tab.png)

    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](https://www.arduino.cc/en/Hacking/LibraryTutorial) for more info.
    45 changes: 45 additions & 0 deletions blinky.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #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