I am trying to make my built in LED on my arduino blink every 1 second while recording my data and putting it in the EEPROM, but I am not sure how to do that. I am supposed to print data to the EEPROM every minute, until the EEPROM is full and while that is going on my built in LED should blink for approx 1 second. My problem seems to be that I am setting a delay to delay how long the data is stored on the EEPROM, but it is also affecting the time it takes for my LED to blink as it is waiting to blink as the data is stored. Any help would be appreciated, the code is below:

#include<EEPROM.h>
const int SWITCH = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly:
  int addr = 0;
  int r = analogRead(0);

  if (digitalRead(SWITCH) == LOW) {
    Serial.println("----Recording----");
  }
  while (addr <= 1024) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay (250);
    digitalWrite(LED_BUILTIN, LOW);
    delay(750);
    Serial.println(r);
    EEPROM.put(addr, r);
    addr = addr + 2;
    delay(600);
  }
  while (true);
}
  • If you add up all your delays you get 1600ms. Removing the last delay(600); would make the duration of you loop a lot closer to 1 second. Indeed the other code would take a bit of time to, but my guess would be that it only takes 5ms per cycle. Depending on you application, that might be good enough. It might also be the case that you need it so precise that the precision of the crystal the Arduino uses isn't good enough. – Gerben Dec 3 at 15:56

Do not use delay(). Use the example Blink without delay example as the starting point.

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Find it under Examples 02.Digital

Simple code like delay() is called blocking code. It prevents other things from happening until t is through. That is acceptable in very sequential linear programs where only one thing is ever happening. And you have discovered the limitation of that. Time to get more sophisticated in your programming.

Please open the example and play with it. Make sure you understand it before thinking of how to modify your code.

  • Would you be able to clean up my code to do this? I'm not sure how I am supposed to go about doing what you said – Thomas Dec 3 at 2:30
  • 1
    I added more to the answer. Open the example and learn what it does. Don't think about your program yet. You need to learn this kind of thing and not just copy someone's code. – Rudy Dec 3 at 2:34

As Rudy mentions in his answer, don't use delay(), and start with the BlinkWithoutDelay example that comes with the Arduino IDE. Here is a sketch to get you started. If you compare it to the BlinkWithoutDelay example, it's very similar, except I added some button code to "start" and "stop" the LED flashing.

const byte SWITCH = 4;
const byte ledPin = 13;
const unsigned long oneSecond = 1000;
unsigned long previousMillis = 0;
byte ledState = LOW;
byte recording = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);
}

void loop(){

  unsigned long currentMillis = millis();

  if(digitalRead(SWITCH) == LOW){
    recording = 1;
  }
  else{
    recording = 0;
    digitalWrite(ledPin, LOW);
  }

  if(currentMillis - previousMillis >= oneSecond){

    previousMillis = currentMillis;

    if(recording){
      if(ledState == LOW){
        ledState = HIGH;
      }
      else{
        ledState = LOW;
      }
    }
    digitalWrite(ledPin, ledState);
  }
}
  • 3
    I didn't post modified code because I want him to understand what is happening in the example without proceeding with his program. I feel it is more important to learn than to just get an answer. – Rudy Dec 3 at 2:49
  • I agree with you Rudy. As I mentioned in my answer, this sketch is almost identical to the BlinkWithoutDelay example, with the addition of the "button code" that Thomas already wrote. I just wanted Thomas to start heading in the right direction. Thomas still has to figure out how to add a "minute" timer to the sketch, get the analog reading and store it in EEPROM. That will keep him busy for a while :) – VE7JRO Dec 3 at 3:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.