我写了一些代码来使用Arduino Uno板驱动外部7段显示。当在Arduino IDE中作为.ino文件运行时,它工作得很好。然后我决定将它变成一个库以供将来使用。我创建了我的.h和.cpp文件,但是当我尝试使用之前工作的相同函数为.ino时,它们现在无法工作。这是我的代码:
.ino
#include <Segment.h>
Segment seg;
void setup() {
}
void loop() {
seg.display(1);
}
·H
#ifndef Segment_h
#define Segment_h
#include "Arduino.h"
class Segment
{
public:
Segment();
void display(int i);
private:
int a;
int b;
int c;
int d;
int e;
int f;
int g;
int dot;
};
#endif
的.cpp
#include "Segment.h"
const int a=6;
const int b=7;
const int c=8;
const int d=10;
const int e=11;
const int f=13;
const int g=12;
const int dot=9;
Segment::Segment()
{
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(dot, OUTPUT);
}
void Segment::display(int i)
{
if (i==0){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
digitalWrite(dot, HIGH);
}
else if (i==1){
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dot, HIGH);
}
else if (i==2){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==3){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g,LOW);
digitalWrite(dot, HIGH);
}
else if (i==4){
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==5){
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==6){
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==7){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dot, HIGH);
}
else if (i==8){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==9){
digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
digitalWrite(dot, HIGH);
}
else if (i==10){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dot, LOW);
}
else if (i==11){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
digitalWrite(dot, HIGH);
}
}
编译器没有错误,但SSD只是空白。我在名为int test()
的.cpp文件中运行了一个测试函数,它只返回一个任意数字,并且运行正常。这让我觉得包含正在按预期工作,并且所有三个文件都在进行通信。我认为由于某种原因,由于某种原因,它无法识别#include "Arduino.h"
,这会导致digitalWrite()
和pinMode
函数被删除。任何人都可以为我阐明这一点吗?感谢。
答案 0 :(得分:1)
正如Brian在他的评论中指出的那样,你将Segment类中定义的变量a,b,c,d,e,f,g和dot作为私有成员,并将它们重新定义为全局常量。在Segments成员函数中调用pinMode()和digitalWrite()时,函数使用尚未初始化的类成员变量,而不是您初始化的全局变量。
在Segment构造函数中,在调用pinMode()之前,初始化成员变量。