/*
declarations for sd card reader
hookup is different for mega from uno in examples
Miso pin 50 (output)
Mosi pin 51 (input)
Sck pin 52 (clock)
chip select pin 53, not 4 as in uno
vcc to 5V. 3.3V not used
*/
#include <SD.h> //Load SD card library
#include<SPI.h> //Load SPI Library, necessary for SD card
const int chipSelect = 53; //chipSelect pin for the SD card Reader - 4 for uno 50 for mega
File RailSensorData; //Data object to write sensor data to
// end sd declarations
#include "Wire.h" // imports the wire library for talking over I2C to addressable sensors
/* declarations for distance (rotary encoder) on pin 2
encoder has 3 pins: 5V, Gnd, and output.
connect output to (digital) pin 2
*/
#define ENCODER_CLK 2 // get pulse from encoder on pin 2.
double distance; // pulse counts so integer values. It may generate large numbers
int newClk;
int lastClk;
float speed; // speed from rotary sensor and clock - add later
// end encoder declarations
// declarations for tie sensor (metal detector) on pin ?
boolean tie;
// end declarations for tie sensor
// declarations for tilt (level) sensor on pin ?
float level; // check what the level sensor puts out - add later
// end level declarations
// declarations for accelerometer on pin ?
// check what outputs from this device
// end declarations for accelerometer
// declarations for left and right rail gap sensors on pins ??
// device will use an optical sensor, so should be just yes/no input, but input value and split to true/false.
// end declarations for rail gap sensors
void setup(){
// general setup
Serial.begin(9600); //turn on serial monitor
// SD card reader setup
pinMode(10, OUTPUT); //SD library wants pin 10 reserved, even though not used.
// see LESSON 21 at www.toptechboy.com.
SD.begin(chipSelect); //Initialize the SD card reader on pin 53
// end sd setup
// distance measure setup
// distanceSensor.begin(); //initialize DistanceSensor
pinMode(ENCODER_CLK, INPUT);
// end distance measure setup
// tie sensor
// level sensor
// accelerometer
// left and right rail gap sensor
}
void loop() {
//
// check if rotary sensor moved. Clk is input from rotary enoder
newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
distance = distance + newClk;} // if click, then sensor moved
// read out to serial monitor for troubleshooting
// copied from example
Serial.print("The distance is: "); //Print Your results
Serial.print(distance);
Serial.println(" pulses");
delay(250); //Pause between readings - take this out later and only output on tie sensor
// read out to SD card. Railsensordata is a file object for SD card.
RailSensorData = SD.open("Rail.txt", FILE_WRITE);
Serial.print(RailSensorData);
if (RailSensorData) {
RailSensorData.print(distance); //write distance data to card
RailSensorData.print(","); //write a commma
RailSensorData.println(level); //write level and end the line (println)
RailSensorData.close(); //close the file
}
}