HELP, when a button is pressed to set a threshold of analog input.

hey !

I am having trouble setting a threshold.
I can code a threshold within the code through a computer.
I wish to set a threshold while the code is running so the code can use that threshold to perform commands based on that threshold.
how can I go about coding the software of the Arduino to set a threshold as the code runs, I assume it would be in the void setup. but I am not sure how to do it?

thanks !!

You can't code anything until you decide what a button press represents. Just one button press? How many button presses? How do you know when you are through pressing buttons?

Paul

Hi paul,
I am going to have 2 modes with presslength_milliseconds declared as an int.
but these need to be based off the threshold for the code within the mode to work.
i will be setting an amount of time maybe 10 seconds to make the Arduino read the analog input and make that the threshold.
i am not sure how to make the Arduino save the analog input as a threshold while the code runs, everything else is covered.
how will i go about putting this in the code, i cannot figure out how to save the read analog input (pressure sensor ) and save it as a threshold for the rest of the code to run off.

thanks !!!

A L W A Y S
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Please try to be a bit more clear too.

patrick Dowers
6:36 PM (5 minutes ago)
to me

Hi,
I have not started the code yet, I have made certain part of the code but have not put it fully together due to the fact I need this part of the code to be understood by me.

My goal is to make the ardunio read the analog input of a pressure sensor - I can do this.

Then click a button for a certain time to activate the sensor to be read. - I can do this.

Then I need the ardunio to save the reading as a threshold. - I can’t do this. This is my problem.

This will then control a solenoid based on the pressure going over or under the threshold.

I do not know how to set a threshold while the code is running, I can set a threshold when I am coding from the computer. But for my project this limits the functionality.

I just need to know how to code it so it saves a variable/pressure as a threshold in the main code.

Thanks !

Patrick_dowers14:
I have not started the code yet, I have made certain part of the code but have not put it fully together due to the fact I need this part of the code to be understood by me.

Never try to develop a complex program as single entity. Develop and test it in small pieces. Have a look at Planning and Implementing a Program

If you post the program that represents your best attempt at solving your problem it will be much easier to see what you don't understand and to focus our advice without wasting time on the parts that do work.

...R

int sensorPin = A0;       // SENSOR pin
const int indicatorLedPin = 13; // BULTIN led
int buttonPin = 2;        // BUTTON pin
float pressLength_milliSeconds = 0; //mode componant
int optionOne_milliSeconds = 1000; //mode 1
int optionTwo_milliSeconds = 4000; //mode 2
int optionThree_milliSeconds = 8000; //set threshold time
const int ledPin2 = 12;       // LED pin
int threshold = 0;   // threshold
int sola = 2; //solanoid
const int ledPin3 = 11;  //LED pin


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(indicatorLedPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(sola , OUTPUT);

}

void loop() {

  while (digitalRead(buttonPin) == LOW ) {

    delay(100);
    pressLength_milliSeconds = pressLength_milliSeconds + 100;

  }

  //set threshold
  if (pressLength_milliSeconds >= optionThree_milliSeconds) {

    analogRead(sensorPin) = threshold));

  }

  //mode 2
  if (pressLength_milliSeconds >= optionTwo_milliSeconds) {

    digitalWrite(ledPin2, HIGH);

    int analogValue = analogRead(sensorPin);

    if (analogValue > threshold) {

      digitalWrite( sola, HIGH);   // open the solanoid
      delay(1000);                       // open for 1 second
      digitalWrite(sola, LOW);    // close the solanoid
      delay(10000) //repeat these steps every 10 seconds

    }
  }

  //mode 1
  else if (pressLength_milliSeconds >= optionOne_milliSeconds) {

    digitalWrite(ledPin3, HIGH);

    int analogValue = analogRead(sensorPin);

    if (analogValue > threshold) {

      digitalWrite( sola, HIGH);   // open the solanoid
      delay(1000);                       // open for 1 second
      digitalWrite(sola, LOW);    // close the solanoid
      delay(600000) //repeat these steps every 10 min

    }

  }
  pressLength_milliSeconds = 0;

}

this is my code put together from my separate testing codes.
when i am trying to set the threshold as seen in "set threshold" it comes up with "lvalue required as left operand of assignment"

this is my issue, the rest of the code is fine, maybe a few formatting mistakes but i can fix that with experimentation.

i just need to set the threshold one the mode is engaged and then save that data as the threshold for the rest of the code.

Thanks, sorry for the confusion !

I see this:

float pressLength_milliSeconds = 0; //mode componant

And wonder why it is a float? You never do anything with it that requires a float, Indeed, you only use it as a long int.

Paul

This is the wrong way round

   analogRead(sensorPin) = threshold));

It should be

threshold = analogRead(sensorPin);

...R

Okay, thanks. you make a very good point that I should have had it the other way around. I appreciate it a lot! Thanks for your time! I will test it out and if I have any other problems I will ask. But thanks a lot !! this is in relation to a School major project, is it all good if I can reference this conversation within my folio? thanks.

Patrick_dowers14:
Okay, thanks. you make a very good point that I should have had it the other way around. I appreciate it a lot! Thanks for your time! I will test it out and if I have any other problems I will ask. But thanks a lot !! this is in relation to a School major project, is it all good if I can reference this conversation within my folio? thanks.

That is fine, if you don’t come in and modify some of your posts.

Paul

Patrick_dowers14:
, is it all good if I can reference this conversation within my folio? thanks.

Yes.

I think what @Paul_KD7HB means is that your Original Post and Replies #2, #4 and #6 should not be edited. Doing so would make a nonsense of the comments that relate to them.

...R

yeah yeah, very understandable and i have no intentions to modify anything. Thanks for the help i appreciate it!