RGB LED lamp with tilting

Hello everyone :slight_smile: 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 :smiley: 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 :blush:

Hi, when you check for single button HIGH, you must also check that the other 2 are LOW you cannot assume it.
Likewise when you check for two buttons HIGH, you must also check the the other button is LOW.

Hope this helps...
Tom...... :slight_smile:

On breadboard? or somehow else in code? :smiley:

ksenias:
On breadboard? or somehow else in code?

It's all in code.

else if (buttonState1 == HIGH && buttonState3 == HIGH && total1>200) {

Hi, here you are checking to see but1 and but3 are HIGH, you should also have to check if but2 is low, I'm not sure how your tilt switches are oriented you may be able to have all 3 HIGH I don't know.

When you check but1 for HIGH, you should also check but2 and but3 for LOW. You are only checking but1 and assuming but2 and but3 LOW.

So you must modify all three lines where you check but1 , but2 and but3 of HIGH only.

Okay, just change your code the one you have listed first.

Tom....... :slight_smile:

Ohhh. I see :slight_smile: tilt switches form kind of triangle, only 2 can be high at same time, or 1.
That's one Long long code, hehe... But it worked O_O Thank you! Need just to rearrange tilt switches so they really for triangle. It is now hard to make each versions "2 active tilts -on" work.

How can i shorten it? Like, all the action starts only if Sensor works, can i put it somewhere in If at the beginning, so there won't be any need to write it each time for each button state?

#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);    

  buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);


  if (buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && total1>200) {     
    digitalWrite(red, LOW);  
        digitalWrite(green, HIGH);  
         digitalWrite(blue, HIGH);  
  } 
  else  if (buttonState2 == HIGH && buttonState1 == LOW && buttonState3 == LOW &&total1>200) {
    digitalWrite(red, HIGH); 
    digitalWrite(green, LOW); 
        digitalWrite(blue, HIGH); 
  }     
         else  if (buttonState3 == HIGH && buttonState1 == LOW && buttonState2 == LOW && total1>200) {
    digitalWrite(red, HIGH); 
    digitalWrite(green, HIGH); 
        digitalWrite(blue, LOW); 
  }
    else  if (buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == LOW && total1>200) {
    digitalWrite(red, LOW); 
    digitalWrite(green, LOW); 
        digitalWrite(blue, HIGH); 
  }     
    else  if (buttonState2 == HIGH && buttonState3 == HIGH && buttonState1 == LOW && total1>200) {
    digitalWrite(red, HIGH); 
    digitalWrite(green, LOW); 
        digitalWrite(blue, LOW); 
  }     
    else  if (buttonState1 == HIGH && buttonState3 == HIGH && buttonState2 == LOW && total1>200) {
    digitalWrite(red, LOW); 
    digitalWrite(green, HIGH); 
        digitalWrite(blue, LOW); 
  }     
  
    else  if (

Another approach is to set the brightness of each LED separately, without worrying about the other two. So button1 would control RED, button2 would control GREEN, and button3 would control BLUE. You really don't care what button2 and button 3 are doing when you decide whether to turn on the RED LED or not.

if (buttonstate1) {
   turn RED on
}
else {
   turn RED off
}

Just do the same for each switch/LED combination.

Best of all would be to use an accelerometer and set the brightness with PWM.