如何为Arduino代码声明C ++函数?

时间:2014-04-24 05:39:41

标签: c++ arduino

我收到了以下用于从蓝牙接收文本的Arduino代码,并将其显示回LCD显示屏。

输入 Enter 时,应使用IP String作为参数调用detectarClase()

这是.ino代码:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <string.h>

LiquidCrystal_I2C lcd(0x20,16,2); 

boolean borrar = false;
String IP;

String detectarClase(String ip) {

  char var[ip.length()]
  ip.toCharArray(var, ip.length());

  int num= atoi(var);  

  if (num < 127 ) 
    return "Clase A";
  if (num == 127 ) 
    return "Direccion reservada";
  if (num > 127 && num < 192   )
    return "Clase B";
  if (num >= 192 && num < 224  )
    return "Clase C";
  if (num >= 224 && num < 240  )
    return "Clase D";
  if (num >= 240 && num < 255  )
    return "Clase E";
}

void setup()
{
  lcd.init();
  lcd.backlight();
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop() {

  while (Serial1.available()) { 

    char caracter = Serial1.read(); //Comprobamos el caracter 

    switch(caracter) {

    default:
                if (borrar) { 
                              IP = ""; 
                              lcd.clear();
                }

                lcd.print(caracter);
                delay(125);
                borrar = false;

                IP.concat(caracter);
                break;  

    case '\r':                
    case 0x0F:
    case 0x0A:  
                borrar = true;
                lcd.print(detectarClase(IP));
                break;  

  } //fin switch

}//serial disponible


}//fin programa

当我尝试编译时,我得到以下输出:

BluetoothLCD.cpp: In function ‘String detectarClase(String)’:
BluetoothLCD.cpp:17:3: error: expected initializer before ‘ip’
BluetoothLCD.cpp:19:25: error: ‘var’ was not declared in this scope

我真的看不出我应该如何声明这个功能,我怎么能修改它?

1 个答案:

答案 0 :(得分:1)

尝试

char var[100];

在编译时,数组可能需要不变的大小。我看到你错过了';'在该行的末尾。