Arduino(C)

我有一个可以在我的测试文档中工作的函数,但是当转到我完成的项目时,我会在'before'之前得到错误'expected')';' 令牌”。

我已经在程序的功能测试状态,以及非功能代码的代码,我知道第二个仍然凌乱,但功能到目前为止,我希望弄清楚为什么而功能导致错误。 我也很抱歉,代码不是完整的英文,而是丹麦文,我希望这不会完全不可能。

编译器的错误信息:

Skarmmedudtest_V_1_1.ino:函数'float tagTemp_NTC()':Skarmmedudtest_V_1_1:142:error:expected:')'before';' 令牌Skarmmedudtest_V_1_1:142:错误:预期:''''令牌之前的主表达式Skarmmedudtest_V_1_1:142:error:expected:';' 之前')'令牌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

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

while (i<STR)

这是因为你这样做:

#define STR 100;

预处理器会将其更改为:

while (i<100;)

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


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

float mindst_luft;

你正在使用

mindstLuft=1000;

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

此外,你的while()循环是无用的,因为在你无条件地调用return


编辑:

那好吧,究竟是什么A0 ? 和A1A2 ...? 如果你想表示一些hex值,你必须使用像0xA0


编辑:

好的,通过避免功能和不起作用的代码混淆,问题在于MACRO的定义。 考虑到MACRO处理是逐字替换的,

#define STR 100;
               ^
               |

会让你的while循环看起来像

while(i < 100;)

这是一个错误的语法。

摆脱;#define

链接地址: http://www.djcxy.com/p/84393.html

上一篇: Arduino (C)

下一篇: Function parameter as case in switch statement