Hi, i'm an arduino novice,
I was attempting modifying a code that allows me to use a pressure sensor and arduino uno to activate an external relay.
I wanted the relay to come on at a low threshold value and turn off at a high threshold value.
Can anyone suggest where i may have gone wrong or what might be out of place?
Any advice is appreciated.
// These constants won't change:
const int analogPin = A0; // pin that the sensor is attached to
const int relayPin = 13; // pin that the relay is attached to
const int thresholdlow = 400; // an arbitrary threshold level that's in the range of the analog input
const int thresholdhigh = 700; //
void setup() {
// initialize the relay pin as an output:
pinMode(relayPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is low enough, turn on the LED:
if (analogValue < thresholdlow) {
digitalWrite(relayPin, HIGH);
} else {
(analogValue > thresholdhigh);
digitalWrite(relayPin, LOW);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
As your code works right now, does the relay turn on when the analog pin reading is below the threshold and turn off when the reading is above the threshold? If so try swapping the digitalWrite(relayPin, HIGH) and digitalWrite(relayPin, LOW) and re-upload to the board. Let me know what happens.
Maybe some positive reinforcement instead of bashing him/her for a small rookie mistake will help lead the person to make the right steps. Simply pointing out mistakes and not pointing out where to look for help is not the way to help people.
If so, which one (post a link) and how did you wire it?
Fine. What actually happens? "Half works" implies that the relay is halfway on.
Use code tags when posting code, as explained in the "How to use this forum" post.
i plan to use a bosch MAP sensor; 5vcc,gnd, signal to A0. at the moment im just using a 10k pot until i can work out the code.
the relayPin is HIGH under thresholdlow value but when it reaches thresholdlow value relayPin goes LOW, i want the relayPin to stay HIGH from thresholdlow to thresholdhigh, hence it only does half what i want it to do.
i am new to this, i have searched through lots of code looking for something that may work, maybe i have seen it but i didnt understand what i was looking at, any help is appreciated.
Again, i'm trying to get the relayPin to switch HIGH at thresholdlow value and continue to run until it reaches thresholdhigh value, then relayPin goes LOW.
i think i may have gone wrong by writing the code with < > (less then or greater then), maybe i should write it to say when thresholdlow value is read keep relayPin HIGH until thresholdhigh is reached?
Again, i'm trying to get the relayPin to switch HIGH at thresholdlow value and continue to run until it reaches thresholdhigh value, then relayPin goes LOW.
If I understand this correctly, you need two if statements. The following code does that, but does not cover all the possible cases.
What state is the relay supposed be in if the input is always between "thresholdlow" and "threshholdhigh"?
What state should the relay be in upon program startup? You have to think through every possible input case.
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is low enough, turn on the LED:
if (analogValue <= thresholdlow) digitalWrite(relayPin, HIGH);
if (analogValue >= thresholdhigh) digitalWrite(relayPin, LOW);
// print the analog value:
Serial.println(analogValue);
}
jremington:
If I understand this correctly, you need two if statements. The following code does that, but does not cover all the possible cases.
What state is the relay supposed be in if the input is always between "thresholdlow" and "threshholdhigh"?
What state should the relay be in upon program startup? You have to think through every possible input case.
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is low enough, turn on the LED:
if (analogValue <= thresholdlow) digitalWrite(relayPin, HIGH);
if (analogValue >= thresholdhigh) digitalWrite(relayPin, LOW);
// print the analog value:
Serial.println(analogValue);
}
Mate that code was spot on for what i need it to do!
the relay is off upon start up. The relay is off when value is between the two threshold values, when the value drops below threshold low relay is HIGH and stays on until the value reaches thresholdhigh then turns off.
i modified my code with the code you provided, loaded it and tested the function. It works and i am grateful!!!