How can i make this relay be turned on by any of the 3 touch sensors i have?

I have a relay and i want to turn on the light with it and i have 2 touch sensors but with this code i can only turn it on with 1 how can i make it work? The third is a switch but it should still work the same. I've tried and it worked with a different code. But that code was for a servo and not a relay. Can you send the full code if you know how to make it work?

int touchPin = 2;
    int relayPin = 3;
    
    int val = 0;
    int lightON = 0;
    int touched = 0;
    
    void setup() {
      Serial.begin(9600);
      pinMode(touchPin, INPUT); 
      pinMode(relayPin, OUTPUT);
    
    }
    
    void loop() {
    
      val = digitalRead(touchPin);
    
      if(val == HIGH && lightON == LOW){
    
        touched = 1-touched;
        delay(100);
      }    
    
      lightON = val;
    
          if(touched == HIGH){
            Serial.println("Light ON");
            digitalWrite(relayPin, LOW); 
           
          }else{
            Serial.println("Light OFF");
            digitalWrite(relayPin, HIGH);
       
          }     
    
      delay(100);
    }

Each touch sensor requires its own pin.
Please post a schematic of your hardware.
What are these touch pins, is there an IC involved?

How exactly do you want this to work?
If the light is already on do you want the next touch to extend the on period or is the on period set from the first touch?
Your code seems quite complex for just a simple thing.

I have 3 TTP223B touch modules arduino uno and 5v relay
here are the pins

int touchPin   = 4;
int touchPin2   = 6;   // Arduino pin connected to touch sensor's pin
int touchPin3   = 7 ;
int relayPin = 9;

I want it to work like when i press 1 touch moduel and it goes on HIGH
and then press another touch moduel and it goes on LOW

A broad outline :

byte val0 = digitalRead(pinNumber0);
byte val1 = digitalRead(pinNumber1);
byte val2 = digitalRead(pinNumber2);
if (val0  ==  HIGH || val1 == HIGH || val2 == HIGH)
{
  //code here to turn on the light
}
else
{
  //code here to turn off the light
}

Can you send the whole code please?
Im not sure if i can make it work correctly

Give it a try and if you have problems post your sketch here and describe what is wrong

This is the code i used. The relay turned on but when i pressed the touch sensors it didnt turn off

int touchPin2 = 6;
int touchPin3 = 7;
int touchPin = 4;
    int relayPin = 9;
    
    int val = 0;
    int lightON = 0;
    int touched = 0;
    
    void setup() {
      Serial.begin(9600);
      pinMode(touchPin, INPUT); 
      pinMode(relayPin, OUTPUT);
    
    }
    
    void loop() {
    
byte val0 = digitalRead(touchPin);
byte val1 = digitalRead(touchPin2);
byte val2 = digitalRead(touchPin3);
if (val0  ==  HIGH || val1 == HIGH || val2 == HIGH)
{
            Serial.println("Light ON");
            digitalWrite(relayPin, LOW); 
}
else
{
            Serial.println("Light OFF");
            digitalWrite(relayPin, HIGH);
}{
   
    
      delay(50);
}    }

Try printing the 3 values before you test them. Are they what you expect ?

Does the relay turn on when its input is HIGH or when it is LOW ?

How?
The relay just stays on if i press the touch sensor it still says on
it doesnt turn off or on when i press the touch sensor

Hello
Did you power up the touch switches ?

yes they have power and the led on them is on too

Still no schematic as requested.
You would make answering all your questions so simple. Why do you not post one?
If you say you haven’t got one then make one, it is the only way to communicate electronics information that is easy to understand.

Do you mean how do you print values? It is by using the serial print statements you are already using, only put in the variable name in place of a string.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.