LED not turning off or on when it should

Hi everyone. I am having this issue whereby my led at digital pin 11 isn't turning off once a condition is set. Kindly refer my code below:

    #include <SoftwareSerial.h>
    #include <SD.h>
    #include <Wire.h>
    #include "RTClib.h"

    RTC_DS1307 rtc;
    const int chipSelect = 10;
    int tempLED = 8;
    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    SoftwareSerial HC12(2, 3);
    int sdled = 11; 
   


    
    void setup() {
      Serial.begin(9600);             // Serial port to computer
      HC12.begin(9600);               // Serial port to HC12
      pinMode (tempLED,INPUT);
       pinMode(sdled,OUTPUT);  

   Serial.println("Ready to transmit data wirelessly...");
  

while (!Serial);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
  
    // don't do anything more:
  
    return;

  }

   
    }


    void loop() {
      
         DateTime now = rtc.now();
         boolean sensor = digitalRead(tempLED);
         File dataFile = SD.open("datalog.txt", FILE_WRITE);
        
         
   if (dataFile) {
     digitalWrite (sdled, LOW);
    dataFile.print(now.day(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.year(), DEC);
    dataFile.print(",");
    dataFile.print(daysOfTheWeek[now.dayOfTheWeek()]);
    dataFile.print(",");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
    dataFile.print(",");
    dataFile.print(sensor);
    dataFile.println();
    dataFile.close();
   }
  // if the file isn't open, pop up an error:
  else {

    Serial.println("error opening datalog.txt");
    digitalWrite(sdled, LOW); //NOT WORKING
  }

Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(",");
   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(",");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(",");
    Serial.print(sensor);
    Serial.println();

HC12.print("DATA,");
HC12.print(now.day(), DEC);
    HC12.print('/');
    HC12.print(now.month(), DEC);
    HC12.print('/');
    HC12.print(now.year(), DEC);
    HC12.print(",");
    HC12.print(daysOfTheWeek[now.dayOfTheWeek()]);
    HC12.print(",");
    HC12.print(now.hour(), DEC);
    HC12.print(':');
    HC12.print(now.minute(), DEC);
    HC12.print(':');
    HC12.print(now.second(), DEC);
    HC12.print(",");
    HC12.print(sensor);
    HC12.println(); 
  //  if (sensor == 1){
   
 //     HC12.println("BEEP");
   // }


        
  
     
     

 
    delay(1000);
     }

But if I run the normal blink test it works fine. Really baffles me why this simple LED off/on does not work with these rows of codes :sob:

I see
digitalWrite (sdled, LOW);

I do not see
digitalWrite (sdled, HIGH);

larryd:
I see
digitalWrite (sdled, LOW);

I do not see
digitalWrite (sdled, HIGH);

Yes I want my LED to stay OFF when it detects my sd card is not present. But the LED stays ON :frowning:

Hi,
How have you got the LED wired to your controller?
What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

Another thing: do fix your layout - ctrl-T in the IDE, so all blocks get the same indentation and so. Much more readable.