Go Down

Topic: capacitive switch variable (Read 767 times) previous topic - next topic

juycce

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: [Select]

#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...  :)

johnwasser

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

Code: [Select]

#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);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

robtillaart

some variation.

Code: [Select]

#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>
Rob Tillaart

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

juycce

Yupii :D: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 ;)
                                                                                                                                                                  Juycce

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy