Bubo
Bubo

Reputation: 808

Writing Libraries for Arduino

I am trying to write a simple library for an Arduino to parse and interpret serial commands. my goal with the example library is to read the expected command and turn on some LEDs. I have gotten serial communications to work via the arduino, and I want it to be handled by a library. For Example...I have the following code on my arduino

Arduino Code:

#include <serialComms.h>
serialComms testing = serialComms();
void setup()
{
 Serial.begin(9600); 
}

void loop() // not terribly concerned with the main loop, only the serialEvent, which I        have tested and works
{

}


void serialEvent()
{
   testing.readNewBytes();
   testing.assignBytes();
}

serialComms.cpp

#include <Arduino.h>
#include <serialComms.h>

void serialComms::init()
{
  // This is where the constructor would be...right now we are too stupid to have one
}

void serialComms::readNewBytes()  // Target Pin,Values
{
        digitalWrite(11,HIGH);
        delay(250);
        digitalWrite(11,LOW);
        assignBytes();

}

void serialComms::assignBytes()
{
    for(int t  = 0;t<5;t++)
    {
        digitalWrite(10,HIGH);
        delay(250);
        digitalWrite(10,LOW);
    }   
   }

serialComms.h

#ifndef serialComms_h
#define serialComms_h



/* serialComms Class */
class serialComms
{
  public:
    serialComms() {};
    void init();
    void readNewBytes(); // Will be used to create the array --> two variables for now...
    void assignBytes();
    };

#endif

My questions are as follows...

1.) Do I have the libraries structured properly? I just want the LEDs to blink when I send a message and trigger the serialEvent, When I run the code in arduino I get the following errors.

testingLibraries:2: error: 'serialComms' does not name a type
testingLibraries.ino: In function 'void serialEvent()':
testingLibraries:16: error: 'testing' was not declared in this scope

I have the .cpp and .h file in a folder named serialComms in the libraries folder. I am not really sure where to go from here, any thoughts?

Upvotes: 2

Views: 7369

Answers (1)

mpflaga
mpflaga

Reputation: 2827

First change your

#ifndef serialComms
#define serialComms

to

#ifndef serialComms_h
#define serialComms_h

You can not have a macro with the same name as an instance.

Then check you capitalization e.g. readBytes vs testing.readbytes(); notice the B


Make SURE you close out all your Arduino IDE's when making a NEW library directory and initial files with in it, for the first time. The IDE on startup caches a list of the files. They can subsequently change there insides. but new files will not be known until the next start.


The following compiles fine for me. Once I corrected all the typo's:

definetest.ino

#include <serialComms.h>
serialComms testing;

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

void loop() {
}

void serialEvent()
{
  testing.readBytes();
  testing.assignBytes();
}

serialComms.cpp

#ifndef serialComms_h
#define serialComms_h

/* serialComms Class */
class serialComms
{
  public:
//       serialComms() {};
void init();
void readBytes(); // Will be used to create the array --> two variables for now...
void assignBytes();
    };

#endif

serialComms.h

#include <Arduino.h>
#include <serialComms.h>

void serialComms::init()
{
  // This is where the constructor would be...right now we are too stupid to have one
}

void serialComms::readBytes()  // Target Pin,Values
{
  digitalWrite(11,HIGH);
  delay(250);
  digitalWrite(11,LOW);
  assignBytes();
}

void serialComms::assignBytes()
{
  for(int t  = 0;t<5;t++)
  {
    digitalWrite(10,HIGH);
    delay(250);
    digitalWrite(10,LOW);
  }   
}

Upvotes: 3

Related Questions