Programming question

Hello, I am just starting out with Arduino and have some trouble with the " function-definition is not allowed here before '{' token" error. If someone could help, that would be great!

It says there is a problem on the second void loop line.
Here is the code:
int Pirsensor = 8;
const int trigPin = 13;
const int echoPin = 12;

float duration;
float distance;

void setup() {
pinMode(13,OUTPUT);
pinMode(Pirsensor, INPUT);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
int sensormode = digitalRead(Pirsensor);
if (sensormode == 1) {
void loop() {
digitalWrite(13, HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
if (distance <= 5) {
digitalWrite(13, LOW);
}
}
}
}

void loop() {
  int sensormode = digitalRead(Pirsensor);
  if (sensormode == 1) {
    void loop() {

What's wrong with the above?

More specifically- what's wrong here:

void loop() {
  int sensormode = digitalRead(Pirsensor);
  if (sensormode == 1) {
    void loop() {

Read this before posting a programming question

Note the // lines in loop()

void loop() {
  int sensormode = digitalRead(Pirsensor);
  if (sensormode == 1) {
    //void loop() {
      digitalWrite(13, HIGH);
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = duration * 0.034 / 2;
      if (distance <= 5) {
        digitalWrite(13, LOW);
      }
    //}
  }
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

second void loop

Says it all

Even if you could define a function within a function (which you can't) you can't have 2 functions with the same name and definition

What exactly are you trying to do ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.