Pages: [1]   Go Down
Author Topic: capacitive switch variable  (Read 399 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi there everyone...i need your help with a capacity code.What im trying to do is a toggle switch(when i touch the foil led turn HIGH,when i touch again led turn LOW).the code i have is this one,but i don't know how to add the variable where to store the led status,so it can change every time i touch the foil.
Code:
#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 LED ON
      
    } else{
      digitalWrite(ledPin1, LOW);  // turn LED OFF
 }

Serial.println(total1);                                

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

thank you so much for help...  smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8108
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's an example I wrote for toggling an LED when a sensor triggered:

Code:
#include <CapSense.h>

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

const 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() {
  static boolean lastSensorHit = false;
  static boolean LEDvalue = LOW;

  bool sensorHit = cs_4_2.capSense(30) > 1100;

  if (sensorHit && !lastSensorHit)  // Now true, was false
  {
    LEDvalue = !LEDvalue; // Toggle the LED
    digitalWrite(ledPin1, LEDvalue);
  }
  lastSensorHit = sensorHit;
  delay(100);
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

some variation.

Code:
#include <CapSense.h>

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

int state = LOW;
int count = 1;

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

   pinMode(ledPin1, OUTPUT); // leds add this line
}

void loop()                   
{   
  long start = millis();   
  long total1 =  cs_4_2.capSense(30);
 
  if (total1 > 3100 && state == LOW)
  {
    state = HIGH;
    digitalWrite(ledPin1, !digitalRead(ledPin1));   // toggle the pin only when the state goes from low to high
  }
  if (total1 < 1500 && prevState == HIGH)   // only set the state low when the cap sense is really lower
  {
    state = LOW;
  }
  Serial.println(total1);
  delay(100);
}

<please continue the discussion on the forum>
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yupii smiley-grin:D thank you so much Johnwasser and Robtillaart ...code works great,i've been with this problem for a long time,i was getting a conflict because of the library,but with your help it is solved.Thank you again guys for help smiley-wink
                                                                                                                                                                  Juycce
Logged

Pages: [1]   Go Up
Jump to: