Go Down

Topic: Timer with two leds and two potentiometers (Read 31 times) previous topic - next topic

kikitron

Hi to all.
I have one problem how to write a code that I can run timer if I only push the buttonPin1 and stop it when I push buttonPin2.

Now my code is if I hold the buttonPin1, timer runs and If I release it then it stops.

Is it possible to do that?

Code: [Select]
/*Timer for two LEDs with potentiometers*/

const int buttonPin1 = 2; // start button
const int buttonPin2 = 3; /// stop button
int potPin1 = 0;    // select the input pin for the potentiometer_1
int potPin2 = 2;    // select the input pin for the potentiometer_2
const int ledPin1 = 10;   // select the pin for the LED_1
const int ledPin2 = 12;   // select the pin for the LED_2
int val1 = 0;       // variable to store the value coming from the sensor_1
int val2 = 0;       // variable to store the value coming from the sensor_2

int buttonState1 = 0;
int buttonState2 = 0;


void setup() {
  pinMode(ledPin1, OUTPUT);  // declare the ledPin1 as an OUTPUT
  pinMode(ledPin2, OUTPUT);  // declare the ledPin2 as an OUTPUT

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() {

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 == HIGH) {       // START the timer
 
    val1 = analogRead(potPin1) * 5;    // read the value from the sensor_1
    digitalWrite(ledPin1, HIGH);  // turn the ledPin1 on
    delay(100);                  // stop the program for 300ms
    digitalWrite(ledPin1, LOW);   // turn the ledPin1 off
    delay(val1);                  // stop the program for some time
 
    val2 = analogRead(potPin2) * 5;    // read the value from the sensor_2
    digitalWrite(ledPin2, HIGH);  // turn the ledPin2 on
    delay(100);                  // stop the program for 300ms
    digitalWrite(ledPin2, LOW);   // turn the ledPin2 off
    delay(val2);                  // stop the program for some time
  }
}

PaulS

Quote
Is it possible to do that?
Of course. The state change detection example shows how to determine that a switch has become pressed or become released. Start the timer when one switch becomes pressed. Do nothing when that switch becomes released. Stop the timer when the other switch becomes pressed. Do nothing when that switch becomes released.

Are the switches properly wired with pullup resistors?

Code: [Select]
    delay(100);                  // stop the program for 300ms

No, that is NOT what the code does.

You need to separate the action of reading the switches from the action of dealing with the switch states.

On some passes through loop(), nothing will happen. On other passes, the timer will be incremented to indicate that time has passed (though your code now does not start a timer or stop a timer).

You also need to look at the blink without delay example, without delay. "Without delay" needs to be the theme of your program.

kikitron

Thanke you PaulS for your answer.
I have switches wired with 10k pullup resistor.

I add "delay(100);" for led blinking and its fixed time 300ms.
I can add "delay(val1);" but for turning led off I need fixed time and for on I use potentiometer.


I Will look example of without delay.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy