Need Help Programming!

Hi Everyone

I'm totally new with Arduinos and i just have some knowledge in electronics.

For a project if mine i need an output pin to first blink 4 times and then continuously light up if a button is pressed. It also needs to cancel at every point in this sequence if the button is released.

If someone would lead me to the correct code, that would be great!

Do you mean the button has to be pressed at the conclusion of the four flashes or during the flashes?

Do you know how to connect the LED on the output? Or how to read a switch?

If not, start by reading the information in the Learning tab at the top of this page.

Weedpharma

The button must be pressed the whole time.

I dont know how to interupt the sequence and how to make it just flash 4x and then light continuously.

You need a state machine.

Have a look at Planning and Implementing a Program

...R

Well I now got the code with the blinking, that turns off when the switch isnt't pressed but i still have now clue how to make the LED stay on after like 4 blinks.

my code now:

// Variables will change :
int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change :
const long interval = 200; // interval at which to blink (milliseconds)

// 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:
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
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(ledPin, ledState);
}
}
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Thanks for doing us the courtesy of trying to include your code in a block for the forum. But that looks like you used quote instead of code tags. Just for future reference...

POLYON:
The button must be pressed the whole time.

I dont know how to interupt the sequence and how to make it just flash 4x and then light continuously.

This is still confusing.

I understand that you want the LED to stay on while button is pressed and go off when released.

However, I don't know at what point the button is to be pressed.

Do you want 4 flashes during which time the button will be ignored, then at the conclusion of the flashes, the button is read?

Or, at any time the LED is flashing, the button can be pressed. Once the button is pressed the LED stays on until released.

Weedpharma

When i press a button a LED should blink 4 times and then be on as long as i keep pressing that button.
When i release the button at any point the led should turn off.

Is that more comprehensible?

use a counter for the blinks:

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 200;           // interval at which to blink (milliseconds)




// 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
const int numberOfBlinks = 4;
int blinks=  0;

// 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:
    {

    if (blinks >= numberOfBlinks) {
      digitalWrite(ledPin, HIGH);
   }
   else {

  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  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;
      blinks=  blinks+1;
    }
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
  }
}
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
    blinks=  0;
  }
}

A different approach. Based in BlinkWithoutDelay and Debounce examples:

const int ledPin =  13;
const int buttonPin = 2;

// Variables will change:
int ledState = LOW;  
unsigned long previousMillis = 0;

unsigned long previousButtonMillis = 0;

const unsigned long interval = 500;
const unsigned long intervalButton = 50; 

char state = 0;
char reading;
char buttonLastState;
char buttonState;
char blinkCount = 0;

const char blinkMax = 4;

void setup() {
  Serial.begin(9600);
  Serial.println("STATE 0");      

  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

void loop()
{
  unsigned long currentMillis = millis();

  //// STATE TRANSITIONS ////
  //read button with debounce
  reading = digitalRead(buttonPin);
  if (reading != buttonLastState) {
    previousButtonMillis = currentMillis;
  } 

  if ((currentMillis - previousButtonMillis) > intervalButton && reading != buttonState) {
    buttonState = reading;

    // change state in acordance with the button 
    if ( buttonState == HIGH && (state == 0 || state == 2) ){
      state = 1;
      Serial.println("STATE 1");
    }
    else if (buttonState == LOW && state == 1) {
      state = 0;
      blinkCount = 0;
      Serial.println("STATE 0");      
    }
  }
  buttonLastState = reading;

  // change state in accordance with the nuber of blinks
  if (state == 0 && blinkCount >= blinkMax) {
    state = 2;
    blinkCount = 0;
    Serial.println("STATE 2");
  }


  //// STATE ACTIONS ////
  if (state == 0) {

    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;   

      if (ledState == LOW) {
        ledState = HIGH;
        blinkCount++;
        Serial.print("blink: ");
        Serial.println( (int) blinkCount, DEC);
      }
      else {
        ledState = LOW;
      }
    }
  }
  else if (state == 1) {
    previousMillis = currentMillis;   
    ledState = LOW;
  }
  else if (state == 2 ) {
    ledState = HIGH;
  }
  
  // change the LED state
  digitalWrite(ledPin, ledState);
}

The advantage of this code is that you see clearly the transitions between states and the action of each state.

You do not want anything UNTIL the button is pressed, then blink 4 times, stop blinking and stay on until button is released.

I am being very particular because it determines what the program has to do and when.

When you are describing how a program works, you need to be very precise and unambiguous otherwise we cannot guess what you want to do.

Weedpharma

Shonek, use the <> icon for code, not the quote icon.

Weedpharma

So i tried both of the above codes and tried to modify them (the input is inverted and i wanted to add another "function" to the code) but it doesn't seem to work.

Here is what i would like to have:

My circuit consist of 1 LED (later a transistor)and a button (later the pin of a 433Mhz transmitter)

-If the button is not pressed nothing should happen.
-If I press and hold the button the led should blink 4 times, after that it should stop blinking and stay on until the button is released.
-If I release the button at any time (blinking or staying on) the led should turn off.
-If I release and re-press the button in less than 3 seconds the blinking part of the LED sequence should be skipped. (it should just be on for as long as i hold)

Variables that should be chanchable:
-Number of blinks
-Time the LED should blink on
-Time betwen 2 blinks
-Time betwen two button-presses that skips the blinking

Thanks for any help!

POLYON:
So i tried both of the above codes and tried to modify them (the input is inverted and i wanted to add another "function" to the code) but it doesn't seem to work.

Here is what i would like to have:

My circuit consist of 1 LED (later a transistor)and a button (later the pin of a 433Mhz transmitter)

-If the button is not pressed nothing should happen.
-If I press and hold the button the led should blink 4 times, after that it should stop blinking and stay on until the button is released.
-If I release the button at any time (blinking or staying on) the led should turn off.
-If I release and re-press the button in less than 3 seconds the blinking part of the LED sequence should be skipped. (it should just be on for as long as i hold)

Variables that should be chanchable:
-Number of blinks
-Time the LED should blink on
-Time betwen 2 blinks
-Time betwen two button-presses that skips the blinking

Thanks for any help!

I don't know why you could not write this in the first post. I took me some time to write the code that I post and now I must loss more time to change it because you could not explain what you really want.

luisilva:
I don't know why you could not write this in the first post. I took me some time to write the code that I post and now I must loss more time to change it because you could not explain what you really want.

I'm sorry to cause the inconvenience but i thaught i could figure out at least a part of the code.

I also didnt realize that i had to be that exact with the description.

Thanks for the efforts!

POLYON:
I'm sorry to cause the inconvenience but i thaught i could figure out at least a part of the code.

I also didnt realize that i had to be that exact with the description.

Thanks for the efforts!

You don't need to be exact with the description if you don't want an exact answer. Also, you must read the forum rules before post. You have the forum rules stocked at the top all the sections of this forum.

I don't know if you have tell everything you want that do, but you can try the following code:

const int ledPin =  13;
const int buttonPin = 2;

// Variables will change:
int ledState = LOW; 
unsigned long previousMillis = 0;

unsigned long previousButtonMillis = 0;

const unsigned long interval = 500;
const unsigned long intervalButton = 50;

char state = 1;
char reading;
char buttonLastState;
char buttonState;
char blinkCount = 0;

const char blinkMax = 4;

const unsigned long intervalToBlink = 3000;
unsigned long previousOnState;

void setup() {
  Serial.begin(9600);
  Serial.println("STATE 1");     

  pinMode(ledPin, OUTPUT);     
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, LOW);
}

void loop()
{
  unsigned long currentMillis = millis();

  //// STATE TRANSITIONS ////
  //read button with debounce
  reading = digitalRead(buttonPin);
  if (reading != buttonLastState) {
    previousButtonMillis = currentMillis;
  }

  if ((currentMillis - previousButtonMillis) > intervalButton && reading != buttonState) {
    buttonState = reading;

    // change state in acordance with the button
    if ( buttonState == LOW && (state == 0 || state == 2) ){
      state = 1;
      Serial.println("STATE 1");
      previousOnState = currentMillis;

    }
    else if (buttonState == HIGH && state == 1) {
      Serial.print("time: ");
      Serial.println(currentMillis - previousOnState);
      if (currentMillis - previousOnState >= intervalToBlink) {
        state = 0;
        blinkCount = 0;
        Serial.println("STATE 0");
      }
      else {
        state = 2;
        blinkCount = blinkMax;
        Serial.println("STATE 2");
      }
    }
  }
  buttonLastState = reading;

  // change state in accordance with the nuber of blinks
  if (state == 0 && blinkCount >= blinkMax) {
    state = 2;
    blinkCount = 0;
    Serial.println("STATE 2");
  }


  //// STATE ACTIONS ////
  if (state == 0) {

    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis;   

      if (ledState == LOW) {
        ledState = HIGH;
        blinkCount++;
        Serial.print("blink: ");
        Serial.println( (int) blinkCount, DEC);
      }
      else {
        ledState = LOW;
      }
    }
  }
  else if (state == 1) {
    previousMillis = currentMillis;   
    ledState = LOW;
  }
  else if (state == 2 ) {
    ledState = HIGH;
  }

  // change the LED state
  digitalWrite(ledPin, ledState);
}

POLYON:
Hi Everyone

I'm totally new with Arduinos and i just have some knowledge in electronics.

For a project if mine i need an output pin to first blink 4 times and then continuously light up if a button is pressed. It also needs to cancel at every point in this sequence if the button is released.

If someone would lead me to the correct code, that would be great!

For blinking, I use a trick. I keep track of the blinks in an int variable. If the variable goes from even to odd, then the light goes on, if it goes for odd to even, then the light goes off. When it goes to zero, that counts as going from odd to even, so that turns the LED off the final time.

To keep the cycle from going around and around for as long as you hold down the button, we have a separate variable 'blinking'. You could also do this by using a special value like -1 in variable blinks, but meh.

int blinks;
unsigned long blinkStart;
boolean blinking = false;

void loop() {
  if(the button is released) {
    if(blinks is an odd number) {
      turn LED off;
    }
    blinks = 0;
    blinking = false;
  }
  else {
    // button is pressed

    if(!blinking) {
      // need to start blinking
      blinks = 8; // off and on four times
      blinking = true;
      blinkStart = millis - 1000; // force blinking to start immediately
   }

   if(blinks > 0 && millis() - blinkStart > 250) {
     // time to blink!
     blinks --;
     if(blinks % 2 == 0) {
       turn LED off;
     }
     else {
       turn LED on;
     }
       blinkStart = millis();
    }
  }
}

If you like, you can compress the on/off code into a one-liner:

  digitalWrite(LED, --blinks % 2);

luisilva:
You don't need to be exact with the description if you don't want an exact answer. Also, you must read the forum rules before post. You have the forum rules stocked at the top all the sections of this forum.

I don't know if you have tell everything you want that do, but you can try the following code:

const int ledPin =  13;

[/quote]

Thanks, man! You helped me a lot!

I was just wondering if you could have different time values for the Blinking (like: 300ms on, 100ms off)

You dont need to re-write the code, just tell me if its possible and how i can do it.

Thanks buddy!

Yes you can. You only need to change this line:

    if(currentMillis - previousMillis > interval) {

to include the "ledState" and 2 different intervals (one to on and other to off);