Hi , I am just new here , I want to be able to trigger a relay once the potentiometer has the value of 3.3 V. Below that value should be off. At this point I don't know if is possible , and haw to right the lines . thanks
if (analogRead(A0)>= 676){
digitalWrite (relayPin, HIGH or LOW as needed);
}
else {
digitalWrite (relayPin, LOW or HIGH as needed);
}
676 comes from 3.3V/(5V/1024)
int relayPin = 12;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
//if (analogRead(A0)>= 676){
digitalWrite (relayPin, LOW );
//if (analogRead(A0)<=676)
digitalWrite (relayPin, HIGH );
}
Thanks. I did it like this , but is not working.
edward690:
but is not working.
You've got half the code //'d out, that's probably why.
ok , thanks. can u give an idea on how the other half should be ?
The // is a comment as (I hope) you know.
So these lines are as if they weren't there:
//if (analogRead(A0)>= 676){
//if (analogRead(A0)<=676)
The result of that is that these two lines:
digitalWrite (relayPin, LOW );
digitalWrite (relayPin, HIGH );
.... happen (unconditionally) faster than you can imagine in the never-ending loop(). So the pin switches too fast to do anything to the relay, so you don't see any effect, but the pin will be changing.
If you take the // out of those lines above, returning them to service, and basically get back to the code CrossRoads posted, it should work. He used an else whereas you used a 2nd explicit "if", but that doesn't really matter.
thanks for that , I have it , is working . But I have a different prob now : sims like the curent on pin 12 is not strong enough to open a transistor( I am using a PN 2222A )