Hello everyone
I have a simple "art" project with little lamp. It starts glowing when you touch-hold it and changes colors if tilted. Tools: 1 RGB LED (1 anode), 3 tilt sensors, 1 capacitive sensor. Problem: LED does not mix colors if 2 tilt switches are active, just makes LED white.
Here's how i made own code. Bu-ut it does not mix colors if 2 tilts are active.
#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
const int buttonPin1 = 8; //tilt1
const int buttonPin2 = 12; // tilt22
const int buttonPin3 = 13; // tilt3
const int red = 9;
const int green = 10;
const int blue = 11;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
void setup() {
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
void loop(){
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
Serial.print(millis() - start);
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH && total1>200) {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}
else if (buttonState2 == HIGH && total1>200) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
}
else if (buttonState3 == HIGH && total1>200) {
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
}
else if (buttonState1 == HIGH && buttonState2 == HIGH && total1>200) {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
}
else if (buttonState2 == HIGH && buttonState3 == HIGH && total1>200) {
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}
else if (buttonState1 == HIGH && buttonState3 == HIGH && total1>200) {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
}
else if ( total1>200) {
digitalWrite(red, LOW);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
}
else {
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}
}
...later i was friendly given following code, but here it Only works if tilted. I'd like to have it glowing without tilting too
But have no idea where to insert such part of a code here (still newbie, just learning. Millis is something unknown for me)
#include <CapacitiveSensor.h>
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
#define buttonPin1 8 //tilt1
#define buttonPin2 12 // tilt22
#define buttonPin3 13 // tilt3
#define red 9
#define green 10
#define blue 11
byte btns[3] = {buttonPin1, buttonPin2, buttonPin3};
byte leds[3] = {red, green, blue};
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
for(byte i = 0; i < 3; i++) pinMode(leds[i], OUTPUT);
}
void loop()
{
unsigned long start = millis();
unsigned long total1 = cs_4_2.capacitiveSensor(30);
Serial.print(millis() - start);
if (total1 > 200)
for(byte i = 0; i < 3; i++)
digitalWrite(leds[i], !digitalRead(btns[i]));
else
for(byte i = 0; i < 3; i++)
digitalWrite(leds[i], 1);
}
Thanks if you help ![]()