Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I try to compile the following code in Arduino ,I get the error "expected initializer before 'void'".How can I get the code to work?

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

float area = 0.0f,pre_area = 0.0f,tarea = 0.0f,rarea = 0.0f;
int x = 0,pre_x = 0,t = 0,pre_t = 0;


long unsigned int t


void loop()
{
  x = analogRead(A0);
  t = millis();
  float diff_t = (float)(t - pre_t)/1000.0f;
  area = (0.5 * (pre_x + x) * diff_t) + pre_area;

  Serial.print(area);
  analogWrite(DAC0, area);
  pre_x = x;
  pre_area = area;
  pre_time = t;
}
share|improve this question
    
Now that the question is answered please flag it as answered – evolutionizer Aug 26 '15 at 14:48

You add the semicolon that is missing at the end of long unsigned int t

share|improve this answer
    
Amongst other things. ;) – Nick Gammon Aug 26 '15 at 10:54

Why have you written t = 0 here and declared it again below??

int x = 0,pre_x = 0,t = 0,pre_t = 0;

Semi Colon Missing

long unsigned int t;

Where have you declared pre_time??

  pre_time = t;
share|improve this answer
long unsigned int t

No semicolon.


Plus, make up your mind what type "t" is:

int x = 0,pre_x = 0,t = 0,pre_t = 0;
long unsigned int t;

Is it int or long unsigned int?


How can I get the code to work?

Avoid C++ syntax errors.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.