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.
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
}
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.