Go Down

Topic: Keeping an LED blinking after letting go of a switch (Read 666 times) previous topic - next topic

jksistheman

So the other day I posted about getting an Arduino to make an LED turn on with one switch and off with another. I want the LED to blink when I press the first switch and keep blinking until I press the second switch. with the code below, it will blink while i have the first switch pressed, but stop as soon as I let it go. If anyone knows how to extend the blinking until i press down the second switch I would very much appreciate it. Thanks

Code: [Select]
// constants won't change
const int Switch[] = {7,11}; 
const int LED =  13;

// variables will change:
int SwitchState = 0;   
int ledState = LOW;

unsigned long previousMillis = 0;
const long interval = 100;

void setup() {
  pinMode(LED, OUTPUT);     
  // initialize the pushbutton pin as an input:
   for(int x=0; x<=1; x++){
    pinMode(Switch[x], INPUT);
  } 
}

void loop(){
  // read the state of the pushbutton value:
  for(int x=0; x<=1; x++){
    SwitchState = digitalRead(Switch[x]);
   
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (SwitchState == HIGH && Switch[x] == 7) { 
     
        // turn LED on:   
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {
          // save the last time you blinked the LED
          previousMillis = currentMillis;

          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW) {
            ledState = HIGH;}
            else {
            ledState = LOW;}
         
    // set the LED with the ledState of the variable:
    digitalWrite(LED, ledState);
  }
    }
    if (SwitchState == HIGH && Switch[x] == 11) {
      // turn LED off:
      digitalWrite(LED, LOW);
    }
  }
}

larryd

When the 1st button is pressed make a Boolean variable true.
When the Boolean variable is true, flash a LED.
When the 2nd button is pressed make the Boolean variable false.


.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

J-M-L

use the first switch to set a boolean blink to true and use the second switch to set that boolean to false

then test the boolean and if it's true, blink (using the blink without delay method)

in pseudo code it would look like this

if swicth1 is pressed blink = true;
if switch2 is pressed blink = false;
if (blink) {
   if it's time to blink the LED, do it.
}
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

J-M-L

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

jksistheman

I got It to work! Thanks people

Code: [Select]
// constants won't change
const int Switch[] = {7,11}; 
const int LED =  13;

// variables will change:
int SwitchState = 0;   
int ledState = LOW;

unsigned long previousMillis = 0;
const long interval = 100;

boolean BooleanSwitch = LOW;

void setup() {
  pinMode(LED, OUTPUT);     
  // initialize the pushbutton pin as an input:
   for(int x=0; x<=1; x++){
    pinMode(Switch[x], INPUT);
  } 
}

void loop(){
  // read the state of the pushbutton value:
  for(int x=0; x<=1; x++){
    SwitchState = digitalRead(Switch[x]);
   
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (SwitchState == HIGH && Switch[x] == 7) {
      BooleanSwitch = HIGH;}
        if (BooleanSwitch == HIGH){

         
     
        // turn LED on:   
        unsigned long currentMillis = millis();
        if (currentMillis - previousMillis >= interval) {
          // save the last time you blinked the LED
          previousMillis = currentMillis;

          // if the LED is off turn it on and vice-versa:
          if (ledState == LOW) {
            ledState = HIGH;}
            else {
            ledState = LOW;}
         
    // set the LED with the ledState of the variable:
    digitalWrite(LED, ledState);
  }
    }
   
    if (SwitchState == HIGH && Switch[x] == 11) {
      BooleanSwitch = LOW;}
        if (BooleanSwitch == LOW){
           // turn LED off:
           digitalWrite(LED, LOW);
    }
  }
}

Go Up