Arduino (C)
I've got a function that works in my test document, but when transfered to my finished project I get the error "expected ´)' before ';' token".
I've included first the program in its functional test state, as well as the code of the non-functional code, I know the second one is still messy but has been functional so far, and I'm hoping to figure out why the while function causes errors. I am also sorry that the code is not in full english but rather danish, I hope this does not make it completely impossible.
Error messages from compiler:
Skarmmedudtest_V_1_1.ino: In function 'float tagTemp_NTC() ': Skarmmedudtest_V_1_1:142: error: expected: ´)' before ';' token Skarmmedudtest_V_1_1:142: error: expected: primary-expression before ')' token Skarmmedudtest_V_1_1:142: error: expected: ´;' before ')' token Skarmmedudtest_V_1_1:806: error: expected: ´}' at end of input
Functional code:
#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);
}
Disfunctional code;
#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
I'm pretty sure your problem is on this line, but it's hard to know because you didn't say which line the compiler gives you error on:
while (i<STR)
This is because you do:
#define STR 100;
The pre-processor will change that to:
while (i<100;)
which is not a valid expression in C.
Maybe i'm missing something [as you've not put complete code], but you have defined something called
float mindst_luft;
and you're using
mindstLuft=1000;
In the Functional code you've shown, mindstLuft
is not defined anywhere.
Also, your while()
loop is useless, because, on you're calling return
unconditionally.
EDIT:
Ok then, what is exactly A0
? and A1
, A2
...? if you want to represent some hex
value, you've got to use like 0xA0
.
EDIT:
Ok, by avoiding the confusion of functional and disfunctional code, the problem is with the MACRO definition. Considering the MACRO processing as a word-by-word replacement,
#define STR 100;
^
|
will make your while loop look like
while(i < 100;)
which is a wrong syntax.
get rid of the ;
in #define
上一篇: 在结构定义中''''标记之前预期''
下一篇: Arduino(C)