Errors in code with Capacitive Sensor

Hi All,

I wrote some code to have a capacitive sensor act as a switch to alternate between 2 led’s (make one on while the other is off, and then switch).
I can’t seem to figure out how to make the capsensor act as a switch, and i got the error ‘class CapacitiveSensor’ has no member named ‘capSense’
here is the full code:

#include <CapacitiveSensor.h>
#define MAINS 11 //the MAIN LED that will be displayed 
#define SECOS 10 //the SECONDARY LED that will serve as a backlight. This will start turned on.
const int threshold = 390;
int val = 0;
int old_val = 0;
int state = 0;
int Touch = 0;

/* ANDREW KUNK CAPSENSE SWITCH
 * USE THE OUTPUT OF CAPACITIVE SENSOR TO ALTERNATE BETWEEN 2 LEDs
 * I.E.: One LED is on while the other is off, and vice versa.
 * December 8, 2012
 * DC
 * Uses a Capacitor Sensor (1 Megohm resistor between pins 2&4,
   with a lead on pin 2
 */
 
CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);  //1M resistor btwn pins 4 & 2, pin2 is sensor, connected to metal panel

void setup()
{
  pinMode(MAINS, OUTPUT);
  digitalWrite(MAINS, LOW);
  pinMode(SECOS, OUTPUT);
  digitalWrite(SECOS, HIGH);
  cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
  Serial.begin(9600);
}
void loop()
{
  val = Touch;
  long totall = cs_4_2.capSense(100);
  
  Serial.println(totall);
  Serial.println(" ");
  
  
  if (totall > threshold){
    Touch = 1;
  } else {
    Touch = 0;
  }
  
  if ((val == HIGH)&& (old_val==LOW)){
    state = 1-state;
    
    delay (50); //delay for debouncing
  }
  
  old_val = val;
  
  if (state == 1) {
    digitalWrite(SECOS, LOW); //turn backlight off
    digitalWrite(MAINS, HIGH); //turn lights on
  } else {
    digitalWrite(SECOS, HIGH);
    digitalWrite(MAINS, LOW);
  }
}

Thanks Alot in advance :slight_smile:

Where is the header file? You wrote a class and you are posting errors associated with both the sketch and class without posting a reference to the class or it's source.
My crystal ball is broken so I can't help you...

Bob

Its the library from the capacitive sensor posted in the playground

Which one? There are several.

hi everyone,since i have found no answer to my questions and didn"t want to make a new post a hope to find my answer here.
i am triyng to do something similar,like lite a led when capacitive value is higher than 300,the library i used is cap.sense 003 and the code ive tryied is :

#include <CapSense.h>

/*

  • CapitiveSense Library Demo Sketch
  • Paul Badger 2008
  • Slightly adapted by Bare Conductive 2011
  • Uses a high value resistor e.g. 10 megohm between send pin and receive pin
  • Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
  • Receive pin is the sensor pin - try different amounts of Bare Paint
  • Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
    */

CapSense cs_4_2 = CapSense(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add Bare Paint

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

void loop()

{
long start = millis();
long total1 = cs_4_2.capSense(30);

Serial.println(total1); // OPTIONAL: To use additional sensors,change Serial.println to Serial.print for proper window spacing

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

…ok so this one works great,with the serial id say 300-350 is the perfect value for me.But i tried with a code ive made and something is rong,i want it to act like a toggle swich,here is my code…

#include <CapSense.h>

CapSense cs_4_2 = CapSense(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin
const int led1 = 13;
const int CapSense

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

void loop() {
static boolean lastSensorTouch = false;
static boolean LEDvalue = LOW;

long start = millis();
long total1 = cs_4_2.capSense(30);

int CapSenseValue = read(CapSense)1,0,900);
bool CapTouch = CapSenseValue > 350 && sensorValue <500;

if (CapTouch && !lastSensorTouch) // Now true, was false
{
LEDvalue = !LEDvalue; // Toggle the LED
digitalWrite(led1, LEDvalue);
}
lastSensorTouch = CapTouch;
}

Serial.println(total1);

delay(100);

anyone have any ideea,il apreciate it,thank you :wink:

But i tried with a code ive made and something is rong,i want it to act like a toggle swich,here is my code...

So, we know what you want the code to do. If we knew what it actually did, maybe we could help.

The code you posted (incorrectly) won't even compile.

hi PaulS
thank you for replay,ok the first code is just an example from the web wich display thru the serial values of how close is the hand to the foil,and the second one ..well i tried to make it like a toogle swich,using a swich code from an example,since i am new with the programing i try to keep it the simpliest as posible,and take it in parts,so i can learn...and since i didnt find any other code like the one i need i am asking for clues.thank you

i am asking for clues.

The code does something. You have not told us what it actually does.

You want the code to do something - act as a toggle switch.

You have not told us how what it actually does differs from what you want.

But, think about this. When you touch the capacitive sensor, the value does not jump to a higher value, and drop to 0 when you let go. The value ramps up and ramps back down. As you move your finger while touching the sensor, the value changes. WHICH value do you want to act as a toggle?

Hi PaulS i have been testing the capacity and yes you were right the values ramps and variates alot,and there are many factors that makes it unstable,so it took me some time to settingit to the right one,(for ex.if it the arduino board is powersuplied by usb and than change it to 12v Dc,the value passes from 260 to 1200 when you are 2 inches from the foil).Anyway dicided to go further with this i am posting the code that i actually use,with the hope to help me changeing it to a toogle swich
The code below light a led for 2000 milisec when the value is more than 1100

  #include <CapSense.h>


CapSense   cs_4_2 = CapSense(4,2);         // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add Bare Paint
int ledPin1 = 13;

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

void loop()                    
{    
    long start = millis();    
    long total1 =  cs_4_2.capSense(30);   
 
    if (total1 > 1100) {
      digitalWrite(ledPin1, HIGH);  // turn LED1 ON
       delay(2000);
    } else{
      digitalWrite(ledPin1, LOW);  // turn LED1 OFF
 }

Serial.println(total1);    
delay(100);                                         
}

thank you again :~

What sequence of events do you want to have happen to cause the pin to toggle? A reading below 1100, followed by a reading below 1100, followed by a reading above 1100?

PaulS:
What sequence of events do you want to have happen to cause the pin to toggle? A reading below 1100, followed by a reading below 1100, followed by a reading above 1100?

in my test the value that i registreted in my room are from 0 to 1000 …ok…now what i want to do is when i close the hand to foil (2-3inches,and values reach 1100 or more),make the led1 On,and when i close the hand again make “led1 Off” …it should be something like

const int led1 = 13;
const int sensor = cs_4_2.capSense; // i think this is rong

void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT); 
}

void loop() {
  static boolean "sensor" = false; 
  static boolean LED1value = LOW;

  bool sensorTouch = sensorValue > 1000 && sensorValue <1900;

  if (sensorTouch && !lastSensorTouch)  // Now true, was false
    {
    LED1value = !LED1value; // Toggle the LED1
    digitalWrite(led1, LED1value);
    }
  lastSensorTouch = sensorTouch;
  delay(150);
}

now i know this example i rote is rong but the ideea is to get something similar,thank you and excuse me for being so annoyng

  static boolean "sensor" = false;

Why is sensor in double quotes?

  bool sensorTouch = sensorValue > 1000 && sensorValue <1900;

Where does sensorValue get a value?

If you declare all the variables referenced, and actually assign a value to sensorValue, does that code do what you want?

It does not appear that it will, since it does not account for the sensor dropping below 1000.

As you are discovering, analog devices are not well suited as digital devices.