【问题标题】:Arduino (C) - "expected primary-expression before ')'Arduino(C)-“')'之前的预期主要表达式
【发布时间】:2014-12-04 12:09:14
【问题描述】:

我有一个可以在我的测试文档中使用的函数,但是当转移到我完成的项目时,我在 ';' 之前收到错误“expected ´)'令牌”。

我首先包含了处于功能测试状态的程序,以及非功能代码的代码,我知道第二个仍然很乱但到目前为止一直正常工作,我希望弄清楚找出为什么 while 函数会导致错误。我也很抱歉代码不是全英文而是丹麦语,我希望这不会使它完全不可能。

来自编译器的错误消息:

Skarmmedudtest_V_1_1.ino:在函数“float tagTemp_NTC()”中: Skarmmedudtest_V_1_1:142:错误:预期:')'在';'之前令牌 Skarmmedudtest_V_1_1:142:错误:预期:')' 标记之前的主表达式 Skarmmedudtest_V_1_1:142:错误:预期:';'在 ')' 标记之前 Skarmmedudtest_V_1_1:806:错误:预期:输入结束时为“}”

功能代码:

    #define STR 100
float ar[STR];
float x;
int i;
int sum;
float mindstLuft;
float temp;

//DENNNE VIRKER OG MÅLER MED EN NØJAGTIGHED PÅ 0.5 GRADER
void TagTemp(){ 
float hej = analogRead(A0);
temp = (float)0.10988796957380947*(float)hej-(float)31.34142857142854334;
 }


float tagTemp_NTC()
{
  int i=0;
  mindstLuft=1000; //sikre at den første måling -altid- er anderledes//

  while (i<STR)
  {
  float hej = analogRead(A0);
  temp = (float)0.10988796957380947*(float)hej-(float)31.34142857142854334;
  delay(1);
  if(temp<mindstLuft)
  {mindstLuft=temp;}
  return mindstLuft; 
  i++;
  }
}

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


void loop()
{ 
  tagTemp_NTC();
  Serial.print("Smallest measurement is;");
  Serial.println(mindstLuft);  



//}
delay(1000);
}

功能失调的代码;

 #include <Tone.h>
#include <i2cmaster.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Time.h>
#include <Thermistor.h>
#include "pitches.h"
Thermistor temp(0);

// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);

// Hardware SPI (faster, but must use certain hardware pins):
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
// Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
// Note with hardware SPI MISO and SS pins aren't used but will still be read
// and written to during SPI transfer.  Be careful sharing these pins!

//////////////////GLOBALE DATAVÆRDIER/////////////////////
#define STR 100;
#define reed 52//pin connected to read switch
unsigned long start, finished, elapsed, midt;
int reedVal = 0;
int counter = 1;
int array[1];
int countLyd = 0;
float tid;
float distance;
int buttonState=1;
int count =0;
float mindstLuft;
//////////////////GLOBALE DATAVÆRDIER/////////////////////









/////////////////////////////////////     UR      ///////////////////////////////////////

void ur()
{

  display.setTextSize(1);
  display.setCursor(42,0);
  display.print(hour());
  taltilur(minute());
  taltilur(second());
  display.println();
}

void taltilur(int digits)
{
  display.print(":");
  if(digits < 10)
    display.print('0');
  display.print(digits);
}

int sensorValueNTC = analogRead(A0);
int sensorValueIFR = analogRead(A1);
int sensorValueFart = analogRead(A2);
int sensorValueDistance = analogRead(A3);

////////////////////////////////////////////////////////////////////////////////////////


int temperatureIR;

void vejTemp()
{
  //////////////////////////////////KODE TIL IR SENSOR///////////////////////////////////
  int dev = 0x5A<<1;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck(); //Read 1 byte and then send ack
  data_high = i2c_readAck(); //Read 1 byte and then send ack
  pec = i2c_readNak();
  i2c_stop();

  //This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
  double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
  double tempData = 0x0000; // zero out the data
  int frac; // data past the decimal point

  // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;

  float celcius = tempData - 273.15;
  float fahrenheit = (celcius*1.8) + 32;

  display.setTextSize(1.5);
  display.setTextColor(BLACK);
  display.setCursor(0,25);
  display.print("Vej temp:");
  display.setTextSize(1.5);
  display.setTextColor(BLACK);
  display.setCursor(60,25);
  display.print(celcius,1); // - temp fra infrarød
  //display.print("10*");
}

////////////////////////////////////////////////////////////////////////////////////////











///////////////////////////////   NTC TEMP    ///////////////////////////////////////////////
float tagTemp_NTC()
{
  int i=0;
  mindstLuft=1000; //sikre at den første måling -altid- er anderledes//

  while (i<STR)
    {
    float hej = analogRead(A0);
    temp = (float)0.10988796957380947*(float)hej-(float)31.34142857142854334;
    delay(1);
    if(temp<mindstLuft)
    {mindstLuft=temp;}
    return mindstLuft; 
    i++;
    }
}

void luftTemp()
{

  display.setTextSize(1.5);
  display.setTextColor(BLACK);
  display.setCursor(0,35);
  display.print("Luft temp:");
  display.setTextSize(1.5);
  display.setTextColor(BLACK);
  display.setCursor(60,35);
  display.print(mindst_luft, 1);

  //display.print("10*");
}
////////////////////////////////////////////////////////////////////////////////////////











//////////////////////////////////////     DISTANCE    //////////////////////////////////////////////////
void distance2()
{


  float radius = 0.35; //meter. Dette skal brugeren selv skrive ind fra skaermen.
  float hjulomkreds = 3.14 * (radius*2);


  reedVal = digitalRead(reed);//get val of A0



  if(reedVal == 1 && counter == 1)
  {
    counter = 0;

    start = millis();

  }
  if(reedVal == 0 && counter == 0)
  {

    counter = 2;
  }


  if(reedVal == 1 && counter == 2)
  {
    finished = millis();
    elapsed = (finished - start);
    array[1] = elapsed;
    tid = array[1];


    counter = 0;
    start = millis();
    distance = distance + hjulomkreds;
  }
  delay(200);


  float hastighed =   ((hjulomkreds/tid)*3600);


  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(1,0);
  display.print(distance); // - distance fra sensor
  //display.print("10km");
}
////////////////////////////////////////////////////////////////////////////////////////









////////////////////////////////////   FART   ////////////////////////////////////////////////////
void fart()
{

  float radius = 0.35; //meter. Dette skal brugeren selv skrive ind fra skaermen.
  float hjulomkreds = 2 * 3.14 * radius*radius;


  reedVal = digitalRead(reed);//get val of A0



  if(reedVal == 1 && counter == 1)
  {
    counter = 0;

    start = millis();

  }
  if(reedVal == 0 && counter == 0)
  {

    counter = 2;
  }


  if(reedVal == 1 && counter == 2)
  {
    finished = millis();
    elapsed = (finished - start);
    array[1] = elapsed;
    tid = array[1];


    counter = 0;
    start = millis();
    distance = distance + 0.35;
  }
  delay(200);

  //float hastighed = ((hjulomkreds * 3600)/tid); //  Km/t
  float hastighed =   ((hjulomkreds/tid)*3600);  


  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,10);
  display.print("Fart:");
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(30,10);
  display.print(hastighed, 1);
  display.setCursor(60,10);
  display.print("km/t");

}
////////////////////////////////////////////////////////////////////////////////////////











/////////////////////////////////////    LYS DIODER     ///////////////////////////////////////////////////
void lysG()
{
  digitalWrite(22, HIGH);
  digitalWrite(24, LOW);
  digitalWrite(26, LOW);
}

void lysR()
{
  digitalWrite(22, LOW);
  digitalWrite(26, HIGH);
  digitalWrite(24, LOW);
}

void lysY()
{
  digitalWrite(22, LOW);
  digitalWrite(26, LOW);
  digitalWrite(24, HIGH);
}

void slukLysGreen()
{
  digitalWrite(22, LOW);
}

void slukLysYellow()
{
  digitalWrite(24, LOW);
}

void slukLysRed()
{
  digitalWrite(26, LOW);
}
////////////////////////////////////////////////////////////////////////////////////////












///////////FUNKTIONER TIL 1 PER SKÆRM//////////////
void ur1()
{
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,20);
  display.print(hour());
  taltilur(minute());
  taltilur(second());
  display.println();
  display.display();
}


void vejTemp1()
{
  display.clearDisplay();
  //////////////////////////////////KODE TIL IR SENSOR///////////////////////////////////
  int dev = 0x5A<<1;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck(); //Read 1 byte and then send ack
  data_high = i2c_readAck(); //Read 1 byte and then send ack
  pec = i2c_readNak();
  i2c_stop();

  //This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
  double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
  double tempData = 0x0000; // zero out the data
  int frac; // data past the decimal point

  // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;

  float celcius = tempData - 273.15;
  float fahrenheit = (celcius*1.8) + 32;

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(15,5);
  display.print("Vej temp:");
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(15,25);
  display.print(celcius, 1);// - temp fra infrarød
  //display.print("10*");
  display.setTextSize(1);
  display.setCursor(63,23);
  display.print("o");
  display.display();
}

void luftTemp1()
{
  display.clearDisplay();
  float tempNTC = temp.getTemp();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(15,5);
  display.print("Luft temp:");
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(15,25);
  display.print(tempNTC, 1);
  display.setTextSize(1);
  display.setCursor(63,23);
  display.print("o");
  display.display();

}

void distance1()
{
  display.clearDisplay();

  float radius = 0.35; //meter. Dette skal brugeren selv skrive ind fra skaermen.
  float hjulomkreds =  3.14 * (radius*2);


  reedVal = digitalRead(reed);//get val of A0



  if(reedVal == 1 && counter == 1)
  {
    counter = 0;

    start = millis();

  }
  if(reedVal == 0 && counter == 0)
  {

    counter = 2;
  }


  if(reedVal == 1 && counter == 2)
  {
    finished = millis();
    elapsed = (finished - start);
    array[1] = elapsed;
    tid = array[1];

    counter = 0;
    start = millis();
    distance = distance + hjulomkreds;
  }
  delay(200);

  //float hastighed = ((hjulomkreds * 3600)/tid); //  Km/t
  float hastighed =   ((hjulomkreds/tid)*3600);




  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(15,5);
  display.print("Distance:");
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(20,25);
  display.print(distance/1000); // - distance fra sensor
  display.display();
  //display.print("10 km");
}


void fart1()
{
  display.clearDisplay();

  float radius = 0.35; //meter. Dette skal brugeren selv skrive ind fra skaermen.
  float hjulomkreds = 2 * 3.14 * radius*radius;


  reedVal = digitalRead(reed);//get val of A0



  if(reedVal == 1 && counter == 1)
  {
    counter = 0;

    start = millis();

  }
  if(reedVal == 0 && counter == 0)
  {

    counter = 2;
  }


  if(reedVal == 1 && counter == 2)
  {
    finished = millis();
    elapsed = (finished - start);
    array[1] = elapsed;
    tid = array[1];

    counter = 0;
    start = millis();
    distance = distance + 0.35;
  }
  delay(200);


  float hastighed =   ((hjulomkreds/tid)*3600);


  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(15,5);
  display.print("Hastighed:");
  display.setTextSize(2);
  display.setTextColor(BLACK);
  display.setCursor(1,25);
  display.print(hastighed, 1); // - farten fra sensor
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(53,32);
  display.print("km/t");
  display.display();
  //display.print("25 km/t");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////










//////////////////////////////////    START SKÆRM    //////////////////////////////////////////////////////
void skaermSetup1()
{
  ur();
  luftTemp();
  vejTemp();
  fart();
  distance2();
  display.display();
  display.clearDisplay(); 
}  
////////////////////////////////////////////////////////////////////////////////////////










////////////////////////////////////     SETUP AF SKÆRM     ////////////////////////////////////////////////////
void setup()  
{
  i2c_init(); //Initialise the i2c bus
  PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
  pinMode(25, OUTPUT);
  pinMode(12, INPUT_PULLUP);
  pinMode(22, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(26, OUTPUT);
  Serial.begin(9600);

  display.begin();
  display.setContrast(50);
  display.setRotation(2);
  display.display(); // show splashscreen
  delay(2000);
  display.clearDisplay();   // clears the screen and buffer
}
////////////////////////////////////////////////////////////////////////////////////////











////////////////////////////////////BUZZER//////////////////////////////////
void ylyd()
{
  tone(8, NOTE_C4, 300);
  delay(500);
  tone(8, NOTE_C4, 300);
  delay(1000);
  countLyd=0;
}


void rlyd()
{
  tone(8, NOTE_C4, 300);
  delay(500);
  tone(8, NOTE_C4, 300);
  delay(500);
  tone(8, NOTE_C4, 300);
  delay(500);
  tone(8, NOTE_C4, 300);
  delay(500);
  tone(8, NOTE_C4, 300);
  delay(1000);
  countLyd=1;
}
//////////////////////////////////////////////////////////////////////////////











void loop() 
{
  float tempNTC = temp.getTemp();

  int dev = 0x5A<<1;
  int data_low = 0;
  int data_high = 0;
  int pec = 0;

  i2c_start_wait(dev+I2C_WRITE);
  i2c_write(0x07);

  // read
  i2c_rep_start(dev+I2C_READ);
  data_low = i2c_readAck(); //Read 1 byte and then send ack
  data_high = i2c_readAck(); //Read 1 byte and then send ack
  pec = i2c_readNak();
  i2c_stop();

  //This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
  double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
  double tempData = 0x0000; // zero out the data
  int frac; // data past the decimal point

  // This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
  tempData = (double)(((data_high & 0x007F) << 8) + data_low);
  tempData = (tempData * tempFactor)-0.01;

  float celcius = tempData - 273.15;
  float fahrenheit = (celcius*1.8) + 32;










///////////////////////////////////////     DET MED KNAPPER     ///////////////////////////////////////////////

  int knapVal = digitalRead(12);

  if (knapVal == LOW & buttonState ==1)
  {
    count = count+1;
    buttonState = 2; 
  }

  if (knapVal == HIGH & buttonState ==2)
  {
    buttonState =1; 
  }

  if(count==0)
  {
    skaermSetup1();

  }
  if(count==1){
    display.clearDisplay();
    ur1();
    display.display();
  }

  if(count==2){
    display.clearDisplay();
    vejTemp1();
    display.display();
  }

  if(count==3){
    display.clearDisplay();
    luftTemp1();
    display.display();
  }

  if(count==4){
    display.clearDisplay();
    fart1();
    display.display();
  }

  if(count==5){
    display.clearDisplay();
    distance1();
    display.display();
  }

  if(count==6)
  {
    count=0; 
  }
////////////////////////////////////////////////////////////////////////////////////////











//////////////////////////////////    DET MED LYS    //////////////////////////////////////////////////////
  if(tempNTC < 26 || celcius < 26) // ændres til 0 og 0
  {
    lysR();

    if (countLyd==0)
    {
      rlyd();
    }
  }


  else if(tempNTC > 30 && celcius < 30)
  {
    lysY();
    if (countLyd==1 || countLyd==2)
    {
      ylyd();
    }
  }

  else if(tempNTC < 30 && celcius > 30)
  {
    lysY();
    if (countLyd==1 || countLyd==2)
    {
      ylyd();
    }
  }

  else if(tempNTC < 30 && celcius < 30 && celcius > 26 && tempNTC > 26) // ændres til 2 og 2 og 0 og 0
  {
    lysY();
    if ((countLyd==1 || countLyd==2))
    { 
      ylyd();
    }
  }

  else if(tempNTC > 30 && celcius > 30) // ændres til 2 og 2
  {
    lysG();
    countLyd=2;
  }
  ////////////////////////////////////////////////////////////////////////////////////////

}// LOOP END

【问题讨论】:

  • 1.我相信您的编译器已为您提供了更多信息。也给他们看。 2. 在使用二元或三元时,操作数和运算符之间始终保持一个空格,以提供更好的可读性。
  • 你在 while 循环中的 i++ 永远不会被调用。
  • 我已经添加了所有的错误信息,还有 Ali786 你能解释为什么它不会吗?它在功能代码中运行良好。

标签: c while-loop arduino


【解决方案1】:

我很确定你的问题在这一行,但很难知道,因为你没有说编译器在哪一行给你错误:

while (i<STR)

这是因为你这样做了:

#define STR 100;

预处理器会将其更改为:

while (i<100;)

这不是 C 中的有效表达式。

【讨论】:

  • 那是怎么回事?我认为那里没有;(在功能代码中)。
  • @NatashaDutta 有。在标记为“Disfunctional code;”的第二个代码中,在“/////////////////GLOBALE DATAVÆRDIER//////////////////////////////////////////////// //////////"
  • 抱歉我的输入错误:) 但它基于您指出的 while 函数是的(这与我在其功能状态下显示的函数相同)。我会在没有定义的 STR 的情况下尝试。 答案: 似乎通过了验证,仍然对 - 为什么 - 在其他代码中工作时使其无法正常工作感到困惑。
  • @jonasnielsen 在其他代码中,您没有分号。这使得 while 循环进入while (i&lt;100),这是正确的。 while (i&lt;100;) 是无效的 C。
  • @Art 一样。我也在关注那个 functional 代码。 :-) 也会更新我的答案。
【解决方案2】:

也许我遗漏了一些东西 [因为你没有放完整的代码],但是你已经定义了一些叫做

float mindst_luft;

你正在使用

mindstLuft=1000;

在您显示的功能代码中,mindstLuft 没有在任何地方定义。

另外,您的 while() 循环是无用的,因为您正在无条件地调用 return


编辑:

那么,A0 到底是什么?和A1A2...?如果你想代表一些hex 值,你必须使用0xA0


编辑:

好的,通过避免混淆 functionaldisfunctional 代码,问题出在 MACRO 定义上。将 MACRO 处理视为 逐字 替换,

#define STR 100;
               ^
               |

会让你的 while 循环看起来像

while(i < 100;)

这是错误的语法。

去掉#define中的;

【讨论】:

  • 我注意到这是从我尝试测试时开始的,之前就出现了错误。当一切都使用 mindstLuft 而不是 mindst_luft 时,进行编辑以适应。
  • 我会尽力回答(在 6 人的项目组中工作)。所有 A# 都是 arduino 的模拟端口,目前有以下分配; A0 = NTC(用于测量空气温度) A1 = 空 atm A2 = 空 atm
  • @jonasnielsen 我不明白,能否请您显示确切的代码?还有,analogRead()函数的签名,请
  • 对于 arduino,我们有模拟端口,可以接收来自各种传感器的输入,A0 端口用于 NTC 传感器,可用于测量其上的温度,这意味着它可以测量空气-周围的温度。端口 A2 和 A3 目前是空的,因为它们没有被使用,但应该很快就会使用。 AnalogRead() 几乎只是读取当时通过它的电流并将其保存在数据类型中。
  • @jonasnielsen 好的。希望您的问题现在得到解决。 :-)
猜你喜欢
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多