[SOLVED] Creating a cpp set of function and callin another *.h file.

I want to populate a NeoFunctions.cpp file with a lot different functions the call them frrom the main ino file.

Please throw me a bone.

OS Linux

Error:

/home/cwc/Arduino/libraries/NeoFunctions/NeoFunctions.cpp: In member function 'void NeoFunctions::cwc()':
/home/cwc/Arduino/libraries/NeoFunctions/NeoFunctions.cpp:13:6: error: 'strip' was not declared in this scope
strip.setPixelColor(i, strip.Color(0,0,0,w ) );
^
/home/cwc/Arduino/libraries/NeoFunctions/NeoFunctions.cpp:15:3: error: 'strip' was not declared in this scope
strip.show();
^
exit status 1
Error compiling for board Arduino/Genuino Uno.

Arduino (neo.ino)

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#include <NeoFunctions.h>
#define PIN 6
#define NUM_LEDS 8
#define BRIGHTNESS 50

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);


void setup() {
  Serial.begin(115200);
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

//set up variables
int w = 0;
int i; 

void loop() {
  Call cwc function in NeoFunctions.cpp file
}

Header

#ifndef NeoFunctions_h
#define NeoFunctions_h

#include "Arduino.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
class NeoFunctions
{
  public:
	NeoFunctions();
    void cwc();
    void clear();

};

#endif

NeoFunctions.cpp file

#include "Arduino.h"
#include "NeoFunctions.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
NeoFunctions::NeoFunctions()
{}

void NeoFunctions::cwc()
	{	int i; int w =0;
		for(i = 0; i < 16 ; i++){
			  strip.setPixelColor(i, strip.Color(0,0,0,w ) );     
		}
	 strip.show();
	 delay(30);
	  w++;
	  if  (w > 255) w = 0;
	} 

void NeoFunctions::clear()
{
// code to come
}

In your .h file you need to initialize the strip class with the extern preface. Then initialize the class in your .cpp. Once you do this, you'll be able to use the strip class.

Or you could pass the strip class as a parameter.

Obviously, your NeoFunctions.cpp has no idea what strip is.

There are many different ways to go about it:

  1. Pass a reference to strip to member functions of NeoFunctions class as a Adafruit_NeoPixel &strip parameter.

  2. Make Adafruit_NeoPixel &strip a member of your NeoFunctions class and initialize it in the constructor.

  3. Use it as a global variable, which means adding

extern Adafruit_NeoPixel strip;

somewhere at the beginning of your NeoFunctions.cpp.

Interfacing through global variables is bad programming practice though.


Why did you write this

NeoFunctions::NeoFunctions()
{}

What purpose does it serve?


Also, it is not clear why you decided to organize your functions into a class. What exactly does that class represent? Why not a bunch of regular functions in NeoFunctions.cpp?

Montmorency:
Obviously, your NeoFunctions.cpp has no idea what strip is.

There are many different ways to go about it:

  1. Pass a reference to strip to member functions of NeoFunctions class as a Adafruit_NeoPixel &strip parameter.

  2. Make Adafruit_NeoPixel &strip a member of your NeoFunctions class and initialize it in the constructor.

  3. Use it as a global variable, which means adding

extern Adafruit_NeoPixel strip;

somewhere at the beginning of your NeoFunctions.cpp.

Interfacing through global variables is bad programming practice though.


Why did you write this

NeoFunctions::NeoFunctions()

{}




What purpose does it serve?



---


Also, it is not clear why you decided to organize your functions into a class. What exactly does that class represent? Why not a bunch of regular functions in `NeoFunctions.cpp`?

I am trying to learn how to build libraries. I know I can get the code to work . But I really want to learn how to create *.h and *.cpp file.

I followed the following guide.

https://www.arduino.cc/en/Hacking/LibraryTutorial

I used a class because I was following a guide:

This compiles except for pixels.cwc();

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#include <NeoFunctions.h>

NeoFunctions pixels();

void setup() {
  Serial.begin(115200);
}

//set up variables
int w = 0;
int i; 

void loop() {
 pixels.cwc();
}

cpp

#include "Arduino.h"
#include "NeoFunctions.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN 6
#define NUM_LEDS 8
#define BRIGHTNESS 50

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
//NeoFunctions::NeoFunctions(){}

void NeoFunctions::cwc()
	{	int i; int w =0;
		for(i = 0; i < 16 ; i++){
			  strip.setPixelColor(i, strip.Color(0,0,0,w ) );     
		}
	 strip.show();
	 delay(30);
	  w++;
	  if  (w > 255) w = 0;
	} 

void NeoFunctions::clear()
{
  digitalWrite(6, HIGH);
  delay(1000);
  digitalWrite(6, LOW);
  delay(250);
}

1885:
I followed the following guide.

https://www.arduino.cc/en/Hacking/LibraryTutorial

Well, in that tutorial they begin with an idea: to implement a class that will output Morse code through a pin. It is a nice and a very well rounded idea. And using a class is very appropriate: the class will store the target pin number and all class methods will work through that stored pin number. If necessary, one can create many objects of that class to independently output many Morse code streams through several different pins.

Now, what is your idea for a class? What will that class do? That is what you have to begin with.

And will your class hold any data internally (like data fields)? If not, then it is quite possible that there's no point of even having a class.

1885:
This compiles except for pixels.cwc();

It is not clear what you were trying to achieve there. In any case this

NeoFunctions pixels();

does not make any sense at all. This is a function declaration, not an object declaration.

Montmorency,

Thank you again. What we plan on doing in writing 30 different functions that light a a 8x8 neo pixel.
We want to put the functions in an external file rather than in the main ino file.
We really want to learn how to make a .h and .cpp file that can be used by the ino file.

I'm not sure how to call the functions I create in the NeoFunctions.cpp file if I do not have it in a class.

This is a programming assignment and I am the instructor (always a student)

This is a better example of what the actual functions will look like:

Rather than a cout the we will light up a pixel on an 8x8 neopixel. Every student need a different design.

int cwc(){
		int r,c;//row and column
		int red, green, blue;
		int val = 0;
		int m[8][8] = {
			{5,5,5,5,5,5,5,5} ,
			{5,4,0,0,0,0,4,5} ,
			{5,0,5,5,5,5,0,5} ,
			{5,0,5,1,1,5,0,5} ,
			{5,0,5,1,1,5,0,5} ,
			{5,0,5,5,5,5,0,5} ,
			{5,4,0,0,0,0,4,5},
			{5,5,5,5,5,5,5,5}
		};

			red = 0; green = 0; blue = 0;
		  for (r = 0; r < 8; r++){
				for (c = 0; c < 8; c++){
				  val = m[r][c];
					switch (val){
						case 0:
							red = 0; green = 0; blue = 0;
							break;
						case 1:
							red = 255; green = 255; blue = 0;
							break;
						case 2: // green
							red = 55; green = 200; blue = 20;
							break;
						case 3:
							red = 255; green = 255; blue = 0;
							break;
						default:
							red = 255; green = 255; blue = 0;
						 break;
					 }//end select
					 	//output the colors
						std::cout<<"["<<red<<" "<<green<<" "<<blue<<"]";
					}//end c
					std::cout<<"\n";
		}//end r
	return 0;
}//end cwc() function

SOLVED using c functons and help from another post.
//Moving Functions to External Libraries - Programming Questions - Arduino Forum

// * * * * * * * * * * * * *
// NewCallFunctions
// * * * * * * * * * * * * *
#include <Adafruit_NeoPixel.h>
#include <neopixel_functions.h>

#define PIN 6
#define NUM_LEDS 16
#define BRIGHTNESS 50


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN,NEO_GRBW + NEO_KHZ800);
void cwc(Adafruit_NeoPixel & pixels);

void setup() {
  pixels.begin();
  pixels.setBrightness(15); // 1/3 brightness
}

void loop() {
  cwc(pixels);
}
// * * * * * * * * * * * * *
//neopixel_functions.h 
// * * * * * * * * * * * * *
#ifndef neopixel_functions
#define neopixel_functions

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>

void cwc(Adafruit_NeoPixel &pixels);

#endif
//https://forum.arduino.cc/index.php?topic=199423.0
// * * * * * * * * * * * * *
//neopixel_functions.cpp
// * * * * * * * * * * * * *
#include "Arduino.h"
#include "Adafruit_NeoPixel.h"
#include <neopixel_functions.h>

void cwc(Adafruit_NeoPixel &pixels) {  // test routine
   int w = 1;
	pixels.setBrightness(20); // 1/3 brightness  pas 
	for(int i = 0; i < 16 ; i++){
          pixels.setPixelColor(i, pixels.Color(w,100,0,w) );
          w = w + 5;
  }   
   pixels.show();
}