expected unqualified-id before '{' token Capsense

Line 10 there is an error with the bracket, I tried deleting the semicolon on Line 9 but it still gives me an error. Any help would be appreciated thanks!

#include <CapacitiveSensor.h>

int led = 10;                                          //change '42' to any desired pin...long time = 0;
int state = HIGH;

boolean yes;
boolean previous = false;

int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);                       
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor... 
   pinMode(led, OUTPUT);
}

voidloop()                    
{
    long total1 =  cs_4_2.capacitiveSensor(30);
    
    if (total1 > 60){yes = true;}
    else {yes = false;}    
    
    // to toggle the state of state
    if(yes == true && previous  == false && millis() - time>debounce){
      
       if(state == LOW){
         state = HIGH;
       }
       else 
         state = LOW;
         time = millis();     
       
    }
       
   
      digitalWrite(led, state);
      previous = yes;

      Serial.println(millis()-time);
      delay(10);
      #include <CapacitiveSensor.h>

int led = 10;                                          //change '42' to any desired pin...long time = 0;
int state = HIGH;

boolean yes;
boolean previous = false;

int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);       // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired// To add more sensors...//CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foilvoidsetup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor... 
   pinMode(led, OUTPUT);
}

void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); //Calibrate the sensor...
pinMode(led, OUTPUT);
}

You also might want to change this:-

voidloop()

to this:-

void loop()

You've also doubled up on some of the code. Remove the superfluous stuff and then I think you'll need to add a closing curly bracket for 'loop()'.

And you haven't declared 'time'. Should be an unsigned long.

Clean things up then re-post with any new errors. Not having that library, I can't compile to check.

Damn that sticky delete key. :wink:

So I’ve done what guys have said and I have this notice I added some new lines at the bottom to make it so the intensity changes as I slide my finger down the slider.

#include <CapacitiveSensor.h>

int led = 13;                                          //change '42' to any desired pin...
long time = 0;
int state = HIGH;

boolean yes;
boolean previous = false;

int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired// To add more sensors...//CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                   
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor... 
   pinMode(led, OUTPUT);
}

void loop() 
{
    long total1 =  cs_4_2.capacitiveSensor(30);
    
    if (total1 > 60){yes = true;}
    else {yes = false;}    
    
    // to toggle the state of state
    if(yes == true && previous  == false && millis() - time>debounce){
      
       if(state == LOW){
         state = HIGH;
       }
       else 
         state = LOW;
         time = millis();     
       
    }
       
   
      digitalWrite(led, state);
      previous = yes;

      Serial.println(millis()-time);
      delay(10);

      int x = map(total1, 100, 800, 0, 255);

analogWrite(13, x);


}

Now I think I’m missing something as my LED isn’t lighting up but pin 13 is when I touch the slider.

Thanks again guys!

huda123:
Now I think I'm missing something as my LED isn't lighting up but pin 13 is when I touch the slider.

Thanks again guys!

I don't follow you. The only LED mentioned in the code that you just posted is on pin 13.

int led = 13;
.
.
.
analogWrite(13, x);

So I have a LED in the ground pin and a 220 ohm resistor going from pin 13 to ground pin. This should be lighting up the LED but it isn't so I feel like I'm either missing something software wise or maybe my circuit is off.

Put the led in pin 11.
analogWrite
On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11.

No luck, not sure at all as to what's going wrong, I've removed the resistor tried pins other than 11 and it still doesn't light up. I had at one point at the onboard led at pin 13 turn on and off as I touched the foil but nothing other than that.

LarryD:
Put the led in pin 11.
analogWrite
On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11.

Well spotted. I didn't notice that.

Show us a good image of your wiring.

.

huda123:
I had at one point at the onboard led at pin 13 turn on and off as I touched the foil but nothing other than that.

Even on pin 13, 'analogWrite()' half works. If you write a value between 0 and 127, the LED will not light, and if you write a value from 128 to 255, it will light.

I just noticed, you should also change this:-
long time = 0;tounsigned long time = 0;

Regardless of which pin you're using, you need to rethink the code. Your 'analogWrite()' is called 10mS after the 'digitalWrite()', making the 'digitalWrite()' redundant. You'd barely see a 10mS LED flash, if at all.

Since you're using both a digitalWrite() and an analogWrite() for the same pin, you're working at cross-purposes. You need to think some more about what you're really trying to achieve.

Usually, too, it's a good idea to make variable names relate closely to their actual purpose. "yes" wasn't the best choice, and makes it harder to follow what's going on.

So here’s my new code I used the CapSense Demo Code and added some stuff to see if it would work. I have the 10M resistor between my pins 2 and 4 (send and receive) but the onboard led for pin 13 doesn’t turn on.

#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
 * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
 */

int ledPin = 13;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                    
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);
}

void loop()                    
{
    long start = millis();
    long total1 =  cs_4_2.capacitiveSensor(30);
    long total2 =  cs_4_6.capacitiveSensor(30);
    long total3 =  cs_4_8.capacitiveSensor(30);

    Serial.print(millis() - start);        // check on performance in milliseconds
    Serial.print("\t");                    // tab character for debug windown spacing

    Serial.print(total1);                  // print sensor output 1
    Serial.print("\t");
    Serial.print(total2);                  // print sensor output 2
    Serial.print("\t");
    Serial.println(total3);                // print sensor output 3

    int x = map(total1, 100, 800, 0, 255);

analogWrite(13, x);

    delay(10);                             // arbitrary delay to limit data to serial port 
}

You haven’t mentioned it, but which Arduino board are you using?

Are you sure your capacitive sensors are returning values between 100 and 800? The video linked from the Capacitive Sensor page shows values well into the thousands. I noticed one value of 6972.int x = map(total1, 100, 800, 0, 255);What are the values for total1, total2 and total3 that are being printed in the serial monitor? Perhaps you could copy and post the output from the serial monitor, (between code tags).

Finally, until you get the code working exactly as you want for a single capacitive sensor, adding more will only confuse the issue. It’s really better to just stick with the one for now, get it singing and dancing, then add the others.

Edit: And I just noticed, even after being told it’s wrong, you still persist in using a ‘long’ to store the ‘millis()’ value:-

long start = millis();

It needs to be ‘unsigned long’.
I found the library, and now see that the example uses ‘long’. The author is wrong. Change it as I suggested to avoid the value rolling over. A ‘long’ isn’t large enough to hold potential ‘millis()’ values.

Which version of the library are you using?

I'm using the Arduino UNO board. So I tried using the serial monitor but I got nothing back besides a static 495. So I think it isn't even sensing my touch on the foil. I'm using Version 4 of the library and have changed the code to include 'unsigned'

huda123:
I'm using the Arduino UNO board. So I tried using the serial monitor but I got nothing back besides a static 495. So I think it isn't even sensing my touch on the foil. I'm using Version 4 of the library and have changed the code to include 'unsigned'

Are you using a laptop or desktop computer? You need a good earth connection from the UNO to true ground.

Also, since you're using an UNO, re-read Larry's reply #7. On an UNO, pin 13 is not a PWM output, so cannot be used for 'analogWrite()'. It will go high or low depending on the value written, but it won't dim at all. (As I mentioned in reply #11.)

Can you take a high resolution photo of your setup and post it?