Flasher Library Help

Hello, I recently tried following the steps in the "programming Arduino" book to create a custom library called "flasher" I have followed all of the steps but I am still getting an error when trying to verify the code( I will have below).

Tell me if I am wrong, but I believe the easiest way to show you my file configuration through my Github link seeing as it contains my .h files and .cpp files with an example that doest verify. GitHub - tjones9163/Flasher

and here is the error message

Arduino: 1.8.12 (Windows Store 1.8.33.0) (Windows 10), Board: "Arduino Nano, ATmega328P"

In file included from C:\Users\tjone\OneDrive\Documents\Arduino\libraries\Flasher\examples\Flasher_example1\Flasher_example1.ino:1:0:

C:\Users\tjone\OneDrive\Documents\Arduino\libraries\Flasher/Flasher.h:3:10: fatal error: WProgram.h: No such file or directory

 #include "WProgram.h"

          ^~~~~~~~~~~~

compilation terminated.

exit status 1
Error compiling for board Arduino Nano.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks

That file (WProgram.h) is not part of the Arduino environment. If you look at other libraries, the typical way to deal with this is

#if defined (ARDUINO) && ARDUINO >= 100
	#include <Arduino.h>
#else
	#include <WProgram.h>
#endif

That will make you library compatible with newer versions of the IDE

blh64:
That file (WProgram.h) is not part of the Arduino environment. If you look at other libraries, the typical way to deal with this is

#if defined (ARDUINO) && ARDUINO >= 100
#include <Arduino.h>

#else
#include <WProgram.h>
#endif



That will make you library compatible with newer versions of the IDE

Thank you for the response. Which file do i add this text to?

Really?

The issue is in your flasher.h file. Hmmmmm.

blh64:
Really?

The issue is in your flasher.h file. Hmmmmm.

Thanks again. It didn’t work when i added the code. I got the same error message. I added the code to my .h file and saved it, then updated and restarted my Arduino and it still doesn’t work.

// LED flashing library


#if defined (ARDUINO) && ARDUINO >= 100
	#include <Arduino.h>
#else
	#include <WProgram.h>
#endif


class Flasher
{
public:
Flasher(int pin, int duration);
void  flash(int times);
private:
int_pin;
int_d;
};

You did not post the error message you received.

And no, you did not get the same error message. I downloaded your code, including the example and your new error message is in Flasher.cpp since that file also does a unilateral include of WProgram.h. It should not even be including that. It should only include Flasher.h.

Your code (Flasher.cpp) and your example both contain typo errors.

Flasher.h

// LED flashing library

#ifndef Flasher_h
#define Flasher_h


#if defined (ARDUINO) && ARDUINO >= 100
	#include <Arduino.h>
#else
	#include <WProgram.h>
#endif

class Flasher
{
public:
Flasher(int pin, int duration);
void  flash(int times);
private:
int _pin;
int _d;
};

#endif

Flasher.cpp

//#include "WProgram.h"
#include "Flasher.h"
Flasher::Flasher(int pin, int duration)
{
  pinMode(pin,OUTPUT);
  _pin=pin;
  _d=duration/2;
}

void Flasher::flash(int times)
{
  for (int i=0; i < times; i++)
  {
    digitalWrite(_pin,HIGH);
    delay(_d);
    digitalWrite(_pin,LOW);
    delay(_d);
  }
}

And your example

#include <Flasher.h>

int ledPin=13;
int slowDuration=300;
int fastDuration=100;
Flasher slowFlasher(ledPin, slowDuration);
Flasher fastFlasher(ledPin,fastDuration);
void setup(){}
void loop()
{
  slowFlasher.flash(5);
  delay(1000);
  fastFlasher.flash(10);
  delay(2000);
}

blh64:
You did not post the error message you received.

And no, you did not get the same error message. I downloaded your code, including the example and your new error message is in Flasher.cpp since that file also does a unilateral include of WProgram.h. It should not even be including that. It should only include Flasher.h.

Your code (Flasher.cpp) and your example both contain typo errors.

Flasher.h

// LED flashing library

#ifndef Flasher_h
#define Flasher_h

#if defined (ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

class Flasher
{
public:
Flasher(int pin, int duration);
void  flash(int times);
private:
int _pin;
int _d;
};

#endif



Flasher.cpp


//#include “WProgram.h”
#include “Flasher.h”
Flasher::Flasher(int pin, int duration)
{
  pinMode(pin,OUTPUT);
  _pin=pin;
  _d=duration/2;
}

void Flasher::flash(int times)
{
  for (int i=0; i < times; i++)
  {
    digitalWrite(_pin,HIGH);
    delay(_d);
    digitalWrite(_pin,LOW);
    delay(_d);
  }
}



And your example


#include <Flasher.h>

int ledPin=13;
int slowDuration=300;
int fastDuration=100;
Flasher slowFlasher(ledPin, slowDuration);
Flasher fastFlasher(ledPin,fastDuration);
void setup(){}
void loop()
{
  slowFlasher.flash(5);
  delay(1000);
  fastFlasher.flash(10);
  delay(2000);
}

You are right, I didn’t even notice. Thank you for taking the time to check and do that.
Everything works fine now. I tried google but it does not give me a simple answer.
Can you explain simply what the header(.h) and .cpp file do and how they are different?
Thanks again, mate.

tjones9163:
Can you explain simply what the header(.h) and .cpp file do and how they are different?

The header is meant to be a sort of API listing that shows what functions/classes/constants/globals are available for use. The cpp is for actually defining the classes/functions as listed in the header.