likely an easy fix

i am new at coding and arduino, i have purchased the starter kit and an accessory pack, and have started on my own with making projects. however, this seemingly simple task has me stumped. basically i am trying to get a relay to energize if it is over the midway point of value on the potentiometer. it is just kind of a project i thought would help me better understand what i am doing and further my skill. however, for the life of me, i cant get it to power the relay on and off based on the value it is outputting. i am familiar with electrical but not on the electronics level (almost all high voltage AC) and i think it is in my coding. no idea where i am screwing up at, any help is appreciated.

const int relayPin = 5;
int analogPin = 2;
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
pinMode(analogPin, INPUT);
Serial.begin(9600);

}
void loop() {

// put your main code here, to run repeatedly:
if((analogPin) > 512) digitalWrite(relayPin, HIGH);
else if ((analogPin) < 513) digitalWrite(relayPin, LOW);

int sensorReading = analogRead(A2);
Serial.println(sensorReading);
delay(10);
}

Please do not cross-post. Other thread removed.

This serves no useful purpose. Remove it...

pinMode(analogPin, INPUT);

Given this...

int analogPin = 2;

...and the fact that you never change the value of analogPin this...

if((analogPin) > 512) digitalWrite(relayPin, HIGH);  
else if ((analogPin) < 513) digitalWrite(relayPin, LOW);

...is equivalent to this...

if(2 > 512) digitalWrite(relayPin, HIGH);  
else if (2 < 513) digitalWrite(relayPin, LOW);

...which can be further reduced to this...

if (2 < 513) digitalWrite(relayPin, LOW);

...then this...

digitalWrite(relayPin, LOW);

Now that you see what the compiler sees are you able to understand what is wrong?

im sorry, im not sure i see what it sees, you reduced it down, however, i am unable to see this. would it be advisable to to set sensor low and high values? i may need to clarify, that i am using a 10k potentiometer to crudely simulate a temperature. and i am at the moment, trying to energize the relay based on the resistance value.

const int relayPin = 5;
int analogPin = 2;
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
pinMode(analogPin, INPUT);
Serial.begin(9600);

}
void loop() {
analogPin = analogRead;
// put your main code here, to run repeatedly:
if((analogPin) > 512) digitalWrite(relayPin, HIGH);
else if ((analogPin) < 513) digitalWrite(relayPin, LOW);

int sensorReading = analogRead(A2);
Serial.println(sensorReading);
delay(10);
}

this is what i have done, it doesn't work, however, i am not looking for an answer. i don't want to know the code i want to understand. where in my logic am i failing? i appreciate you not "cheating" me out of this learning experience but i am still stuck...

thank you.

From your original post...

int sensorReading = analogRead(A2);
Serial.println(sensorReading);

Does that snippet work? Are numbers showing up in Serial Monitor?

yes, values from 0-1023 show up in moniter. which is part of why i am confused. and why i dont think i am coupling the number from the raw read, to the integer of the "if" statement.

Hi,

Can you post your original code that @Coding Badly refers to please, it has gone missing in the removal of the other posting?

Thanks.. Tom.. :o

TomGeorge:
Can you post your original code that @Coding Badly refers to please...

I'm working against the code in the original post.

Are you referring to the over-quote I removed?

Mooshman812:
yes

Code duplicated here for convenience...

int sensorReading = analogRead(A2);
Serial.println(sensorReading);

Do your best to break that code into the smallest possible pieces then describe what each piece does. Hint: You should end up with at least three pieces.

Hi,

[quote author=Coding Badly date=1515311452 link=msg=3552900]
I'm working against the code in the original post.

Are you referring to the over-quote I removed?[/quote]
This code.
OPs other code;

const int relayPin = 5;
int analogPin = 2;
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode(relayPin, OUTPUT);
pinMode(analogPin, INPUT);
Serial.begin(9600);

}
void loop() {

// put your main code here, to run repeatedly:
if((analogPin) > 512) digitalWrite(relayPin, HIGH);  
else if ((analogPin) < 513) digitalWrite(relayPin, LOW);

int sensorReading = analogRead(A2);
Serial.println(sensorReading);
delay(10);
}

Okay.. Tom.. :slight_smile:

People are trying to get you to compare these two lines. The first one does what you expect. The second does not

int sensorReading = analogRead(A2);
if((analogPin) > 512)

In the second line you are comparing the pin number when you should be comparing the analog reading from that pin.

...R

I think i finally see what you were trying to show me,

if((analogPin) > 512) digitalWrite(relayPin, HIGH);  
else if ((analogPin) < 513) digitalWrite(relayPin, LOW);

i was essentially telling the compiler that would always write low because analogPin will never be greater than 513.

Mooshman812:
i was essentially telling the compiler that would always write low because analogPin will never be greater than 513.

To be more pointed, 2 will never be greater than 513.

Robin2:
People are trying to get you to compare these two lines. The first one does what you expect. The second does not

int sensorReading = analogRead(A2);
if((analogPin) > 512)

In the second line you are comparing the pin number when you should be comparing the analog reading from that pin.

...R

thank you, as soon as i read this it clicked, now i am able to control the relay correctly.

if i am understanding you correctly..

int sensorReading = analogRead(A2);
// this is creating a value, called sensorReading, that is correlating to the value read by pin A2
Serial.println(sensorReading);

// this is telling the compiler to display the value of sensorReading in the serial monitor, vertically, because "ln"

but I'm not really sure how to break the code down any more than that.
and thank you for helping me really have to think and understand what i am doing. that's exactly what i want.

Yes, you understand correctly.