Compilation error on double click : a function-definition is not allowed here before '{' token

Hi i am new in Arduino and I'm working on a project for university and I'm getting this error: a function-definition is not allowed here before '{' token void doubleClick(){ on this program:


// Sketch to toggle a blinking LED. Push-on, push off.

#define ledOn HIGH
#define ledOff LOW

const int led1Pin =  8;  // set LED pin numbers
const int led2Pin =  4;
const int buttonPin1 = 12;   //set PushButton pin number
const int buttonPin2 = 7; 


int led1State = ledOff;    //initialising led
int led2State = ledOff;      
int previousState1 = LOW;       // the previous buttonState from the button pin
int previousState2 = LOW; 

unsigned long buttonTime = 0;                  // The last time the output pin was toggled
unsigned long debounceTime = 400;              // debounceTime time (I am using a noisy button)
int clicks = 0;


void setup() {
  pinMode(led1Pin, OUTPUT);  // initialize the LED pin as an output:
  pinMode(led2Pin, OUTPUT);
  pinMode(buttonPin1, INPUT_PULLUP);  // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT_PULLUP);
  digitalWrite(led1Pin, led1State);
  digitalWrite(led2Pin, led2State);
}


void loop() {
  int button1State = digitalRead(buttonPin1);
  if (button1State != previousState1 && millis() - buttonTime > debounceTime) {
    led1State = !led1State;
    buttonTime = millis();
  }

  if (led1State == ledOn) {
    digitalWrite(led1Pin, (millis() >> 9) & 3);  //Blink
  } else
    digitalWrite(led1Pin, ledOff);

  previousState1 = button1State;

   int button2State = digitalRead(buttonPin2);
  if (button2State != previousState2 && millis() - buttonTime > debounceTime) {
    led2State = !led2State;
    buttonTime = millis();
  }

  if (led2State == ledOn) {
    digitalWrite(led2Pin, (millis() >> 9) & 3);  //Blink
  } else
    digitalWrite(led2Pin, ledOff);

void doubleClick(){

  Serial.println("x2");

  led2State = !led2State; // reverse the LED
  digitalWrite(led2Pin, led2State);
} // doubleClick

  previousState2 = button2State;
}

Welcome to the forum

  • The loop() function has no closing curly brace

  • also, you have code outside of a function

void doubleClick()
{
  Serial.println("x2");
  led2State = !led2State; // reverse the LED
  digitalWrite(led2Pin, led2State);
} // doubleClick
previousState2 = button2State;
}

Thank you so much !!

You are welcome

Both problems showed up clearly when I used Auto Format in the IDE

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