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?
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.
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?
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?
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);
}
}