Hi,
I'm creating a capacitive touch lamp with a simple RGB LED, an arduino nano and the CapacitiveSensor library.
The lamp should change color when touching the capacitive touch pin.
My problem: On my computer it works fine, but when I try to power it over an USB adapter (usually used to charge my phone), the LED is flickering while I'm touching the sensor.
(It's flickering because the colors are changing very quickly, it should change the color once when touching and you should only be able to change again when releasing the sensor and touching again.)
I also tried it with an 9V battery, nothing happend when touching the sensor.
Is it something with the current (limit) of my usb adapter/the computer/the battery?
Can somebody help me?
Here's a photo of my circuit:
And here is my code:
(I'm using my own library RGBLED here, which contains built-in colors, so you cannot find it in the Library manager.)
//Including the libraries
#include <RGBLED.h>
#include <CapacitiveSensor.h>
//Uncomment this to enable serial debugging:
//#define DEBUG
//Initializing the RGBLED Library with the LED RGB pins
RGBLED rgb(3, 2, 4);
//Initialize the Touch sensor on pins 12 and 7
CapacitiveSensor touch = CapacitiveSensor(12, 7);
void setup() {
#ifdef DEBUG
//Debugging
Serial.begin(9600);
Serial.println("DEBUG\n\nTouchRGBlamp started.");
#endif
//Setting up library
rgb.r_pinmode();
//Make light bright on start
rgb.white();
}
bool touchedBefore = false;
//Light state (0 to 9)
int state = 0;
void loop() {
//Read capacitive sensor
long sensorValue = touch.capacitiveSensor(30);
//Is it touched?
bool touched = (sensorValue > 1000);
if (touched) {
if (!touchedBefore) {
//Got first touch
touchedBefore = true;
//Switch light
switchState();
}
} else {
//No touch
touchedBefore = false;
}
#ifdef DEBUG
//Print values to serial monitor:
Serial.print("Cap. Sensor: ");
Serial.println(sensorValue);
Serial.print("Touched, Touched before: ");
Serial.print(touched);
Serial.print(", ");
Serial.println(touchedBefore);
#endif
//Wait 10ms
delay(10);
}
void switchState() {
//Switch the Light state:
switch (state) {
case 0:
//Turn LED white
rgb.white();
break;
case 1:
//Turn LED red
rgb.red();
break;
case 2:
//Turn LED green
rgb.green();
break;
case 3:
//Turn LED blue
rgb.blue();
break;
case 4:
//Turn LED yellow
rgb.yellow();
break;
case 5:
//Turn LED orange
rgb.orange();
break;
case 6:
//Turn LED pink
rgb.pink();
break;
case 7:
//Turn LED dark pink
rgb.darkPink();
break;
case 8:
//Turn LED cyan
rgb.cyan();
break;
case 9:
//Turn LED off
rgb.off();
break;
}
//Set light state
state++;
if (state > 9) {
state = 0;
}
}
