Writes to Serial Com but not Log file

When I break a beak I get a reading on the Serial Com, but this is not flowing onto the Log.txt

#include <SD.h>

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 8;
int pow_pin = 8;
//IR Distance Sensor Pins
  int IR1_pin = 0;
float refresh_rate = 0.0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
  
  //Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT);  
  digitalWrite(pow_pin, HIGH);
  
  //check if card is ready
    
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
  
  //Read the Configuration information (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");
    
    float decade = pow(10, (commandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }
  
}

void loop()
{{
  int value = analogRead(IR1_pin);

  if (value > 375 || value < 300)
      Serial.println(value); 
  delay(50);   //wait half a second, then check again.
}
    //Open a file to write to
  //Only one file can be open at a time
 int IR1_val = analogRead(IR1_pin);
  File logFile = SD.open("LOG.txt", FILE_WRITE);
  if (logFile)
  {
    logFile.println(IR1_pin);
    logFile.close();
    Serial.println(IR1_pin);
  }
  else
  {
    Serial.println("LOG.txt");
    Serial.println("Couldn't open log file");
  }
  delay(50);
}

I can see the following on serial com (the high numbers are detected movement -

Initializing Card
Card Ready
Reading Command File
Refresh rate 1000.00ms
0
0
0
567
589
389
o
o
o

however when I look in the log file i just see this a line of 0.

Please be patient as this is all very new so I am trying to build this bit by bit. I have run other sketches which write ok to the SD card. Is there something simple I have missed from my sketch which is preventing what I see on the serial port appearing on the text file?

  int IR1_pin = 0;
    logFile.println(IR1_pin);

Might have something to do with it.

{{

int value = analogRead(IR1_pin);

if (value > 375 || value < 300)
      Serial.println(value);
  delay(50);  //wait half a second, then check again.
}

Adding extra {}s around code has no purpose. If your goal was to create a small loop there, you need you read about while() loops.

Apparently FILE_WRITE means (O_READ | O_WRITE | O_CREAT) which does not include O_APPEND so when the file is opened it gets erased.

Try "FILE_WRITE | O_APPEND" instead.

Thanks for the suggestion.
This part of the sketch is telling the arduino that I am using analogue pin 0.

int IR1_pin = 0;

Do I need to change the part between brackets after log.Fileprintln ? If so what should it read?

Also I have added the following -
File logFile = SD.open("LOG.txt", FILE_WRITE | O_APPEND); No change.

ripon1:
This part of the sketch is telling the arduino that I am using analogue pin 0.

int IR1_pin = 0;

Yes but the ONLY thing you print to the logfile is the value of IR1_pin (which you set to 0). Your code is telling the Arduino to ONLY print 0 to the file. You never print anything else to the file.

Do I need to change the part between brackets after log.Fileprintln ? If so what should it read?

Presumably you want to store the value read from the pin, rather than the pin number, so you need another analogRead().

Many thanks for all of your responses, which I think I have incorporated into my sketch.

void loop()
{
  int value = analogRead(IR1_pin);

  if (value > 375 || value < 300)
      Serial.println(value); 
  delay(50);   //wait half a second, then check again.

    //Open a file to write to
  //Only one file can be open at a time
 int IR1_val = analogRead(IR1_pin);
  File logFile = SD.open("LOG.txt", FILE_WRITE | O_APPEND);
  if (logFile)
  {
    logFile.println(analogRead(IR1_pin));
      logFile.close();
    Serial.println(IR1_pin);

  }
  else
  {
    Serial.println("LOG.txt");
    Serial.println("Couldn't open log file");
  }
  delay(50);
}

Just with this section of the sketch I now have the IR value going to the sd card which is great. I am still missing something as it is not picking up on the if value is over 375 or under 300 aspect of the code. How do I include this so it will accept this onto the SD card?

  if (value > 375 || value < 300)
      Serial.println(value);

You need to have more than just the Serial.println() as the body of your if statement.

Do I need a line between serial println and delay? If so what do I need to build in please?

  int value = analogRead(IR1_pin);

  if (value > 375 || value < 300)
      Serial.println(value);

You read and print a value if the value is out of range.

Then,

 int IR1_val = analogRead(IR1_pin);
  File logFile = SD.open("LOG.txt", FILE_WRITE | O_APPEND);
  if (logFile)
  {
    logFile.println(analogRead(IR1_pin));
      logFile.close();
    Serial.println(IR1_pin);

  }

you read a new value and write it to the file. Why are you reading another value? Why are you unconditionally writing it to the file? Why are you then surprised that the serial port output does not match the log file contents?

Does it matter that a value is out of range, if it doesn't matter when that value occurred?

So I have changed it to this

void loop()
{
  int value = analogRead(IR1_pin);

  if (value > 375 || value < 300)
      Serial.println(value); 
  delay(50);   //wait half a second, then check again.

    //Open a file to write to
  //Only one file can be open at a time
 int IR1_val = Serial.println(value);
  File logFile = SD.open("LOG.txt", FILE_WRITE | O_APPEND);
  if (logFile)
  {
    logFile.println(Serial.println(value));
      logFile.close();
    Serial.println(value);

  }

This does return the full reading of the IR pin, but I was trying to filter everything out between 300 and 375. That way I would only see the highs and lows which relates back to a break in the beam of the IR on pin 0.

if (value > 375 || value < 300)
{
Serial.println(value);

delay(50); //wait half a second, then check again.

//Open a file to write to
//Only one file can be open at a time
int IR1_val = Serial.println(value);
File logFile = SD.open("LOG.txt", FILE_WRITE | O_APPEND);
if (logFile)
{
logFile.println(Serial.println(value));
logFile.close();
Serial.println(value);
}
}

Great thanks for the response.

The Serial Com is working as it was - when I pass the IR the number goes high (between 375 - 700.

The SD card has a few 5 on it. Do these 5s represent each number in the serial com?

Look at what you are writing to the serial port and what you are writing to the log file. When they match, life will be good.