Go Down

Topic: Blinking LED w/ button (Read 241 times) previous topic - next topic

mikeyspring

Hello! I am a super noob here! I am working on writing a code that turns on a LED with the press of a button. Then the LED will blink for some amount of time. Finally, when you press the button again it turns off. I have got the LED to turn on and blink and then let me turn it off. However, the trouble I am having is that if I want to turn the button on again and get it to blink I have to reset my ArduinoNano. (i.e. There is only one run per reset.) I want my code to run to where I am able to have it blink and then turn it off as many times as I would like. Can someone please help me? I am new to the Arduino language. Some example code or code to look at would be helpful thank you! I will attach my code!

Delta_G

What's the value of var at the end of the loop?  What does it need to be in order to start flashing?
If at first you don't succeed, up - home - sudo - enter.

GolamMostafa

#2
Jun 19, 2017, 11:02 pm Last Edit: Jun 19, 2017, 11:15 pm by GolamMostafa
@mikeyspring

1. You have said that you are supper noob; if so, from where have you got the codes that you have attached?

2. I tried to read your codes, but I failed as the presentation style is totally illiterate. There is something in programming called literate programming which means: If you wish that your codes are presented for others to read and comment, then maintain the alignment of the braces. For example:for(expression){statements;} is perfectly alright syntactically; but, it is illiterate when presented to others. The literate format:

Code: [Select]

for(expression)
{
   //statements;
}


3. You are a noob, and I am a novice. I hope that I have understood your logic of playing with the led2 and button2. I assume that you have an external pull-up resistor connected with your button2. led2 is connected at digital pin-10, and button2 is connected with digital pin-9 with internal pull-up resistor enabled.

I have taken your programming problem as an exercise for me, and I am presenting my solution (tested) below with a hope that it might be helpful for you!


4. Flow Chart of the Solution




5. Arduino Codes for the Flow Chart of Section-4
Code: [Select]


void setup()
{
   pinMode(9, INPUT_PULLUP);    //button2 with internal pullup
   pinMode(10, OUTPUT);         //L1
   digitalWrite(10, LOW);       //LED2 OFF
    
   while(digitalRead(9) !=LOW)   //L2
      ;
}

void loop()
{
  
  
     for (int n =0; n<=14; n++)  //L3
     {
        digitalWrite(10, HIGH);
        delay(500);
        digitalWrite(10, LOW);
        delay(500);  
     }
      
      digitalWrite(10, HIGH);     //L4

      while(digitalRead(9) != LOW)    //L5
        ;
      delay(500);                            //key debounching delay
      digitalWrite(10, LOW);       //L6
    
      while (digitalRead(9) !=LOW)   //L7
        ;
   //; goto  L3;
  

}




(a) Compile and upload the program.

(b) Press button2.

(c) Check that led2 blinks for 15 times and then goes ON.

(d) Press button2

(e) Check that led2 is OFF

(f) Press button2

(g) Check that program control has gone to Step-(c)

***Note: Experiment dictated me to insert key debounching delay for the program logic to work as per Flow Chart***


mikeyspring

Thank you for your reply!

Okay, so I was able to download and upload your code to my ArduinoNano. It worked, but it doesn't quite do what I am needing it to do. Your code makes the led blink indefinitely and the button isn't incorporated. I need code that turns on the light and has it blink for some amount of time, then be able to shut off with another press of the button. Then when I press the button a third time it will start over and blink again and then turn off when the button is pressed a fourth time.

Hopefully, someone can help!

Thank you all for all your help!

Delta_G

Did you think about reply #1?  It will require thought.  It isn't just a simple gimme da code.  But your code isn't that far off and if you think about that one thing you'll be almost there.
If at first you don't succeed, up - home - sudo - enter.

mikeyspring

What's the value of var at the end of the loop?  What does it need to be in order to start flashing?
Would Var need to go back to being 0 again? This makes sense, but types of commands do that? Thank you for your help I appreciate it!

mikeyspring

By setting var = 0 at the end of my code. I was able to get it to go through its cycle of blinks! The only problem now is it won't turn off. How should I go about making it turn off and then go through the cycle of blinks again and then turn off and then turn off again with the cycle?

Delta_G

#7
Jun 21, 2017, 06:58 pm Last Edit: Jun 21, 2017, 06:59 pm by Delta_G
Think about WHERE in the code you set var back to 0.  Remember, the loop function is going to repeat over and over. 

If you are having trouble with the new code, then post the new code.  I can't tell from here where you put that line. 

Please inline it this time so folks using phones don't have to download it and can still see it.  Don't forget the code tags. 
If at first you don't succeed, up - home - sudo - enter.

mikeyspring

Code: [Select]
int led2 = 10;
int button2= 9;
int val2;
int buttonState2;
int lightMode2 = 0;
int var = 0;

setup loop()
{

  pinMode(button2, INPUT);
  pinMode(led2, OUTPUT);
  buttonState2 = digitalRead(button2);

void loop(){

val2 = digitalRead(button2);
 
 if (val2 != buttonState2)
 {

   if (val2 == LOW)
   {

     if (lightMode2 == 0)
     {

       lightMode2 = 1;
       digitalWrite(led2, HIGH);}
       {

        while(var < 15)
        {

          delay(250);
          digitalWrite(led2,LOW);
          delay(250);
         digitalWrite(led2,HIGH);
         var++;
         if ( var == 15)
         {

           lightMode2 = 0;
           digitalWrite(led2,LOW);
         }
        }
     if (lightMode2 == 0)
     {

       lightMode2 = 1;
       digitalWrite(led2, HIGH);
     }

     else
     {

       lightMode2 = 0;
       digitalWrite(led2, LOW);
     }
   }
 }
   }
       buttonState2 = val2;
       var = 0;
}


Here is my new code. I inserted var = 0 at the end of the code before the void loop end brace.

mikeyspring

Think about WHERE in the code you set var back to 0.  Remember, the loop function is going to repeat over and over. 

If you are having trouble with the new code, then post the new code.  I can't tell from here where you put that line. 

Please inline it this time so folks using phones don't have to download it and can still see it.  Don't forget the code tags. 
Hello! I got the blinking to loop, but is there a way to have it turn off in between the next set of blinks?

Code: [Select]
int led2 = 10;
int button2= 9;
int val2;
int buttonState2;
int lightMode2 = 0;
int var = 0;

setup loop()
{

  pinMode(button2, INPUT);
  pinMode(led2, OUTPUT);
  buttonState2 = digitalRead(button2);

void loop(){

val2 = digitalRead(button2);
 
 if (val2 != buttonState2)
 {

   if (val2 == LOW)
   {

     if (lightMode2 == 0)
     {

       lightMode2 = 1;
       digitalWrite(led2, HIGH);}
       {

        while(var < 15)
        {

          delay(250);
          digitalWrite(led2,LOW);
          delay(250);
         digitalWrite(led2,HIGH);
         var++;
         if ( var == 15)
         {

           lightMode2 = 0;
           digitalWrite(led2,LOW);
         }
        }
     if (lightMode2 == 0)
     {

       lightMode2 = 1;
       digitalWrite(led2, HIGH);
     }

     else
     {

       lightMode2 = 0;
       digitalWrite(led2, LOW);
     }
   }
 }
   }
       buttonState2 = val2;
       var = 0;
}

Go Up