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