Turning LED on to blink

I am brand new to Arduino and the coding...so I realize this is noob question. But, I am trying to get an LED to blink using a button input. Essentially, the LED should stay dark until the Onbutton is pressed, after which it should blink continuously. Right now, it doesn't come on till the button is pushed, but after a second turns the LED off and never turns it back on a second later. Eventually, of course, I'd like to add the second button to turn it off and thus the OnOff flag. Coding is below.

const int ledPin1 = 5;
const int Onbutton = 9;
int OnOff = 0;
int ledState = 0;
unsigned long previousMillis = 0;
unsigned long interval = 1000;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(Onbutton, INPUT_PULLUP);
}
void loop()
{
unsigned long currentMillis = millis();
if (digitalRead(Onbutton) == LOW)
{
OnOff = 1;
digitalWrite(ledPin1, HIGH);
};
if ((currentMillis - previousMillis > interval) && (OnOff = 1))
{
previousMillis = currentMillis;
if(ledState = 0)
{
ledState = 1;
digitalWrite(ledPin1, HIGH);
}
else
{
ledState = 0;
digitalWrite(ledPin1, LOW);
}
}
}

Oops

Gotcha... ==

1 Like

Never mind...Offbutton wasn't in pinMode. For anyone looking to do this, here is the correct code.

const int ledPin1 = 5;
const int Onbutton = 9;
const int Offbutton = 8;
int ledState = LOW;
unsigned long previousMillis = 0;
unsigned long interval = 1000;
int OffOn = 0;

void setup() {
pinMode (ledPin1, OUTPUT);
pinMode (Onbutton, INPUT_PULLUP);
pinMode (Offbutton, INPUT_PULLUP);
}

void loop() {
unsigned long currentMillis = millis();
if (digitalRead(Onbutton)==LOW)
{
OffOn = 1;
digitalWrite(ledPin1, HIGH);
}
if (digitalRead(Offbutton)==LOW)
{
OffOn = 0;
digitalWrite(ledPin1, LOW);
}
if ((currentMillis - previousMillis > interval) && (OffOn == 1))
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin1, ledState);
}
}

Debouncing Switches in Arduino provides a simple button debounce class that detects change of button state.
How to write Timers and Delays in Arduino covers using millis() for timers and has a convenient millisDelay class.
For both of these to work you need to keep you loop() running quickly.
Multi-tasking in Arduino has suggestions on how to structure your code as tasks which can assist in organizing your operations.
multitaskingDiagramSmall

But posted incorrectly. 3 times! Please read the forum guide before your next post.

In my excitement of finding support and of fixing my problem, I posted too much and too early. Sorry about that. If it would be better for the community for it to be removed, I understand.

This is really helpful. Thanks!!

What would be better would be if you edited those posts and put in the code tags. Then you will have put things right yourself instead of someone else having to do it for you.

It was - literally - my first day on the forum. My apologies for being excited and breaking community standards or making anyone else have to do what was expected of me. May I - ever so tentatively - suggest that these community standards be included in the robot introduction? Had I understood those standards I would have tried to avoid them. Again, deepest apologies.

Sorry, what's that?

I'm not a forum moderator and have no direct influence over the rules or how they are communicated.

Yes, 8 or 9 out of 10 new forum users give the excuse that they are new. The other 1 or 2 read the forum guide before they post, and don't have anything to be excused for.

In the time it took you to post your last reply, you could have fixed the 3 posts where you don't follow the forum rules. Please just do it.

This starts led blinking when you press a button:

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) {
  while (1) {
     digitalWrite(ledPin, HIGH);
     delay(1000);
       digitalWrite(ledPin, LOW);
       delay(1000);
  }
  } 
}

Such a small amount of code.
So many questions.

Fixed what I think I could by deleting the posts that I could. The robot introduction is the intro that Arduino brings you through to become a member to post on forums. It didn't let me know about forum guides or rules....but I will be sure to check them in the future, so your posts have been helpful. Are we done now? I think you have made me feel as bad as possible for posting to this thread....

Newer mind. New users can not be expected to know every rule. In my opinion.

1 Like

Thanks...this is helpful. I also was able to get two separate signals to flash separately. I realize that, again, this all noobish....but exciting to me... Not sure how to get it to show up in the unique coding box (anyone's gracious help here is appreciated so that I don't get blasted by pros)....but here it is below. Obviously, it is just adding '2' to most of the code....but it was a big step for me....

const int ledPin1 = 11;
const int ledPin2 = 8;
const int Onbutton = 13;
const int Onbutton2 = 10;
const int Offbutton = 12;
const int Offbutton2 = 9;
int ledState = LOW;
int ledState2 = LOW;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long interval = 1000;
unsigned long interval2 = 1000;
int OffOn = 0;
int OffOn2 = 0;

void setup() {
pinMode (ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (Onbutton, INPUT_PULLUP);
pinMode (Offbutton, INPUT_PULLUP);
pinMode (Onbutton2, INPUT_PULLUP);
pinMode (Offbutton2, INPUT_PULLUP);
}

void loop() {
unsigned long currentMillis = millis(); //updates timer
if (digitalRead(Onbutton)==LOW) //checks if on button pushed
{
OffOn = 1; //updates status to on
digitalWrite(ledPin1, HIGH); //turns led on
}
if (digitalRead(Offbutton)==LOW) //checks if off button pushed
{
OffOn = 0; //updates status to off
digitalWrite(ledPin1, LOW); //turns led off
}
if ((currentMillis - previousMillis > interval) && (OffOn == 1)) // checks if 1 minute has passed AND status on
{
previousMillis = currentMillis; //changed previous timer to current timer to reset
if (ledState == LOW) //checks if led status is off
ledState = HIGH; //sets led status on
else
ledState = LOW; //sets led status off
digitalWrite(ledPin1, ledState); //turns led on or off
}
unsigned long currentMillis2 = millis(); //updates timer
if (digitalRead(Onbutton2)==LOW) //checks if on button pushed
{
OffOn2 = 1; //updates status to on
digitalWrite(ledPin2, HIGH); //turns led on
}
if (digitalRead(Offbutton2)==LOW) //checks if off button pushed
{
OffOn2 = 0; //updates status to off
digitalWrite(ledPin2, LOW); //turns led off
}
if ((currentMillis2 - previousMillis2 > interval2) && (OffOn2 == 1)) // checks if 1 minute has passed AND status on
{
previousMillis2 = currentMillis2; //changed previous timer to current timer to reset
if (ledState2 == LOW) //checks if led status is off
ledState2 = HIGH; //sets led status on
else
ledState2 = LOW; //sets led status off
digitalWrite(ledPin2, ledState2); //turns led on or off
}
}

Sorry...the only downside to the code you provided is the inherent delay....which doesn't allow other tasks to run at the same time. I started with delays....but with multiple tasks running (i.e. multiple possible inputs at the same time), the code I posted seemed to work better.... Thanks!

Yes, I'm done with you. I can't fix your attitude.

1 Like

Heh, heh. :grin:

Paul your rude. Honestly I just signed up myself and this is my first forum and I had no clue about a guide. Iā€™m sorry not trying to attack you but you need to ease up a little.