arduino code for bicycle computer

#include <SD.h>

#include <RTClib.h>

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>

#define ECHO_TO_SERIAL 1 // echo data to serial port

#define WAIT_TO_START 0 // Wait for serial input in setup()

#define greenLEDpin 4

#define redLEDpin 3

#define SWITCH 5

#define BRAKEL 7

#define BRAKER 9

// how many milliseconds before writing the logged data permanently to disk

// set it to the LOG_INTERVAL to write each time (safest)

// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to

// the last 10 reads if power is lost but it uses less power and is much faster!

#define SYNC_INTERVAL 30000 // mills between calls to flush() – to write data to the card

uint32_t syncTime = 0; // time of last sync()

const int chipSelect = 10;

File logfile;

int val2 = 0;

int val1 = 0;

int val = 0; //Udes to store input value var = variable

int previousVal = 0;

int previousVal1 = 0;

int previousVal2 = 0;

void error(char *str)

{

Serial.print(“error: “);

Serial.println(str);

// red LED indicates error

digitalWrite(redLEDpin, HIGH);

while(1);

}

//time coding:

RTC_DS1307 RTC;

//Void setup

void setup(void)

{

Serial.begin(9600);

Serial.println();

#if WAIT_TO_START

Serial.println(“Type any character to start”);

while (!Serial.available());

#endif //WAIT_TO_START

// initialize the SD card

Serial.print(“Initializing SD card…”);

// make sure that the default chip select pin is set to

// output, even if you don’t use it:

pinMode(10, OUTPUT);

// see if the card is present and can be initialized:

if (!SD.begin(chipSelect)) {

Serial.println(“Card failed, or not present”);

// don’t do anything more:

return;

}

Serial.println(“card initialized.”);

// create a new file

char filename[] = “LOGGER00.CSV”;

for (uint8_t i = 0; i < 100; i++) {

filename[6] = i/10 + ‘0’;

filename[7] = i%10 + ‘0’;

if (! SD.exists(filename)) {

// only open a new file if it doesn’t exist

logfile = SD.open(filename, FILE_WRITE);

break; // leave the loop!

}

}

if (! logfile) {

error(“couldnt create file”);

}

Serial.print(“Logging to: “);

Serial.println(filename);

Wire.begin();

if (!RTC.begin()) {

logfile.println(“RTC failed”);

#if ECHO_TO_SERIAL

Serial.println(“RTC failed”);

#endif //ECHO_TO_SERIAL

}

logfile.println(“Speed,brakeL,brakeR”);

#if ECHO_TO_SERIAL

Serial.println(“Speed,brakeL,brakeR”);

#endif

//#if ECHO_TO_SERIAL// attempt to write out the header to the file

// if (logfile.writeError || !logfile.sync()) {

// error(“write header”);

// }

//#endif

pinMode(redLEDpin, OUTPUT);

pinMode(greenLEDpin, OUTPUT);

// If you want to set the aref to something other than 5v

//analogReference(EXTERNAL);

}

void loop()

//biede remmen

{

if (val1 == LOW && val2 == LOW){

digitalWrite(greenLEDpin, HIGH);

// log milliseconds since starting

uint32_t m = millis();

logfile.print(m);

logfile.print(“, “);

logfile.print(“Bei REMMEN”); // milliseconds since start (m)

logfile.println();

#if ECHO_TO_SERIAL

Serial.print(m);

Serial.print(“, “);

Serial.print(“BEIDE REMMEN”); // milliseconds since start

Serial.println();

#endif

}

else{

//Linker rem

val1 = digitalRead(BRAKEL);

val= digitalRead(SWITCH); //read the SWITCH value and store it

if (val1 == LOW){

digitalWrite(greenLEDpin, LOW);// turn the led on

}

else{

digitalWrite(greenLEDpin, HIGH);

// log milliseconds since starting

uint32_t m = millis();

logfile.print(m);

logfile.print(“, “);

logfile.print(“REM LINKS”); // milliseconds since start (m)

logfile.println();

#if ECHO_TO_SERIAL

Serial.print(m);

Serial.print(“, “);

Serial.print(“REM LINKS”); // milliseconds since start

Serial.println();

#endif

}

}

}

//RECHTER REM

{

val2 = digitalRead(BRAKER);

val= digitalRead(SWITCH); //read the SWITCH value and store it

if (val2 == LOW){

digitalWrite(greenLEDpin, LOW);// turn the led on

}

else{

if (val == HIGH) {

previousVal = HIGH;

}

else{

if (previousVal != LOW) {

previousVal = LOW;

digitalWrite(greenLEDpin, HIGH);

// log milliseconds since starting

uint32_t m = millis();

logfile.print(m);

logfile.print(“, , “);

logfile.print(“REM RECHTS”); // milliseconds since start (m)

logfile.println();

#if ECHO_TO_SERIAL

Serial.print(m);

Serial.print(“, , “);

Serial.print(“REM RECHTS”); // milliseconds since start

Serial.println();

#endif

}

}

}

}

}

//REED SENSOR

val= digitalRead(SWITCH); //read the SWITCH value and store it

if (val==HIGH){

digitalWrite(greenLEDpin, LOW);// turn the led on

previousVal = HIGH;

}

else{

if (val1 == LOW && val2 == LOW){

digitalWrite(greenLEDpin, HIGH);

if (previousVal !=LOW) {

previousVal = LOW;

// log milliseconds since starting

uint32_t m = millis();

logfile.print(m); // milliseconds since start

logfile.println();

#if ECHO_TO_SERIAL

Serial.print(m); // milliseconds since start

Serial.println();

#endif

}

}

}

// Now we write data to disk! Don’t sync too often – requires 2048 bytes of I/O to SD card

// which uses a bunch of power and takes time

if ((millis() – syncTime) < SYNC_INTERVAL) return;

syncTime = millis();

// blink LED to show we are syncing data to the card & updating FAT!

digitalWrite(redLEDpin, HIGH);

logfile.flush();

digitalWrite(redLEDpin, LOW);

}

Advertisements