This means (to my somewhat rough understanding) that your function is created at the same time the class (which you are trying to pass to it) is.
Error, Will Robinson.
Does not compute.
The object you are declaring does not exist.
The good news: there's a way to get around this. After digging around in the forums, I found you needed to create a library. For those of you who don't know C libraries, they are containers for classes and other goodies, and an easy way to keep your code modular and clean. They consist, in c++ of a head file containing the initial class declaration, and a .cpp file containing methods and other such goodies. All you need to do is add a reference in your file.
While I found the linked article very informative, I had some difficulty figuring out how to create the files themselves. I must admit that in my professional life I've been pampered by Visual Studio and its ability to handle libraries and other components for you. How does one create and manage libraries manually?
After doing some digging, I found this blog that pointed me in the correct direction. Here's how I did it:
- Turn off the Arduino IDE (not 100% this is necessary, but it won't recognize your class until it restarts.)
- Went into file explorer
- Navigated to the program file for Arduino (in my case C:\Program Files (x86)\Arduino)
- Open the libraries folder
- In the libraries folder, create a new folder (name it for your class (in my case LED))
- Open your class folder and create two new notepad files.
- Name one [YourClassName].h and the other [YourClassName].cpp (where [YourClassName] is the same name as you folder.)
- Open the .h file. Add the lines:
#ifndef [YourClassName]_h //If this header has not been defined in this scope...
#define [YourClassName]_h // Define it
#include <Arduino.h> // Reference the arduino program core libraries (not 100% this has to be here, but hey)
#include <WProgram.h> - Paste in your class declaration. This will be everything EXCEPT the external methods and members outside the class declaration brackets.
- Cap it off with an #endif
- Save and close the .h file.
- Open the .cpp file. Add the following lines:
#include<[YourClassName].h> // References your header files
#include <Arduino.h> // Reference the arduino core libraries
#include <WProgram.h> - Paste in the remainder of your code.
- Reopen the IDE and open your sketch.
- Remove the class declaration and any methods/members.
- Go to Sketch >> Import Library >> [Your Class Name]
- Done!
NOTES/TROUBLESHOOTING:
- Remember to replace any instances of [YourClassName] with the name of your class. This needs to be the same between your folder name, cpp and h file. I would recommend keeping everything down to the capitalization the same!
- I'm not saying that this is the optimal, expert-prescribed, or only, way to go about this. This is just what worked for me.
- If your text editor adds .txt to everything, trim it off the end. If you have Hide Extensions for known File Types checked under your view options, you may not see the .txt suffix, but it will appear as a notepad icon and NOT process as a library.
My code follows.
Led.h:
#ifndef LED_h
#define LED_h
#include <Arduino.h>
#include <WProgram.h>
/*
LED.h - Library for standard and RGB LED's
Beau Bureau, 2014
Released into the public domain.
*/
class Led {
private:
int red, green, blue, rPin, gPin, bPin;
bool isValidRGB(int);
public:
Led() {};
Led(int);
Led(int,int,int);
int Blue() {return blue;}
int Green() {return green;}
int Red() {return red;}
//RPin is also the power pin.
int RPin() {return rPin;}
int GPin() {return gPin;}
int BPin() {return bPin;}
int Power() {return rPin;}
// checks whether the bPin has been set to -1. If it is set to -1 it is not an RGB LED.
bool IsRGB() {return bPin!=-1;}
void SetRed(int);
void SetGreen(int);
void SetBlue(int);
void SetRandomColor();
};
#endif
//
Led.cpp:
#include<LED.h>
#include <Arduino.h>
#include <WProgram.h>
Led::Led(int p) {
//Initiates a standard LED. the rPin is power.
rPin=p;
// Either g or b can bee tested against to see if this is an RGB. They will be equal to -1 if a standard RGB.
gPin=-1;
bPin=-1;
red=0;
green=0;
blue=0;
}
Led::Led(int r,int g,int b) {
// Initiates an RGB LED, sets color to white.
rPin=r;
gPin=g;
bPin=b;
red=255;
green=255;
blue=255;
}
bool Led::isValidRGB(int val)
{
if (val>-1&&val<256)
{
return true;
}
else {
return false;
}
}
void Led::SetRandomColor() {
// Randomize, choose a color of 0-6. 0=red, 1=green, 2=blue, 3=purple, 4=yellow, 5=orange
int color=random(0,5);
switch (color) {
case 0:
red=255;
green=0;
blue=0;
break;
case 1:
green=255;
red=0;
blue=0;
break;
case 2:
blue=255;
red=0;
blue=0;
break;
case 3:
red=255;
blue=255;
green=0;
break;
case 4:
red=255;
green=255;
blue=0;
break;
default:
red=255;
green=128;
blue=0;
break;
}
}
void Led::SetRed(int val) {
if (isValidRGB(val))
{
red=val;
}
}
void Led::SetBlue(int val) {
if (isValidRGB(val))
{
blue=val;
}
}
void Led::SetGreen(int val) {
if (isValidRGB(val))
{
green=val;
}
}
There we go! Hope this saves you some hours of hair-pulling frustration.
No comments:
Post a Comment