I want to blink a led to see how many stations are connected to the esp32 wifi accespoint.
if 1 station is connected it has to blink 1times in 5 seconds
if 2 stations are connected it has to blink 2 times in 5 seconds
,
,
,
if 10 stations are connected is has to blink 10 times in 5 seconds
i cannot think of a way to manipulate the led that it will blink like i want.
The numer of stations is printed on teh serial monitor, that part is already working.
the led is the pin 13 on the esp it is the on board led
If you divide the number of seconds (ie 5) by the number of stations and divide the result by 2 you get the on/off period of the blink and you already know how many blinks are needed in each case. Are you familiar with using mills() for timing as in the BlinkWithoutDelay example ?
UKHeliBob:
If you divide the number of seconds (ie 5) by the number of stations and divide the result by 2 you get the on/off period of the blink and you already know how many blinks are needed in each case. Are you familiar with using mills() for timing as in the BlinkWithoutDelay example ?
However, if you are willing to have your program do nothing but blink the LED for 5 seconds then you may as well use delay() for the timing of the blinks.
1 station 1sec on 4 sec off
2 stations 1 sec on 1sec off 1 sec on 1sec off
3 stations 1 sec on 0,5sec of 1 sec on 0,5sec off
Was that the goalposts moving ?
How about an array of on and off periods in two columns and one row per number of stations ? That way you could have any combination of on/off for any number of stations.
How about an array of on and off periods in two columns and one row per number of stations ? That way you could have any combination of on/off for any number of stations.
// Blink two LEDs.
// Hardware: 2 LEDs with resistors. Pushbutton.
// If button is not pressed:
// LED1 will blink: 100 ms on, 2 seconds off
// LED2 will be off
// While botton is pressed:
// LED1 will blink 70 ms on, 70 ms off
// LED2 will blink 50 ms on, 500 ms off
const int buttonPin = 2;
const int LED1 = 4;
const int LED2 = 5;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) { // if the botton is not pressed
blink(LED1, 100, 2000);
digitalWrite(LED2, LOW);
}
else { // the button is pressed
blink(LED1, 70, 70);
blink(LED2, 50, 500);
}
}
void blink(byte ledPin, unsigned int onTime, unsigned int offTime) {
digitalWrite( ledPin, (millis() % (onTime + offTime)) < onTime );
}
You need to read up on arrays. Something for you to try
unsigned long periods[][2] =
{
{1000, 4000}, {1000, 1000} //ON/OFF periods
};
const byte ledPin = 3;
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); //start with LED off
blinkLed(1); //blink once
delay(3000); //short delay for testing
blinkLed(2); //blink twice
}
void loop()
{
}
void blinkLed(byte stations)
{
long onPeriod = periods[stations - 1][0]; //array index starts at zero
long offPeriod = periods[stations - 1][1];
Serial.print("On period : ");
Serial.println(onPeriod);
Serial.print("Off period : ");
Serial.println(offPeriod);
Serial.print("Number of blinks : ");
Serial.println(stations);
Serial.println();
for (int count = 0; count < stations; count++)
{
digitalWrite(ledPin, LOW); //turn on LED
delay(onPeriod);
digitalWrite(ledPin, HIGH); //turn off LED
delay(offPeriod);
}
}
Add more on/off periods to the array to do what you want. Bear in mind that using the delay() function will stop the program doing anything else during the time that the LED is blinking.
The LEDs on my test system are on when LOW so change the digitalWrite()s to suit yours if they are on when HIGH.
when the led is blinking it wil not execute the http request's ?
From reply #4
However, if you are willing to have your program do nothing but blink the LED for 5 seconds then you may as well use delay() for the timing of the blinks.
Goalposts moved again.
now the expample with array should be converted to milis . . . .
Feel free to have a go after reading the links I gave you in reply #4