how to control led blinking times ?

i want to blink a led only 1 , 2 or 3 times each time when a Hall-sensor is giving a signal

at this time the led blinks only once after a Hall-sensor signal, no matter which void flikker() function i write in the void loop(). Its probably something very simple.

const int ledPin = 13; 
const int  hallPin = 2;  // the pin that the hallsensor is attached to
int hallCounter = 0;  
int hallState = 0;        
int lasthallState = 0;  
int delaytime = 10000;
void setup() {

  pinMode(hallPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  hallState = digitalRead(hallPin);

  if (hallState != lasthallState) {
    if (hallState == LOW) {
      hallCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(hallCounter);
    } 
    else {
      Serial.println("off"); 
    }
  }
  lasthallState = hallState;

  if (hallCounter>= 1) {
    flikker2();
    }
   
   else {
   digitalWrite(ledPin, LOW);
  }
  
  
}
void flikker1(){

    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime);
    hallCounter--;
}

void flikker2(){

    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime); 
    hallCounter--;
}
void flikker3(){
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime); 
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime); 
    hallCounter--;
}

Thanks for explaining and helping.

You are - for some bizarre reason - using delayMicroseconds(10000) instead of delay(10). In either case, you are saying switch on for 1/100 of a second, switch off for 1/100 of a second and so on, which is to say telling it to flash at 50 Hz with a 50% duty cycle. That is not "flashing" or "flickering", it is merely dimming your LED.

Here is a tip.
When ever you find yourself writing the same code over and over it is time to do something else. In your case you need to use a for loop.

From what I can see you just need one function and pass in the variable containing the number of times you want the led to be on or off.

void flicker(int times){
For(int i =0; i < times; i ++){
    digitalWrite(ledPin, HIGH);
    delayMicroseconds(delaytime);
    digitalWrite(ledPin, LOW);
    delayMicroseconds(delaytime);
    }
}

Then when you want to use simply

Flicker(5);

Amazing!

In 15 min. all i wanted to know. :smiley:

Thanks for the very fast reply’s, answers, and tips.

Of course, there is lot more to it.

You should not be using any form of "delay()" because that prevents you doing other things during the delay time. You should have an overall code structure which continuously cycles through the main loop, making decisions according to various events, such as whether your sensor is triggered and whether a given time has passed by comparing the current millis() value to the previous event time of the particular nature.

All such decisions should be made and acted on without impeding the continuous cycling of the main loop(), one decision after another, again and again. This process will continue at the maximum speed of the processor, generally thousands of times each second so that each particular event is measured with fine granularity, yet all processes seem to proceed simultaneously.

You need to learn this structure early on in your career.

Is it also possible to do the flicker() function without delay?

I have try to replace the "digitalwrite led high-low part " with the basic “blink without delay” but it wont work.

}
  void flicker(int times){
for(int i =0; i < times; i ++){
  unsigned long currentMicros = micros();
  if(currentMicros - previousMicros >= interval) {
    previousMicros = currentMicros;   
    if (ledState == LOW)
      ledState = HIGH;
    else
     ledState = LOW;
    digitalWrite(ledpin, ledState);
  }

Heh, heh, no!

What I was explaining was a totally different concept in writing code in order to execute "blink without delay". You have to literally turn the code "inside out", removing any obstructions such as "for" or "while" loops.

Instead you use counters which decrement each time an event is detected as the loop is traversed. In other word, you test for the event, if it has not occurred, you skip on but when it does occur, you decrement the counter and take any required action.

Pardon me, but I'm off to bed just now ...

Is it also possible to do the flicker() function without delay?

Yes but not that way. Actually delay is fine if either the delay is short enough or you don't want to do anything else with the time that delay pisses away.