C++ 为Arduino编写库

C++ 为Arduino编写库,c++,c,header,arduino,libraries,C++,C,Header,Arduino,Libraries,我正试图为Arduino编写一个简单的库来解析和解释串行命令。我使用示例库的目标是读取预期的命令并打开一些指示灯。我已经通过arduino进行了串行通信,我希望它能被图书馆处理。例如,我的arduino上有以下代码 Arduino代码: #include <serialComms.h> serialComms testing = serialComms(); void setup() { Serial.begin(9600); } void loop() // not terr

我正试图为Arduino编写一个简单的库来解析和解释串行命令。我使用示例库的目标是读取预期的命令并打开一些指示灯。我已经通过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();
}
我的问题如下

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
我在libraries文件夹中名为serialComms的文件夹中有.cpp和.h文件。我真的不知道从这里走到哪里,有什么想法吗?

首先改变你的想法

#ifndef serialComms
#define serialComms

不能有与实例同名的宏

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


第一次创建新的库目录和其中包含的初始文件时,请确保关闭所有Arduino IDE。IDE在启动时缓存文件列表。他们可以随后改变这些内部。但是新文件在下一次启动之前是未知的


以下内容对我来说很好。一旦我纠正了所有的打字错误:

定义测试

#include <serialComms.h>
serialComms testing;

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

void loop() {
}

void serialEvent()
{
  testing.readBytes();
  testing.assignBytes();
}
串行通信

#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
#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);
  }   
}
#包括
#包括
void serialComms::init()
{
//这就是构造器的位置…现在我们太蠢了,没有构造器
}
void serialComms::readBytes()//目标引脚,值
{
数字写入(11,高);
延迟(250);
数字写入(11,低);
赋值字节();
}
void serialComms::assignBytes()
{

对于(int t=0;t更改了这些,得到了以下错误'testingLibraries:2:错误:'serialComms'未在函数'void serialEvent()中命名类型'testingLibraries.ino:':''testingLibraries:16:错误:“testing”未在此范围内声明“这非常有效,谢谢,我现在在尝试传递数组时遇到问题,是否需要使用指针来完成此操作?我不确定是的,示例void foo(int*bar);请参阅您的
#include <serialComms.h>
serialComms testing;

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

void loop() {
}

void serialEvent()
{
  testing.readBytes();
  testing.assignBytes();
}
#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
#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);
  }   
}