Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno" sketch_oct31a:2: err

This error message came up. Am new to this and a noob. Please help.

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Uno"

sketch_oct31a:2: error: expected initializer before 'int' sketch_oct31a:6: error: expected initializer before 'Serial' sketch_oct31a:8: error: expected declaration before '}' token expected initializer before 'int'

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

My code is:

void setup()

int moistureSensor = 0; int moisture_val;

void setup() Serial.begin(9600); //open serial port

}

void loop() moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes Serial.print("moisture sensor reads "); Serial.println( moisture_val ); delay(500);

}

}

It would be easier to discuss this if the code was properly formatted. That is,

(1) The code should be autoformatted (CTRL-T) in the IDE.

(2) The code should be posted in code tags using the </> button.

However…

This

void setup() 

  int moistureSensor = 0;
int moisture_val;

void setup() 
Serial.begin(9600); //open serial port

}

is missing a left curly bracket and has setup() listed twice. It should look like this:

void setup() 

{
  int moistureSensor = 0;
  int moisture_val;
  Serial.begin(9600); //open serial port
}

loop() is also missing a left curly bracket and should look like this:

void loop() 
{
  moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probes
  Serial.print("moisture sensor reads ");
  Serial.println( moisture_val );
  delay(500);
}

There is also an extra right curly bracket at the end.

delay(…) is dangerous and should be avoided except for rare cases such as examples.

The error messages always contain the name of the file with the error, and contain a line number that should help (although sometimes the error is a line or two PRIOR to the one listed).

...but more likely you intended:

int moistureSensor = 0;
int moisture_val;

void setup() 
{
  Serial.begin(9600); //open serial port
}

How do you have your moisture sensor hooked up? Is it really connected to pin 0?

vaj4088: How do you have your moisture sensor hooked up? Is it really connected to pin 0?

If it is, the OP may have problems uploading his sketch, after he's got it to compile.