Code help on my project

Hey guys, I am developing a unit to help run a machine. This machine basically has 12v ac solenoids that both need to be activated at the same time. I am using an ardiuno relay to control the power sent to these solenoids. However, the solenoids and therefore the relay can only be powered when I have two signals from two body touch sensors (KY-036). When both of these sensors are touched (they both have to be touch in order to activate the relay E.I. if one side is touched but the other is not then the relay should not be activated) I then need to send a signal to the relay which will power the solenoids. Also, once one or both of the sensors is no longer activated then the relay needs to be closed. I am very new to coding and don't know if anyone can help me but this is my code so far, I am getting hung up on the part where both sensors have to have a high value for the relay to be activated. Thanks

int touchsensorR = ~6; // Digital Output reads signal from right KY-036
int touchsensorL = ~5; // Digital Output reads signal from left KY-036
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
}

void loop ()
{
val = digitalRead (touchsensorR) ;
val = digitalRead (touchsensorL) ;
if (val == HIGH)

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

if (digitalRead(touchsensorR) == HIGH && digitalRead(touchsensorL) == HIGH) 
{
   digitalWrite (relay5, HIGH);
}
else
{
   digitalWrite (relay5, LOW);
}

I'm guessing you put the ~ in these lines:

int touchsensorR = ~6; // Digital Output reads signal from right KY-036
int touchsensorL = ~5; // Digital Output reads signal from left KY-036

.... becasue that's the way they are printed on the board?

But the effect of the ~ is to "not", ie negate, the value.

Run this simple code:

int touchsensorR = ~6;
int touchsensorL = ~5;

void setup()
{
  Serial.begin(9600);
  Serial.println(touchsensorR);
  Serial.println(touchsensorL);
} //setup

void loop()
{

} //loop

You will get this output:

-7
-6

(You can verify that in the Windows calculator set to Programmer; not(6) is -7, and not(5) is -6.

So I wonder with your pins set to -7 and -5, which ones it's actually reading?

At the top of the forum there's a thread to read before you post but why bother?

Maybe it tells how to post code so that html formatting doesn't screw it up?

Code tags [code]  ... [/code] go around code!

And right up top, what does ~6 and ~5 mean? If that compiles, it won't do what you likely think it will.
Whatever possessed you to throw ~ in? That's the bitwise NOT operator, int ~6 is binary 111111110101, a negative value, not a pin number at all.

// don't use int where a byte that can hold 0 to 255 will do while using half the RAM.
byte touchsensorR = 6; // Digital Output reads signal from right KY-036
byte touchsensorL = 5; // Digital Output reads signal from left KY-036
// byte touchsensorR = ~6; // Digital Output reads signal from right KY-036  just to show that you actually used ~6
// byte touchsensorL = ~5; // Digital Output reads signal from left KY-036
byte 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  // you never use serial so why put it in?
}
  
void loop ()
{
  byte val1 = digitalRead (touchsensorR) ;
  byte val2 = digitalRead (touchsensorL) ;

  if (val1 == HIGH && val2 == HIGH)  // && is logical AND, this is true if both are HIGH
  {
    digitalWrite (relay5, HIGH);
  }
  else
  {
    digitalWrite (relay5, LOW);
  }
}

This was a serious question by the way:

arduin_ologist:
So I wonder with your pins set to -7 and -5, which ones it's actually reading?

If you ask an Arduino to read non-existent pins, what does it do?

arduin_ologist:
This was a serious question by the way:

If you ask an Arduino to read non-existent pins, what does it do?

From wiring_digital.c (in arduino-1.8.5/hardware/arduino/avr/cores/arduino):

int digitalRead(uint8_t pin)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PIN) return LOW;

	// If the pin that support PWM output, we need to turn it off
	// before getting a digital reading.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}

The relevant line being:

if (port == NOT_A_PIN) return LOW;

.

Delta_G:
The relevant line being:

if (port == NOT_A_PIN) return LOW;

Thanks.

So there you have it, OP: your non-existent pins are reading LOW all the time.

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.

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.

You might post your need for a local person to help by posting in this sub-forum:https://forum.arduino.cc/index.php?board=28.0.

Paul

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.

SeanKile:
Thanks for your help guys, sorry about not doing things the exact right way.

No no. Your code has to be compiled before it can run. The compiler does not read minds and needs exacting instructions to do anything. So you need to spell everything exact which means you have to give enough to check and double-check and still go on after being wrong in different ways until it is as right as you need or want. In a big project you can be wrong 30 times before lunch between little quick to fix errors and dealing with actual bugs at least if you’re getting paid to do it or really want it done.

Do not self-identify with your code. You learn and grow on.

The reason I say to get the foundations down and the basics up is to save you from “hurting yourself” over simple stuff you should know but don’t. Get yourself nice solid footing in variables and arrays (grouped variables), logic structures, using functions and making your own and using char arrays to handle text, and never waste time coding String on Arduino, it will teach you bad habits that can lead to “why does my sketch crash?” threads.

By “hurting yourself” I mean you spending hours and hours making things work the hard way through massive typing just because you skipped on arrays and perhaps text. Remember learning to read and write? Imagine trying to write a book without learning --all-- of the alphabet or the actual meanings of most of the words you might spell right or wrong as an approach to becoming an author!

You’re at a great age to learn. One thing to learn is organize your learning, something the teachers and courses you take do for you but now you are outside of their box.

The Arduino site has all you need. Bookmark these pages, make an easy to get bookmark folder and add bookmarks as you go. When you have the Arduino IDE open, open a browser and a tab in it for every page you need to reference, have instant lookup of details.

https://www.arduino.cc/en/Tutorial/Foundations <<=== Arduino, Microcontroller and Programming Basics
Just be familiar and able to find your way back. After repeated use you won’t need to.
It gets you into the Reference Page.

https://www.arduino.cc/en/Tutorial/BuiltInExamples <<=== your IDE has these in the File->Examples menu.
You may not have all the parts to try every sketch. No problem, try the ones you can and see how they work.
You should mess with examples from sections 1,2,3 and 5 but not 4 and the rest are special cases. 1,2,3,5 teach a bit.
The libraries have examples in your IDE too. Library examples show working code you can use.

There are books out aimed at teaching Arduino to young people, the member Crossroads here has one on Amazon IIRC.

Have you heard of the MAKE movement and MAKE magazine? There’s a kids component and I am sure a whole raft of teaching materials.

The member Terry King here has a shop “Yourduino” with starter kit, other kits and pages of illustrated how-to explanations.

The physics teacher might be very interested in Arduino. Mention the 10-bit analog to digital pins, range is 0 to 1023. Mention the digital pins that can read digital sensors (IR light detector for example) incredibly fast. They can send signals, light leds, run motors as well. Events can be timed to within 4 microseconds up to over 70 minutes apart. If you sense a pendulum swinging past center it can take reads and tell how many reads the pendulum blocked a light and how long that took at around 67 to 81 times per millisecond depending on short prints to no prints, blink or fade the led to indicate speed.

It is hard to overstress the speed of these electronic machines. Most CPU instructions run on 1 or 2 cycles. There are 16000 cycles in an Arduino millisecond. 67 times a millisecond comes out to 239 cycles from one time to the next, enough to do a little bit and those bits add up at 67000 per second. But you need to learn the do-many-things-at-once lesson (the IDE example BlinkWithoutDelay is an example) to get that kind of performance. It’s a bit much, the 1st address in my signature space below has a nice simple explanation and code but you need to get the Foundations down to be able to read it.

So, I've came pretty far in the past week or so of working on this but I have a new problem. Basically, when I soldered onto the two KY-036 human touch sensors the connection to the wire I soldered is causing the sensors to always be activated. I thought maybe I could work around this by reading the analog values of the sensors instead of the digital and then using that (the sensors would read a constant value and then when I touch them they work read a higher value?). I couldn't find much information on how analog actually worked. Here's the very rough analog detection code I made:

void setup ()
{
  pinMode(A0, INPUT); //analog touch sensor input Left
  pinMode(A4, INPUT); //analog touch sensor input Right
  pinMode(11, OUTPUT); //relay output
}
  
void loop ()
{
  if( analogRead(A0) >> 5400 && analogRead(A4) >> 5400) //attemping to read the analog values
  digitalWrite(11, HIGH); // if both analog values increase then turn on the relay and keep on untill the increase goes away
  
  else
  digitalWrite(11, LOW); // if there's is no increase turn relay off
}

I thought analog could only be read in 0-1024 but for some reason when I put (value)>>5400 the relay was on and when I put (value)>5500 the relay was off. If anyone could help me with code guidance or possibly getting different components that would work better for my application that'd be fantastic.

analogRead(A0) >> 5400 && analogRead(A4) >> 5400

Did you mean

analogRead(A0) > 5400 && analogRead(A4) > 5400

BTW, analogRead(A0) has a range of 0-1023

SeanKile:
So, I've came pretty far in the past week or so of working on this but I have a new problem. Basically, when I soldered onto the two KY-036 human touch sensors the connection to the wire I soldered is causing the sensors to always be activated. I thought maybe I could work around this by reading the analog values of the sensors instead of the digital and then using that (the sensors would read a constant value and then when I touch them they work read a higher value?). I couldn't find much information on how analog actually worked. Here's the very rough analog detection code I made:

void setup ()

{
  pinMode(A0, INPUT); //analog touch sensor input Left
  pinMode(A4, INPUT); //analog touch sensor input Right
  pinMode(11, OUTPUT); //relay output
}
 
void loop ()
{
  if( analogRead(A0) >> 5400 && analogRead(A4) >> 5400) //attemping to read the analog values
  digitalWrite(11, HIGH); // if both analog values increase then turn on the relay and keep on untill the increase goes away
 
  else
  digitalWrite(11, LOW); // if there's is no increase turn relay off
}So, I've came pretty far in the past week or so of working on this but I have a new problem. Basically, when I soldered onto the two KY-036 human touch sensors the connection to the wire I soldered is causing the sensors to always be activated. I thought maybe I could work around this by reading the analog values of the sensors instead of the digital and then using that (the sensors would read a constant value and then when I touch them they work read a higher value?). I couldn't find much information on how analog actually worked. Here's the very rough analog detection code I made:

[code]
void setup ()
{
  pinMode(A0, INPUT); //analog touch sensor input Left
  pinMode(A4, INPUT); //analog touch sensor input Right
  pinMode(11, OUTPUT); //relay output
}
 
void loop ()
{
  if( analogRead(A0) >> 5400 && analogRead(A4) >> 5400) //attemping to read the analog values
  digitalWrite(11, HIGH); // if both analog values increase then turn on the relay and keep on untill the increase goes away
 
  else
  digitalWrite(11, LOW); // if there's is no increase turn relay off
}




[/code]

I thought analog could only be read in 0-1024 but for some reason when I put (value)>>5400 the relay was on and when I put (value)>5500 the relay was off. If anyone could help me with code guidance or possibly getting different components that would work better for my application that'd be fantastic.

is the right-bit-shift operator. More than is >

False premise leads to false conclusion.

Try just print the analog read values you get and leave the unknowns out of your conclusions.

pinMode(A0, INPUT); //analog touch sensor input Left
  pinMode(A4, INPUT); //analog touch sensor input Right

There is no need to set pinMode for analogue pins used as analogue inputs.

Okay so fixed the original part of my code, now I'm working on a code to the analog values.

int analogPin = A4; 
int val = 0;           

void setup()
{
  Serial.begin(9600);              //  setup serial
}

void loop()
{
  val = analogRead(analogPin);     // read the input pin
  Serial.println(val);             // debug value
}

When I click 'monitor' on my computer I'm getting values that look like this.
120
683
683
120
682
683
115
683
683
113
118
683
681
118
683
684
118
683
683
305
684
683
131
682
683
112
117
684
683
117
683
682

Wondering if this is the right way to do it.

It's hooked up to what? How? Is anything going on or is that a floating pin?

The pin, A4, on my uno has a wire going from it to the analog pin on the sensor. The sensor does have power and is lit up. I used the supplied wires in my kit.

How does that value change if you touch it?

I don't see anything wrong in the details I'm given. It's the ones I'm not that could be problems.