How to Switch colors of an RGB LED with two Piezo-sensor

Hello, I’m trying to modify the lights of an RGB LED with two piezo-sensor.
I Would like that when I don’t touch either of the piezo or I touch both the light will stay on Blu, when I touch one of the piezo would switch on green and with the other on Red.
Unfortunately It seems I’ve wright the code wrong because it’s not working correctly… ( It works with the first piezo but then it turns the colors in the opposite way, or mix it together with the blu light)
Could someone help me with this?

Thank You!!!

const int PiezoGreen = 2;
const int PiezoRed = 7;

const int GreenLED = 9;
const int RedLED = 10;
const int BluLED = 11;

int PiezoGreenState = 0;
int PiezoRedState = 0;


void setup ()
{
pinMode (PiezoGreen, INPUT);
pinMode (PiezoRed, INPUT);

pinMode (GreenLED, OUTPUT);
pinMode (RedLED, OUTPUT);
pinMode (BluLED, OUTPUT);
}


void loop () {

PiezoGreenState = digitalRead(PiezoGreen);
if (PiezoGreenState == HIGH){
  
  digitalWrite (GreenLED, HIGH);
}
else if (PiezoGreenState == LOW) {
  
  digitalWrite (GreenLED, LOW);
}

PiezoRedState = digitalRead(PiezoRed);
if (PiezoRedState == HIGH){
  
  digitalWrite (RedLED, HIGH);
}

else if (PiezoRedState == LOW) {
  
  digitalWrite (RedLED, LOW);
}


if (PiezoGreenState == PiezoRedState)

{  
  digitalWrite (BluLED, HIGH);
}
  
  else
{
  digitalWrite (BluLED, LOW);

}
}

Just a small thing, but if the result of a digitalRead is not HIGH, why bother testing to see if it is LOW?

You're right I tried to write the code more properly, but still works in the same way...

const int PiezoGreen = 4;
const int PiezoRed = 7;

const int GreenLED = 9;
const int RedLED = 11;
const int BluLED = 10;




void setup ()
{
pinMode (PiezoGreen, INPUT);
pinMode (PiezoRed, INPUT);

pinMode (GreenLED, OUTPUT);
pinMode (RedLED, OUTPUT);
pinMode (BluLED, OUTPUT);
}


void loop () {

if (digitalRead(PiezoGreen) == HIGH){
  
  digitalWrite (GreenLED, LOW);
}
else if (digitalRead(PiezoGreen) == LOW) {
  
  digitalWrite (GreenLED, HIGH);
}

if (digitalRead(PiezoRed) == HIGH){
  
  digitalWrite (RedLED, HIGH);
}
else if (digitalRead(PiezoRed) == LOW) {
  
  digitalWrite (RedLED, LOW);
}


if (digitalRead(PiezoGreen) == digitalRead(PiezoRed))

{  
  digitalWrite (BluLED, HIGH);
}
  
  else if (digitalRead(PiezoGreen) != digitalRead(PiezoRed))
{
  digitalWrite (BluLED, LOW);

}
}

I tried to write the code more properly,

No you did not get rid of the second test like this:-

void setup ()
{
pinMode (PiezoGreen, INPUT);
pinMode (PiezoRed, INPUT);

pinMode (GreenLED, OUTPUT);
pinMode (RedLED, OUTPUT);
pinMode (BluLED, OUTPUT);
}


void loop () {

if (digitalRead(PiezoGreen) == HIGH){
  digitalWrite (GreenLED, LOW);
}
else {
  digitalWrite (GreenLED, HIGH);
}

if (digitalRead(PiezoRed) == HIGH){
    digitalWrite (RedLED, HIGH);
}
else  {
  
  digitalWrite (RedLED, LOW);
}


if (digitalRead(PiezoGreen) == digitalRead(PiezoRed))

{  
  digitalWrite (BluLED, HIGH);
}
  
  else 
{
  digitalWrite (BluLED, LOW);

}
}

Now you say:-

but then it turns the colors in the opposite way, or mix it together with the blu light)

Because that is the way you have written it.

if (digitalRead(PiezoGreen) == HIGH){
          digitalWrite (GreenLED, LOW);
// is the opposite way round to
if (digitalRead(PiezoRed) == HIGH){
    digitalWrite (RedLED, HIGH);

The blue light will only be off if one or the other Piezo is on otherwise the blue light is on if both Piezos are on or both Piezos are off.

void loop () 
{
  int greenState = digitalRead(PiezoGreen);
  int redState = digitalRead(PiezoRed);
  
  digitalWrite (GreenLED, !greenState);

  digitalWrite (RedLED, redState);
  
  digitalWrite (BluLED, greenState == redState);
}

Still doesn’t work… I tried use your code, but the problem remain the same, it works first with one piezo, then when I try to push the other one it goes in different colors, from purple to red, and never goes green, I rechecked the circuit and the code but it’seems everything ok :disappointed_relieved:

Are you expecting these to latch on?

Still doesn't work..

Yes it does, it works exactly as you have written it. :-

If the green sensor is off the green light is on
If the green sensor is on the green light is off

If the red sensor is off the red light is off
If the red sensor is on the red light is on

If the red sensor is off and the green sensor is off the blue light is on
If the red sensor is on and the green sensor is on the blue light is on

If the red sensor is off and the green sensor is on the blue light is off
If the red sensor is on and the green sensor is off the blue light is off

So that is what it does. Please tell us if it is not doing this, otherwise what do you want it to do?

No, It does not follow the logic of the code.
It works the same with all the code I’ve try till now: It switch colors automatically from green to red or even to purple while touching more time the piezos…

I attached a Video to understand this better…

Video.mov (2.52 MB)

How have you wired those sensors up?
I can’t see a pull up resistor anywhere. If you haven’t got one then your inputs are floating and all bets are off as to what they return.
See this by printing the sensor readings to the serial monitor.

Yes, there I'm not using resistors for the Piezos because when I used them (connecting 1M ohm resistor in parallel as here (http://arduino.cc/en/Tutorial/Knock ) the code basically works good but the Led just stays on just for a while, so I thought the problem Where the resistors..

Have you tried a serial print of the sensors's output yet? the problem was that 1M was too high to discharge the voltage from the sensors or to act as a good enough pull down resistor.
These are not touch sensors anyway, the convert change in stress on the crystal into a voltage.

I've tried with every resistors I've on the starter kit.. it just work with the 1M ohm resistor.. but just for a millisecond.. while without resistors I still have the same problem.. but the Serial.print write down something strange: constantly 1.63 volts for one Piezo.. and for the other one first 0 and than 5.0 volt... I'm trying to figure it out with potentiometers but still not getting the point..

t just work with the 1M ohm resistor.. but just for a millisecond

Yes that is how those sensors are supposed to work. I think you are expecting then to do something else. They are not touch sensors you get a very quick spike when they experience a mechanical shock.

You could try a small capacitor across the sensors like a 0.1uF to extend the pulse but you are better off extending it in software.

but the Serial.print write down something strange:

Yes that is exactly why you are seeing the light behave like you are. As I said before you have a floating input and that means it could pick up anything.

Yes! you were right I used the 0.1uF capacitors and everything works perfectly! They stabilized the voltage so the circuit It's working as I wanted .. The lights stays on as long as I press the Piezo!
Thank you so much .. I'll post something about the whole work when I'll finished it!