我编写了一个小程序,但是在编译时,如果我使用#include "Temperature.h"
和#include "Lcd.h"
进行编译,则会收到一条奇怪的错误消息。
但是,如果我删除一个代码,则我的代码可以正常工作,但是如果我两个都删除,则不能。
我知道oneWire
和TemperatureSensors
被多次定义,但是在我的Lcd.cpp中我不使用它!
sketch/Temperature.cpp.o (symbol from plugin): In function 'oneWire':
(.text+0x0): multiple definition of 'oneWire'
sketch/Lcd.cpp.cpp.o (symbol from plugin):(.text+0x0): first defined here
sketch/Temperature.cpp.o (symbol from plugin): In function 'oneWire':
(.text+0x0): multiple definition of 'TemperatureSensors'
sketch/Lcd.cpp.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
[Error] Exit with code=1
.ino
#include "Temperature.h"
#include "Lcd.h"
#define UP_KEY 13
#define OK_KEY 12
#define DOWN_KEY 11
#define LED_GREEN 2
#define LED_RED 3
void setup() {
Serial.begin(9600);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(UP_KEY, INPUT_PULLUP);
pinMode(OK_KEY, INPUT_PULLUP);
pinMode(DOWN_KEY, INPUT_PULLUP);
initSensorTemperature();
numberOfTemperatureSensor();
initLcd();
}
void loop(){
digitalWrite(LED_GREEN, HIGH);
delay(1000);
digitalWrite(LED_GREEN, LOW);
delay(1000);
getTemperature();
}
温度.h
#ifndef Temperature_h
#define Temperature_h
#include <DallasTemperature.h>
#include <OneWire.h>
#define TEMPERATURE_WIRE_BUS 8
#define TEMPERATURE_RESOLUTION 11
OneWire oneWire(TEMPERATURE_WIRE_BUS);
DallasTemperature TemperatureSensors(&oneWire);
void initSensorTemperature();
void numberOfTemperatureSensor();
void getTemperature();
#endif
Temperature.cpp
#include "Temperature.h"
void initSensorTemperature() {
TemperatureSensors.begin();
TemperatureSensors.setResolution(TEMPERATURE_RESOLUTION);
}
void numberOfTemperatureSensor() {
Serial.print(F("Searching devices... "));
Serial.print(F("Found "));
Serial.print(TemperatureSensors.getDeviceCount());
Serial.println(F(" devices."));
}
void getTemperature() {
TemperatureSensors.requestTemperatures();
Serial.print(F("Temperature is "));
Serial.print(TemperatureSensors.getTempCByIndex(0));
Serial.print(F(" at "));
Serial.print(TemperatureSensors.getResolution());
Serial.println(F(" bits "));
}
Lcd.h
#ifndef Lcd_h
#define Lcd_h
#include <LiquidCrystal_I2C.h>
#define LCD_ADRESSE 0x27
#define LCD_COLUMN 16
#define LCD_LINE 2
LiquidCrystal_I2C lcd(LCD_ADRESSE, LCD_COLUMN, LCD_LINE);
// lcd.init();
void initLcd();
#endif
Lcd.cpp
#include "Lcd.h"
void initLcd(){
lcd.init();
lcd.backlight();
lcd.home();
lcd.printstr("INIT OK");
}