SeanKile:
Thanks for your help guys, sorry about not doing things the exact right way. I’m 15 and this is the first electronics/coding project I’ve been involved with. That said my school doesn’t have any teachers that can help me and no one that I know even knows what an Arduino is so I’m kind of on my own except for you guys. I’ll get back to you with how the code works.
Try not to let the miserable curmudgeons here get you down. They too started, at some point, not knowing shit. Over time they seem to have forgotten this and think it's acceptable to ridicule and condescend anyone trying to learn. Not sure why they bother frequenting the forum, TBH...
This slightly modified version of your code should work:
const int touchsensorR = 6; // Digital Output reads signal from right KY-036
const int touchsensorL = 5; // Digital Output reads signal from left KY-036
const int relay5 = 7; // Signal for Relay
void setup ()
{
pinMode (touchsensorR, INPUT); // This for reading the digital signal on the right side
pinMode (touchsensorL, INPUT); // This for reading the digital signal on the left side
pinMode (relay5, OUTPUT); // This is to switch the relay on, sending power to the solenoids
Serial.begin (9600); // Serial output with 9600 bps
}//setup
void loop ()
{
if( digitalRead (touchsensorR) == HIGH && digitalRead (touchsensorL) )
digitalWrite (relay5, HIGH);
else
digitalWrite (relay5, LOW);
}//loop
You were almost there. A simple misunderstanding about pin naming and about how variables work. Each time you read a sensor into "val" (which you hadn't declared, BTW) you basically erased the last value in that variable:
val = digitalRead (touchsensorR) ;
val = digitalRead (touchsensorL) ;
if (val == HIGH)
This would actually result in the relay turning on if only the left sensor was touched. You read the right into val and then immediately overwrite that with the read of the left sensor. Your test is only looking at the value returned from the left side.
There are a number of ways to get around this. The simplest is to assign each its own variable and then compare both:
int
leftSensor,
rightSensor;
.
.
.
rightSensor = digitalRead (touchsensorR) ;
leftSensor = digitalRead (touchsensorL) ;
if( rightSensor == HIGH && leftSensor == HIGH )
{
.
.
.
}
If you don't need to remember the values of the sensors -- you just need to know their state at that moment, for example -- you can dispense with the variable storage and just look at the pins directly:
if( digitalRead (touchsensorR) == HIGH && digitalRead (touchsensorL) )
As you begin to get more experience you'll learn why you might want to remember those values.
For example, suppose someone just brushes the sensors for a half-second? The code would see the touches and immediately turn the relay on. Maybe that's not good (or safe.) Maybe you want not just for the sensors to be touched but to be touched for, say, 2-seconds minimum before turning on the relay. Remembering values in cases like that can be useful.