Motion & solenoid

Guys i need help here. my programming can't work. It's said "exit status 1
expected '}' at end of input" :frowning:

int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 1; // variable to store the sensor status (value)
int solenoidPin = 7; //This is the output pin on the Arduino

void setup()
{
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop()
{
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait 1 Second
digitalWrite(solenoidPin, LOW); //Switch Solenoid OFF
delay(1000); //Wait 1 Second
{
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(solenoidPin, HIGH); // turn solenoidPin ON
delay(100); // delay 100 milliseconds

if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(solenoidPin, LOW); // turn solenoidPin OFF
delay(200); // delay 200 milliseconds

if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

Message says you're short a }.

That's because you have an extra { here:

delay(1000);                          //Wait 1 Second
  digitalWrite(solenoidPin, LOW);       //Switch Solenoid OFF
  delay(1000);                          //Wait 1 Second
  { <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    val = digitalRead(sensor);           // read sensor value
    if (val == HIGH) {                   // check if the sensor is HIGH
      digitalWrite(solenoidPin, HIGH);

But now its said

exit status 1
Error compiling for board Arduino/Genuino Uno.

Yeah I get that message all the time even with good code. But you should scroll up in the message pane and see what else it says.

Heaven knows what the phantom error is caused by, but if it happens with code I believe to be good (like it may have worked the other day) I just sit and hit the compile icon again and again and again and eventually code will compile with no changes. (Assuming it doesn't have actual errors....)

But anyway, when I took that { out of your code it compiled for me.

Thanks. Hope it can work for me