I appreciate the help of this forum very much and it`s my first time to ask you experts befor driving crazy...
ArduinoUno and SDcard-BreakOutboard and DistanceSensor HC-SR04 are not working well together. The problem - as far as I found out - is in this statement:
if (!sd.begin(sdChipSelect) || !file.open("DATALOG.TXT", O_CREAT | O_WRITE | O_APPEND)) {
// Replace this with something for your app.
Serial.println(F("SD problem"));
//while(1);
}
When it is erased the distance sensor works perfectly, if it is in place it shows 0 cm (and that for days! ;-) ). I read something that SdFat ist doing something with Pin10 but also putting it on OUTPUT - HIGH or LOW didn't do the trick. Any ideas?
Kind regards
Rob
The "minimal" code:
/*
SD card datalogger
This example shows how to log data
to an SD card using the SdFat library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 7 (for MKRZero SD: SDCARD_SS_PIN)
*/
#include <SdFat.h>
const uint8_t sdChipSelect = 7;
// SD file system.
SdFat sd;
// Log file.
SdFile file;
// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
// Maximum Distance is 400 cm
unsigned long echo = 10;
int ultraSoundSignal = 9; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
void setup() {
// Serial Port
Serial.begin(9600);
// Pin 9 = TriggerPin
pinMode(ultraSoundSignal,OUTPUT);
// Initialize the SD and create or open the data file for append.
if (!sd.begin(sdChipSelect) || !file.open("DATALOG.TXT", O_CREAT | O_WRITE | O_APPEND)) {
// Replace this with somthing for your app.
Serial.println(F("SD problem"));
//while(1);
}
Serial.println("card initialized.");
delay(1000);
}
void loop() {
Serial.println("Beginn");
// Distanzmessung mit HC-SR04
int x = 0;
x = ping();
Serial.print(x); Serial.println(" cm");
delay(1000); //delay 1/4 seconds.
}
unsigned long ping()
{
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
// please note that pulseIn has a 1sec timeout, which may
// not be desirable. Depending on your sensor specs, you
// can likely bound the time like this -- marcmerlin
// echo = pulseIn(ultraSoundSignal, HIGH, 38000)
echo = pulseIn(ultraSoundSignal, HIGH,38000); //Listen for echo
ultrasoundValue = (echo / 58.138);// * .39; //convert to CM then to inches
return ultrasoundValue;
}