为Arduino编写库

时间:2013-03-08 22:25:39

标签: c++ c header arduino libraries

我正在尝试为Arduino编写一个简单的库来解析和解释串行命令。我的示例库的目标是读取预期的命令并打开一些LED。我已经通过arduino进行了串口通信,我想让它由一个库来处理。例如......我的arduino上有以下代码

Arduino代码:

#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

我的问题如下......

1。)我是否正确构建了库?我只是想在发送消息并触发serialEvent时LED指示灯闪烁,当我在arduino中运行代码时出现以下错误。

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

我在.cpp文件夹中名为serialComms的文件夹中有.cpp和.h文件。我不确定从哪里开始,有什么想法?

1 个答案:

答案 0 :(得分:3)

首先改变你的

#ifndef serialComms
#define serialComms

#ifndef serialComms_h
#define serialComms_h

您不能拥有与实例同名的宏。

然后检查大小写,例如readBytes vs testing.readbytes();注意B


首次确保在制作新的库目录和初始文件时关闭所有Arduino IDE。启动时IDE将缓存文件列表。他们随后可以改变内部。但是在下次开始之前不会知道新文件。


以下编辑对我来说很好。一旦我纠正了所有的拼写错误:

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);
  }   
}