Pages: [1]   Go Down
Author Topic: capacitive sensor  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to get my capacitive sensor to turn the brightness of an LED up and down. It is giving me the correct output 0-255 because I have debugged it.

when I set:

analogWrite(lightPin, newVal);

to:
analogWrite(lightPin, 128);

it gives it about half brightness, which is correct. So I am very confused why the variable wont translate to brightness. any ideas?

Code:
#include <CapSense.h>

CapSense cSensor(4,2);


int lightPin = 13;
int buttonPin = 3; 

void setup() {
 
    Serial.begin(9600);
    pinMode(lightPin, OUTPUT);
    pinMode(buttonPin, INPUT);
}

void loop() {

    int val = digitalRead(buttonPin);
    long sensor = cSensor.capSense(30);

    long newVal = (sensor/1000)*20;
    Serial.print(newVal);
    Serial.print(", ");
      analogWrite(lightPin, newVal);   
    if (newVal < 255 && newVal > 20 && val == 0 ) {

    } else {
      digitalWrite(lightPin, LOW);  // turn LED OFF
    }
}

there is also a button too in case you're wondering, and yes it is on smiley
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26274
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try using one of the pins that supports analogWrite.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

try it without the if, it can set the LED allways off (too fast to see)

Code:
void loop() {

    int val = digitalRead(buttonPin);
    long sensor = cSensor.capSense(30);

    long newVal = (sensor/1000)*20;
    Serial.print(newVal);
    Serial.print(", ");
      analogWrite(lightPin, newVal);   
}
Logged

Rob Tillaart

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

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try using one of the pins that supports analogWrite.

doh. that was it. totally forgot some pins don't support analog signals. thanks.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Pages: [1]   Go Up
Jump to: