New to coding: I wrote a code that half works, can anybody advise?

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

}

You could add some serial prints to get your code to tell you what it is doing.

Hello there!

Your code looks great :slight_smile:

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.

} else  {
    (analogValue > thresholdhigh);
      digitalWrite(relayPin, LOW);
    }

Your code doesn't look great - ignore the noob

TolpuddleSartre:
Your code doesn't look great - ignore the noob

What leads you to believe the code isn't great?

bos1714:
What leads you to believe the code isn't great?

The code that I highlighted.
Just because it compiles don't make it good.

a code that allows me to use a pressure sensor

Do you have a pressure sensor?

If so, which one (post a link) and how did you wire it?

I wanted the relay to come on at a low threshold value and turn off at a high threshold value.

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.

TolpuddleSartre:
The code that I highlighted.
Just because it compiles don't make it good.

Just because a line of code looks out of place or isn't performing the function one thinks it is doesn't make it bad either.

What leads you to believe the code isn't great?

Statements that don't accomplish anything useful should not lead one to believe that this is "great code".

Lack of 'if', presence of semicolon.

Lack of you reading that the OP is new to coding.

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.

I see, we should simply praise the mistakes, as you did.

bos1714:
Lack of you reading that the OP is new to coding.

I can only see that the poster has a low post count.
Not long ago, I too had a low post count, but that doesn't mean to say I was new to coding.

You need to develop your logical thinking.

TolpuddleSartre:
You need to develop your logical thinking.

My logical thinking is fine. For example, since you are not listening, I am going to end this conversation.

bos1714:
My logical thinking is fine.

Right.
Calibrated.

jremington:
Do you have a pressure sensor?

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!!!

thanks for your help!