Arduino won't recognise 5V input, driving me mental!!

Be nice, I am new to all this LOL
I am having more Arduino issues!!

I am building a bullet case annealing machine.

I have decided to try using a IR switch from work to sense the presence of a brass case on a set of rollers, seeing as it is a finished unit.
It is 12V, and it switches a relay that will provide 5V to digital pin 1, when there is case present.

Problem is, the program runs, as if there is not IF statement.

I run the serial monitor on the state of pin 1, and it shows “xxx”, or does’t print at all, depending on the switch state……

HELPS! I am such a NOOB

//Brass case annealer coding.
//utilises 12v geared head motor for case feeder, and a servo to spit the case out.
//also uses 12v IR switch and relay to provide input for case presence

int current_potent = A0; //potentiometer
int sensor_ir_pin = 1; //relay from IR switch connected to pin 1
int val = 0; //value to store the digitalRead from the relay on pin 1
int pos = 0; // variable to store the servo position

const int channel_a_enable = 6; //feeder motor enabling
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;

#include <Servo.h>;
Servo casespit; // create servo object to control a servo

void setup()
{
Serial.begin(9600);
pinMode (current_potent, INPUT); //potentiometer as an input
pinMode (sensor_ir_pin, INPUT); //turns pin 1 to an input from the relay with IR switch

pinMode( channel_a_enable, OUTPUT ); // Channel A enable
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

casespit.attach(9); // attaches the servo on pin 9 to the servo object (casespit)
}

void loop()
{
analogWrite( channel_a_enable, 255); //turns feeder motor on, to feed brass case
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);

val = digitalRead (sensor_ir_pin); //reads IR switch and checks if it has a case present yet, by 5V present on pin 1

Serial.println (val); //prints value of val to serial monitor to see if the signal from the relay is being detected as HIGH

if (val = HIGH) //if brass case rolls past sensor, switching IR switch and relay, val will = HIGH

analogWrite( channel_a_enable, 0); //turns feeder motor off
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);

current_potent = analogRead(A0); //reads the potentiometer setting between 0 and 1023

delay(current_potent * 14.6627); //delays, while the brass case is in the flame, getting annealed. also provides the multiplier for 15000 mill seconds into 1023 read point on the potentiometer

{
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
casespit.write(pos); // tell servo to go to position in variable ‘pos’
delay(5); // waits 5ms for the servo to reach the position
}

for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
casespit.write(pos); // tell servo to go to position in variable ‘pos’
delay(5); // waits 5ms for the servo to reach the position
}
}

}

Don’t use pin 0 or pin 1.
Use pin 2 or higher.

 if (val = HIGH)      //if brass case rolls past sensor, switching IR switch and relay, val will = HIGH
  
  analogWrite( channel_a_enable, 0);              //turns feeder motor off

= is asssignment, comparison is ==.

You should really put braces around blocks of code in if or for blocks even if there is only one line like here. It makes things easier to read and if you come back and decide that something else should happen if val is HIGH besides this one analogWrite, you can just add the code instead of having to go put braces in. Putting braces in code that’s already written can be a nightmare sometimes.

I have changed the pin to number 13, so I get an LED signal when the switch gets tripped. So I know there is 5V on the pin, as the LED on the board lights up.....So the board is definitely getting 5V from the relay.

Also, I chucked curly brackets in the IF statement, so help segregate it. I think I did it right, like I said, this is all very new.

I tried those, and verified then uploaded.....no difference.

Please post your amended code.

Sorry, that would be the smart thing to do LOL

//Brass case annealer coding.
//utilises 12v geared head motor for case feeder, and a servo to spit the case out.
//also uses 12v IR switch and relay to provide input for case presence

int current_potent = A0; //potentiometer
int sensor_ir_pin = 13;
int val = 0; //sensor the see when the cse is infront of it, running through a relay
int pos = 0; // variable to store the servo position

const int channel_a_enable = 6; //feeder motor enabling
const int channel_a_input_1 = 4;
const int channel_a_input_2 = 7;

#include <Servo.h>;
Servo casespit; // create servo object to control a servo

void setup()
{
Serial.begin(9600);
pinMode (current_potent, INPUT);
pinMode (sensor_ir_pin, INPUT);

pinMode( channel_a_enable, OUTPUT ); // Channel A enable
pinMode( channel_a_input_1, OUTPUT ); // Channel A input 1
pinMode( channel_a_input_2, OUTPUT ); // Channel A input 2

casespit.attach(9); // attaches the servo on pin 9 to the servo object (casespit)
}

void loop()
{
analogWrite( channel_a_enable, 255); //turns feeder motor on, to feed brass case
digitalWrite( channel_a_input_1, HIGH);
digitalWrite( channel_a_input_2, LOW);

val = digitalRead (sensor_ir_pin); //reads IR switch and checks if it has a case present yet
Serial.println (val);
{
if (val == HIGH) //if brass case rolls past sensor, switching IR switch and relay

analogWrite( channel_a_enable, 0); //turns feeder motor off
digitalWrite( channel_a_input_1, LOW);
digitalWrite( channel_a_input_2, LOW);

current_potent = analogRead(A0); //reads the potentiometer setting (set for a max of 15 seconds time)

delay(current_potent * 14.6627); //delays, while the brass case is in the flame, getting annealed. also provides the multiplier for 15000 mill seconds into 1023 read point on the potentiometer
}
{
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
casespit.write(pos); // tell servo to go to position in variable ‘pos’
delay(5); // waits 15ms for the servo to reach the position
}

for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
casespit.write(pos); // tell servo to go to position in variable ‘pos’
delay(5); // waits 15ms for the servo to reach the position
}
}

}

{
  if (val == HIGH)                      //if brass case rolls past sensor, switching IR switch and relay
  
  analogWrite( channel_a_enable, 0);              //turns feeder motor off
  digitalWrite( channel_a_input_1, LOW);
  digitalWrite( channel_a_input_2, LOW); 
  
  current_potent = analogRead(A0);         //reads the potentiometer setting (set for a max of 15 seconds time)
  
  delay(current_potent * 14.6627);         //delays, while the brass case is in the flame, getting annealed. also provides the multiplier for 15000 mill seconds into 1023 read point on the potentiometer
}

Close, but the curly brace goes after the if. Like this:

if (val == HIGH) {

  //  this code is conditional

}

Also notice how i put the code in code tags for the forum. Makes it easier to read. it’s the little icon up there above where you write that looks like a scroll with a pair of < and > on it.

ITS FIXED!!!!!

It was the stupid curly bracket!! I placed it as you said Delta_G. something so simple, it was starting to get under my skin. That will be a lesson learnt

Congratulations:

2014-12-29_20-44-21.gif