C ~ Arduino:调用函数后停止
C ~ Arduino: halt after call to function
我很难调试这段代码,我希望有人能指出我犯错的地方。
我的串行输出是:
connected
GetData()
got data...
next stage...
我已经使用一个简单的网络客户端进行了测试,该客户端循环发送hello world,并且工作正常。每次此代码在尝试向client
发送值后停止时。我知道getData()
导致了这个问题,但我无法弄清楚为什么,或者如何最好地解决它。(对代码转储表示歉意,我想彻底)
#include <LiquidCrystal.h>
#include <dht11.h>
#include <SPI.h>
#include <Ethernet.h>
dht11 DHT11;
#define DHT11PIN 8
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,100);
IPAddress ip(192,168,1,50);
EthernetClient client;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int trigPin = 6;
const int echoPinOne = 7;
const int echoPinTwo = 8;
const int echoPinThree = 9;
const int echoPinFour = 10;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
delay(1000);
//Network
if (Ethernet.begin(mac) == 0) {
Serial.println("DHCP FAIL");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
lcd.setCursor(0, 0);
lcd.print("connecting");
connect();
}
void connect(){
if (client.connect(server, 8080)) {
Serial.println("connected");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("connected");
client.println("hello server");
client.println();
delay(500);
lcd.clear();
}
else {
Serial.println("connection failed");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connection Failure");
}
delay(500);
}
void loop() {
//network
if(!client.available()){
Serial.println("lost connection!-restart");
lcd.println("Disconnection");
client.stop();
connect();
}
else {
int inp[5];
getData(inp);
Serial.println("next stage...");
char *output = (char *) &inp[0];
client.println(output);
client.println("first");
Serial.println("sent first...");
//here repeats the same process for the other sensors - omitted for clear reading
}
//end network
delay(500);
}
void getData(int *inputBuf){
Serial.println("GetData()");
long durationThree, inchesThree, cmThree = 0;
long durationOne, inchesOne, cmOne = 0;
long durationTwo, inchesTwo, cmTwo = 0;
long durationFour, inchesFour, cmFour = 0;
//SENSOR ONE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinOne, INPUT);
durationOne = pulseIn(echoPinOne, HIGH);
delay(100);
//SENSOR TWO
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinTwo, INPUT);
durationTwo = pulseIn(echoPinTwo, HIGH);
delay(100);
//SENSOR THREE
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinThree, INPUT);
durationThree = pulseIn(echoPinThree, HIGH);
delay(100);
//SENSOR FOUR
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPinFour, INPUT);
durationFour = pulseIn(echoPinFour, HIGH);
//DISTANCE CALCULATION
cmOne = microsecondsToCentimeters(durationOne);
cmTwo = microsecondsToCentimeters(durationTwo);
cmThree = microsecondsToCentimeters(durationThree);
cmFour = microsecondsToCentimeters(durationFour);
//Serial.println("sending...");
//char *out = (char *) &cmOne;
//Serial.println(out);
//client.println(out);
//Serial.println("sent!"); used this to test - same issue
Serial.println("got data...");
inputBuf[0] = cmOne;
inputBuf[1] = cmTwo;
inputBuf[2] = cmThree;
inputBuf[3] = cmFour;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
我不知道
println()
是如何工作的,但假设它需要一个nul
终止的字节序列,所以你需要nul
在此声明之后终止
int inp[5]
添加这个
inp[4] = ' ';
或者您可以在此处添加
inputBuf[0] = cmOne;
inputBuf[1] = cmTwo;
inputBuf[2] = cmThree;
inputBuf[3] = cmFour;
inputBuf[4] = 0; // <--- here
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销