Controlling LED [Solved!]

Hi!

I wrote already in the LED part of this forum but I think I was too confusing and since my problem is with the coding stuff I will put my problem here in a much simpler way:

In reality I wanted to use Megunolink to control the arduino from my pc but in this example lets imagine that I have a button connected to arduino and a LED. When I pressed the button, I wanted the LED to turn ON for a "x" time based on a linear equation. The LED would stay ON for x time when I pressed the button and then would go OFF automatically after passing that time. Do you have any idea how to do that?

I think a good starting point would be the example of the button that I have on the library:

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

// 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 =  13;      // the number of the LED pin

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Thanks a lot!!

The LED would stay ON for x time when I pressed the button and then would go OFF automatically after passing that time.

Save the millis() value at the time that the button becomes pressed (or maybe released. You choose). Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the required period has elapsed then turn off the LED. If not, then go round loop() again but don't block the free running of loop().

See Using millis() for timing. A beginners guide

The OP wrote about using a linear equation but never told us what the input parameter would be. Of course, it is not hard to implement a linear equation.

vaj4088:
The OP wrote about using a linear equation but never told us what the input parameter would be. Of course, it is not hard to implement a linear equation.

The inputs (I believe that with that you mean the variables of the equation) for my work would be flow rate, the volume needed do be injected and the time to the solenoid be ON (in my example, the time of the LED to be ON). Correct me if I understood you wrong please (sorry I am really new in these subjects ahah)

UKHeliBob:
Save the millis() value at the time that the button becomes pressed (or maybe released. You choose). Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the required period has elapsed then turn off the LED. If not, then go round loop() again but don't block the free running of loop().

See Using millis() for timing. A beginners guide

Thank you very much!!! I will read it!

UKHeliBob:
Save the millis() value at the time that the button becomes pressed (or maybe released. You choose). Then, each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now. If the required period has elapsed then turn off the LED. If not, then go round loop() again but don't block the free running of loop().

See Using millis() for timing. A beginners guide

Can you please exemplify? I tried reading what you have sent but I couldn't do it

You answered my question about the linear equation. Thanks!

Can you please exemplify? I tried reading what you have sent but I couldn't do it

The Beginner's Guide provides examples.

I tried reading what you have sent but I couldn't do it

What did you try ?
Where are you stuck ?

It is like this (dont laught at me please ahah):

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

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;  

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
   startMillis = millis();
}

void loop() {
  // read the state of the pushbutton value:
   currentMillis = millis();  //get the current time

  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

I really didnt had time to read with calm and attention your guide because here it's already night and I am tired, but I tried to make the LED ON during 5 seconds using your example of changing the brigthness

Remember what I said in my earlier post ?

Save the millis() value at the time that the button becomes pressed

Your code does not do that

each time through loop(), check whether the required wait period has elapsed by subtracting the start time from the millis() value now.

Your code does not do that so you can't do this

If the required period has elapsed then turn off the LED.

Get some sleep and come back to the problem tomorrow

UKHeliBob:
Remember what I said in my earlier post ?
Your code does not do that
Your code does not do that so you can't do this
Get some sleep and come back to the problem tomorrow

Thank you very much for your help :)! I changed the code as you suggested but yet when I stop pressing the button, the led immediately stops shining when I wanted him to stay on for 5 seconds.

// 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 =  13;      // the number of the LED pin

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

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;  //the value is a number of milliseconds, ie 1 second

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

void loop() {
  currentMillis = millis();
  // read the state of the pushbutton value:
  if (currentMillis - startMillis >= period);  //test whether the period has elapsed
  
   buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
     startMillis = currentMillis;
    
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    
   
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

I changed the code as you suggested

Well, actually you didn't do as I suggested.

when I stop pressing the button, the led immediately stops shining

That should come as no surprise bearing in mind this portion of your code

  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }

Basically it says "if the button is currently pressed then turn the LED on, otherwise turn it off"
Remove the else part of the if/else

In another part of your code

 currentMillis = millis();
  // read the state of the pushbutton value:
  if (currentMillis - startMillis >= period);  //test whether the period has elapsed
  buttonState = digitalRead(buttonPin);

Which says "if the period has elapsed then read the button input" when it should say "if the period has elapsed then turn the LED off"

There is a superfluous semi-colon on this line:

  if (currentMillis - startMillis >= period);  //test whether the period has elapsed

UKHeliBob:
Well, actually you didn't do as I suggested.
That should come as no surprise bearing in mind this portion of your code

  buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }



Basically it says "if the button is currently pressed then turn the LED on, otherwise turn it off"
Remove the else part of the if/else

In another part of your code


currentMillis = millis();
  // read the state of the pushbutton value:
  if (currentMillis - startMillis >= period);  //test whether the period has elapsed
  buttonState = digitalRead(buttonPin);



Which says "if the period has elapsed then read the button input" when it should say "if the period has elapsed then turn the LED off"

Sorry!! I tried to change the code as you said but now the LED is permanently ON. I know that I messed up somewhere but I dont see where ahah

// 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 =  13;      // the number of the LED pin

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

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;  //the value is a number of milliseconds, ie 1 second

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  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);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  startMillis = currentMillis;
     
      // turn LED on:
    digitalWrite(ledPin, HIGH);
  
    currentMillis = millis(); 
   if (currentMillis - startMillis >= period);  //test whether the period has elapsed
  } 
    // turn LED off:
    digitalWrite(ledPin, LOW);
  
}

wildbill:
There is a superfluous semi-colon on this line:

  if (currentMillis - startMillis >= period);  //test whether the period has elapsed

What do you mean with that?

 if (buttonState == HIGH)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    currentMillis = millis();
    if (currentMillis - startMillis >= period);  //test whether the period has elapsed
  }

Separate the check for the button being pressed from the check whether the period has elapsed. A separate if for each and only turn the LED off if the period has elapsed

At the moment every time through loop() you set the LED pin LOW. Why ?

    if (currentMillis - startMillis >= period);

If this test is true then the only code that is conditionally execute is the semicolon. Remove it.

What do you mean with that?

If statements rarely properly end with a semicolon.

UKHeliBob:

 if (buttonState == HIGH)

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



Separate the check for the button being pressed from the check whether the period has elapsed. A separate if for each and only turn the LED off if the period has elapsed

At the moment every time through loop() you set the LED pin LOW. Why ?



if (currentMillis - startMillis >= period);



If this test is true then the only code that is conditionally execute is the semicolon. Remove it.

Thnak you so much for all your help. I did that now. But apparently with some error because now, the LED is OFF but when I press the button it doenst turn ON

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

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;  //the value is a number of milliseconds, ie 1 second

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:
  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 == HIGH)
  {
    startMillis = currentMillis;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    currentMillis = millis();
 }
   
    if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
  
}

That doesn't compile. Please post the whole thing.