Blinking led+ lcd screen

Hy everybody, I am new here and I need help for my little project: when I push the button ( just one time),the lcd screen must “write” something ( hello world for example) ,but I want a blinking led too. I know how to blink a led ( with delay or millis ) ,but when I create the code the led just turn on and it doesn’t blink!
Here a piece of the code:

if (previousSwitchState != SwitchState){ // if you push the button one times flag = 1
if (SwitchState == HIGH ){

flag=1; // this “flag” it’s for a switch structure

led(); // blink led

} }

If I write led() outside if structures, the led starts blinking but It’s not good for me. Hoper you can help,thanks!

Here a piece of the code:

Here a piece of the answer: "Because"

here the whole code :

#include <LiquidCrystal.h>
LiquidCrystal lcd (12,11,5,4,3,2);
int previousSwitchState = 0;
int previousSwitchState2 = 0;
int Mos = 0;
int SwitchState = 0;
int SwitchState2 = 0;
int flag = 0 ;
const int SwitchPin = 6;
const int SwitchPin2 = 7;
const int mosPin = 10;
const int ledPinred = 8;
const int ledPingreen = 9;
unsigned long fastLedTimer=0;

void setup() {
// put your setup code here, to run once:
pinMode(SwitchPin, INPUT);
pinMode(mosPin, OUTPUT);
pinMode(ledPinred,OUTPUT);
pinMode(ledPingreen,OUTPUT);
pinMode(SwitchPin2, INPUT);
lcd.begin(16,2);
digitalWrite(ledPinred,HIGH);

lcd.print(“BENVENUTO”);

}

void loop() {
// put your main code here, to run repeatedly:

SwitchState = digitalRead( SwitchPin) ;

SwitchState2 = digitalRead( SwitchPin2) ;

if (previousSwitchState != SwitchState){

if (SwitchState == HIGH ){

Mos =! Mos;
flag=1;
led(); // if I write led() outside if structures, the green led blinks

}
else {
flag=4;
} }

if (previousSwitchState != SwitchState && Mos==0){
if(SwitchState== HIGH){
flag=3;
} }

if (previousSwitchState2 != SwitchState2){
if (SwitchState2 == HIGH ){
flag = 2;

}
}
previousSwitchState = SwitchState;
previousSwitchState2 = SwitchState2;

switch (flag){
case 1:

digitalWrite(ledPinred,LOW);
lcd.clear();
lcd.print("Premi il ");
lcd.setCursor( 0, 1);
lcd.print(“secondo pulsante”);
delay(1000);
lcd.clear();
lcd.print("per accendere ");
lcd.setCursor( 0, 1);
lcd.print("il rele’ 1 ");
delay(1000);
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("Premi il ");
lcd.setCursor( 0, 1);
lcd.print(“terzo pulsante”);
delay(1000);
lcd.clear();
lcd.print("per accendere ");
lcd.setCursor( 0, 1);
lcd.print("il rele’ 2 ");
delay(1000);
lcd.clear();

break;

case 2:
digitalWrite(ledPingreen,HIGH);
delay (250);
digitalWrite(ledPingreen,LOW);
delay (250);
digitalWrite(ledPinred,LOW);
lcd.clear();
lcd.print(" BRAVO ");
delay(1000);
lcd.clear();
break;

case 3:
lcd.clear();
lcd.print(“ARRIVEDERCI”);
delay(1000);
lcd.clear();
digitalWrite(ledPingreen,LOW);
digitalWrite(ledPinred,HIGH);
digitalWrite(mosPin,LOW);
break;
}

}

void led(){
if (millis() - fastLedTimer >= 100)
{
digitalWrite(ledPingreen, !digitalRead(ledPingreen));
fastLedTimer = millis();
}
}

every delay means the code for the blinking led has to wait (its delayed)

your code roughly says

if the button is pushed and its high call led() one time then reset the button state so its not repeated

It might turn on and off every time you push the button as long as you wait 5 seconds between pushes

Welcome to the Forum. Please read the post: How to use this forum - please read.

led(); // if I write led() outside if structures, the green led blinks [/code]

Keep a flag that indicates if the LED needs to flash or not.

void loop() {
  // indicate if led should flash (true) or not (false)
  static bool fFlashLed = false;

This uses a static variable that will remember the value of fFlashLed. You can use it as shown below.

  if (SwitchState == HIGH ) {

    Mos = ! Mos;
    flag = 1;
    fFlashLed = true;
  }
  else {
    flag = 4;
  }

  // flash led if needed
  if (fFlashLed == true)
  {
    led();
  }

Your using a global variable for fastLedTimer. The problem with that is that it can be changed anywhere in your code (setup(), loop() etc) possibly resulting in accidental changes; if fastLedTimer is only used for the flashing of the LED, it's better to move it to the led() function as shown below.

void led() {
  static unsigned long fastLedTimer = 0;
  if (millis() - fastLedTimer >= 100)
  {
    digitalWrite(ledPingreen, !digitalRead(ledPingreen));
    fastLedTimer = millis();
  }
}

To post code, please place your code in code tags.

[code]
led();                          // if I write led() outside if structures, the green led blinks
[/code]

will result in

led();                          // if I write led() outside if structures, the green led blinks

Thanks for your replies. I am sorry but I was busy in February and I wasn't able to answer to you! I am sorry again! About your code:I need to blink the green led and I need to write lcd screen together. I was thinking that the "delay" function ( as you can see in flag 1 ) probably can modify blinking led time because it tells the microcontroller to stop,as gpop1 said. I have to try your code,but my arduino doesn't work now so I am waiting for a new ATmeg328. Thank you for your help.

Now it works!!!! Thank you :) :)