O
O
oc1tane2017-07-24 15:49:21
Arduino
oc1tane, 2017-07-24 15:49:21

Arduino. Flashing LED from setup?

Toaster gurus help me out, I can’t deal with flashing in arduino. As I know setup - it is initialized only 1 time, and loop - has a cycle.
Here is a simple example of flashing an LED

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

I want to make a blink in response if it is not in the W5100 drive, the SD card
is something like that (code snippet)
if (!SD.begin(4)) { func(); }

//и где-то вынесенное описание 
void func();

But the question is how will it blink if there is no cycle? in setup?
How can I correctly make the LED blink on the reaction if there is no SD?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MonaGioconda, 2017-07-24
@MonaGioconda

I warn you right away that this is not the most elegant solution, but it seems the most banal.
Throw the function of blinking the LED into an endless loop with one or another exit condition. As far as I understand, in your case, the condition will be the presence of an SD card.
Example:

void loop() {
    while (!SD.begin(4)) { 
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);                       // wait for a second
        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        delay(1000); 
    }
}

Haven't worked with the W5100 so I'm not entirely sure about this option, but you can also look into the possibility of using interrupts. But only under the condition that the inserted / non-card affects the HIGH / LOW level of this pin'a.
UPD. This option is best suited if you are using an Arduino Due board.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question