Part III of the controlling LED series. The final stage [Solved!]

Hi! Because my second post was becoming too big again and kinda lost in the forum, I open a third and hopefully a last one to end this problem. Briefly, in the first part the problem was manage to get the LED ON for 5 seconds after pressing the button using millis (), the second was getting a good code with a good function/equation to instead of staying just 5 seconds ON, the LED would stay x time on depending on the values that I would put in the equation.

Yesterday, with the code that I wrote with the help of UKHeliBob and MorganS, I dont know how, but I manage to get the thing working :slight_smile: . I wrote the volume value, then the flow in the serial monitor, the calculation was done and then when I pressed the button, the LED stayed ON for the exact time that I have got in the result of the equation and the thing repeated.

Now, I have three questions:

  1. Why I cant put values smaller than 0.01 in the serial monitor?
  2. During this topics, people told me to use the boolean function but the code is working without it. Is there any need to add it, and if yes, how?
  3. I would like that the event of pressing the button didnt turn the LED ON but instead, make appear the "Enter volume here:" and all the other equation part and when I got the result the LED would turn ON automatically for the time that the equation gave.

EDIT: I think the question 3 is no longer necessary to answer because I spoke with another guy in the lab and he said to me that it would be better to the LED/solenoid turn ON when the button is pressed and not when we got the result from the equation.

Here is the code that I have got so far:

// Solenoid part:  

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

unsigned long startMillis;
unsigned long currentMillis;
 
// Function part:

float timer;
float volume;
float flow; 

void setup() {
  
 Serial.begin(9600);

 Serial.print("Enter volume value: ");
 
 pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  startMillis = millis();  //initial start time

}

void loop() {

   currentMillis = millis(); 
   buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
   
    
 }
  
  if (Serial.available()) {
   
    volume = readSerial();

    Serial.print("Enter flow value: ");
    while (!Serial.available());
    flow = readSerial();

    findSide(volume, flow);

    Serial.println();
    Serial.print("Enter volume value: ");
  }

 if (currentMillis - startMillis >= timer)  //test whether the period has elapsed
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    
  }
 
    }

  float readSerial()
  {
    float i = Serial.parseFloat();

    Serial.println(i);
    Serial.parseFloat();
    return i;
  }

  void findSide(float x, float y) 
  {
    //calculation

    timer = (x / y) * 10000;

    //print out the result
    Serial.print("TIME = ");
    Serial.println(timer);

  }

Thank you!!!! :slight_smile:

  1. Why I cant put values smaller than 0.01 in the serial monitor?

I don’t know why you can’t. I can.

You could look at the documentation for the print() and/or println() methods (of the Print class), to see what optional arguments there are, and what they do.

  1. During this topics, people told me to use the boolean function but the code is working without it. Is there any need to add it, and if yes, how?

There is no “boolean function”. There is a boolean type. There are functions that return a boolean value. So, your question, as asked, does not make sense.

  1. I would like that the event of pressing the button didnt turn the LED ON but instead, make appear the “Enter volume here:” and all the other equation part and when I got the result the LED would turn ON automatically for the time that the equation gave.

So, move the code to turn the LED on, and record when that happens, to where you want it to happen.

PaulS:
I don't know why you can't. I can.

You could look at the documentation for the print() and/or println() methods (of the Print class), to see what optional arguments there are, and what they do.

How can I do that?

There is no "boolean function". There is a boolean type. There are functions that return a boolean value. So, your question, as asked, does not make sense.
So, move the code to turn the LED on, and record when that happens, to where you want it to happen.

Yeah! Sorry but I expressed myself wrong :D. I was talking about the boolean type. Is there any need for that? You will probably say no because the code is already working ahah

Thank you!!!

How can I do that?

print()

  1. Why I cant put values smaller than 0.01 in the serial monitor?

You can enter a smaller value but the default number of decimal places when printing a float is 2. By using a second parameter in Serial.println() you can change the number of decimal places printed

  1. During this topics, people told me to use the boolean function but the code is working without it. Is there any need to add it, and if yes, how?

As has been pointed out boolean is data type not a function. Somewhere in this long saga of threads and posts I believe that I suggested using the value of a boolean variable to control when the prompts occurred. If the code is doing what you want without it then you don't need it.

Thanks guys!!!! I think its done! Later in this week or next week, we will assemble the reactor and see if it works in "real life" with the solenoids and the Megunolink programme :D. I will leave here the final code for everyone to see it and use it as an example if they want.

I hope that this is my final message regarding this topic at least ahah, it would be a good sign!

Thank you again for all the help!!!

// Solenoid part:  

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

unsigned long startMillis;
unsigned long currentMillis;
 
// Function part:

float timer;
float volume;
float flow; 

void setup() {
  
 Serial.begin(9600);

 Serial.print("Enter volume value: ");
 

 pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  startMillis = millis();  //initial start time

}

void loop() {

   currentMillis = millis(); 
   buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
 }
  
  if (Serial.available()) {
   
    volume = readSerial();

    Serial.print("Enter flow value: ");
    while (!Serial.available());
    flow = readSerial();

    findSide(volume, flow);

    Serial.println();
    Serial.print("Enter volume value: ");
  }

 if (currentMillis - startMillis >= timer)  //test whether the period has elapsed
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    
  }
 
    }

  float readSerial()
  {
    float i = Serial.parseFloat();

    Serial.println(i, 4); // The comma and the 4 just represents the number of decimal houses displayed on the serial monitor, it could be changed to whatever we want
    Serial.parseFloat();
    return i;
  }

  void findSide(float x, float y) 
  {
    //calculation

    timer = (x / y) * 10000;

    //print out the result
    Serial.print("TIME = ");
    Serial.println(timer);

  }
    Serial.println(i, 4); // The comma and the 4 just represents the number of decimal houses displayed on the serial monitor, it could be changed to whatever we want

Decimal houses?

PaulS:

    Serial.println(i, 4); // The comma and the 4 just represents the number of decimal houses displayed on the serial monitor, it could be changed to whatever we want

Decimal houses?

Ups sorry ahah. Decimal places*. I translated it literally from Portuguese