Moving functions into separate cpp file


hello,

i have 1 sketch works fine. want outsource functions separate cpp files. have followed far:
http://arduino.land/faq/content/7/43/en/breaking-a-sketch-into-multiple-files.html

but reason not compile. got error: leds not declared in scope.

this sketch, works fine:

code: [select]
#include "fastled.h"

#define num_leds 64

crgb leds[num_leds];

#define data_pin 11
#define clock_pin 12

#define max_brightness 12 // (0...255)

void setup()
{
  fastled.addleds<apa102, data_pin, clock_pin, rgb>(leds, num_leds);
}

void loop()
{
  one(crgb::green);
  delay(2000);
  two(crgb::green);
  delay(2000);
}

void one(uint32_t color)
{
  fastled.clear();
  fastled.setbrightness(10);
  leds[2] = color;
  leds[13] = color;
  leds[14] = color;
  leds[16] = color;
  leds[18] = color;
  leds[29] = color;
  leds[34] = color;
  leds[45] = color;
  leds[48] = color;
  leds[49] = color;
  leds[50] = color;
  leds[51] = color;
  leds[52] = color;
  fastled.show();

  return;
}

void two(uint32_t color)
{
  fastled.clear();
  fastled.setbrightness(10);
  leds[1] = color;
  leds[2] = color;
  leds[3] = color;
  leds[11] = color;
  leds[20] = color;
  leds[28] = color;
  leds[34] = color;
  leds[46] = color;
  leds[48] = color;
  leds[49] = color;
  leds[50] = color;
  leds[51] = color;
  leds[52] = color;
  fastled.show();

  return;
}


and here code after modification:

for sketch:

code: [select]

#include "fastled.h"
#include "alphabet.h"

#define num_leds 64

crgb leds[num_leds];

#define data_pin 11
#define clock_pin 12

#define max_brightness 12 // (0...255)

void setup()
{
  fastled.addleds<apa102, data_pin, clock_pin, rgb>(leds, num_leds);
}

void loop()
{
  one(crgb::green);
  delay(2000);
  two(crgb::green);
  delay(2000);
}


for cpp file:

code: [select]

#include "fastled.h"
#include "arduino.h"


void one(uint32_t color)
{
  fastled.clear();
  fastled.setbrightness(10);
  leds[1] = color;
  leds[2] = color;
  leds[3] = color;
  leds[11] = color;
  leds[20] = color;
  leds[28] = color;
  leds[34] = color;
  leds[46] = color;
  leds[48] = color;
  leds[49] = color;
  leds[50] = color;
  leds[51] = color;
  leds[52] = color;
  fastled.show();

  return;
}

void two(uint32_t color)
{
  fastled.clear();
  fastled.setbrightness(10);
  leds[1] = color;
  leds[2] = color;
  leds[3] = color;
  leds[11] = color;
  leds[20] = color;
  leds[28] = color;
  leds[34] = color;
  leds[46] = color;
  leds[48] = color;
  leds[49] = color;
  leds[50] = color;
  leds[51] = color;
  leds[52] = color;
  fastled.show();

  return;
}


for header file:

code: [select]

#ifndef alphabet_h
#define alphabet_h

void one(uint32_t color);
void two(uint32_t color);

#endif


i think clear want archive, want code displaying letters , numbers not in main sketch large.

quote
i want code displaying letters , numbers not in main sketch large.
what current code create library.  can helpful if want use again in other programs.

if, however, want code split smaller, more manageable sections need add 1 or tabs in ide, give them filename .ino extension , put code in there.  files created th same folder main program , compiled it.

as current problem, leds[] array declared , full error message ?


Arduino Forum > Using Arduino > Programming Questions > Moving functions into separate cpp file


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???