Team 4
From BenningtonWiki
Code 5/6/09:
<source lang="c">
- include <sd_raw.h>
- include <string.h>
- include <stdio.h>
- include <stdarg.h>
- define LED_PIN 7
- define RESET_PIN 5
- define READOUT_PIN 6
- define HIGHONE 4
- define HIGHTWO 3
- define ZERO_FT_LIGHT_PIN 3
- define ZERO_FT_TEMP_PIN 4
- define FIVE_FT_LIGHT_PIN 1
- define TEN_FT_LIGHT_PIN 2
- define FIFTEEN_FT_LIGHT_PIN 0
- define FIFTEEN_FT_TEMP_PIN 5
- define LOG_INTERVAL 1000 // milliseconds
//SD STUFF: //Pins 10, 11, 12, 13 used for SD card reading
- define CARD_ID 0x68656c6f
long int log_count; void rsprintf(const char *format, ...); void sd_format(void); void sd_check_format(void); void sd_log(struct data* storage); void sd_print_log(void);
int temperatureRead(int pinNumber);
struct data { unsigned long int time_stamp; int zero_ft_light; int zero_ft_temp; int five_ft_light; int ten_ft_light; int fifteen_ft_light; int fifteen_ft_temp; };
void setup(void) {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(RESET_PIN, INPUT);
pinMode(READOUT_PIN, INPUT);
pinMode(HIGHONE, OUTPUT);
pinMode(HIGHTWO, OUTPUT);
digitalWrite(RESET_PIN, HIGH);
digitalWrite(READOUT_PIN, HIGH);
//SD Stuff: struct sd_raw_info info; // unsigned long write_test_value, read_test_value; // sd_raw_init(); sd_raw_get_info(&info); sd_check_format();
//////// SD FORCE FORMAT! //// //sd_format(); ///////
//sd_print_log();
}
struct data storage;
void loop(void) { unsigned long int this_time; static bool led_switch; static long int next_blink, next_log;
digitalWrite(HIGHONE, HIGH);
digitalWrite(HIGHTWO, HIGH);
this_time = millis();
if (this_time >= next_blink) { led_switch = !led_switch; digitalWrite(LED_PIN, led_switch); next_blink = this_time + 1000;
if (digitalRead(READOUT_PIN) == HIGH) {
sd_print_log();
//Serial.print("READOUT!");
}
if (digitalRead(RESET_PIN) == HIGH) {
sd_format();
//Serial.print("RESET!");
}
}
if (this_time >= next_log) {
storage.time_stamp = this_time;
storage.zero_ft_light = analogRead(ZERO_FT_LIGHT_PIN); storage.zero_ft_temp = temperatureRead(ZERO_FT_TEMP_PIN); storage.five_ft_light = analogRead(FIVE_FT_LIGHT_PIN); storage.ten_ft_light = analogRead(TEN_FT_LIGHT_PIN); storage.fifteen_ft_light = analogRead(FIFTEEN_FT_LIGHT_PIN); storage.fifteen_ft_temp = temperatureRead(FIFTEEN_FT_TEMP_PIN);
sd_log(storage);
next_log = this_time + LOG_INTERVAL; } }
int temperatureRead(int pin)
{
int tempc = 0, tempf= 0;
int i;
tempc = ( 5.0 * analogRead(pin) * 100.0) / 1024.0; tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
return tempf; }
void sd_read(unsigned long offset, void *buffer, int length) {
sd_raw_read(offset, (uint8_t *)buffer, length);
}
void sd_write(unsigned long offset, void *buffer, int length) {
sd_raw_write(offset, (uint8_t *)buffer, length); sd_raw_sync();
}
void sd_format(void) {
long id = CARD_ID;
sd_write(0, (uint8_t *) &id, sizeof(id));
log_count = 0;
sd_write(4, (uint8_t *) &log_count, sizeof(log_count));
}
void sd_check_format(void) { long id; sd_read(0, (uint8_t *) &id, sizeof(id)); if (id != CARD_ID) sd_format(); sd_read(4, (uint8_t *) &log_count, sizeof(log_count)); }
void sd_log (struct data storage) {
sd_read(4, (uint8_t *) &log_count, sizeof(log_count)); sd_write(log_count * sizeof(struct data) + 8, (uint8_t *) &storage, sizeof(storage)); ++log_count; sd_write(4, (uint8_t *) &log_count, sizeof(log_count)); }
void sd_print_log(void) { int i; struct data storage;
sd_read(4, (uint8_t *) &log_count, sizeof(log_count)); for (i = 0; i < log_count; ++i) { sd_read(i * sizeof(storage) + 8, (uint8_t *) &storage, sizeof(storage)); // rsprintf("Time: %d\n0ft Light: %d, 0ft Temp: %d\n5ft Light: %d\n10ft Light: %d\n15ft Light: %d, 15ft Temp: %d\n\n", storage.time_stamp, storage.zero_ft_light, storage.zero_ft_temp, storage.five_ft_light, storage.ten_ft_light, storage.fifteen_ft_light, storage.fifteen_ft_temp);
// rsprintf("%ld %d %d %d %d %d %d \n", storage.time_stamp, storage.zero_ft_light, storage.zero_ft_temp, storage.five_ft_light, storage.ten_ft_light, storage.fifteen_ft_light, storage.fifteen_ft_temp);
Serial.print(i);
Serial.print(" ");
Serial.print(storage.time_stamp);
Serial.print(" ");
Serial.print(storage.zero_ft_light);
Serial.print(" ");
Serial.print(storage.zero_ft_temp);
Serial.print(" ");
Serial.print(storage.five_ft_light);
Serial.print(" ");
Serial.print(storage.ten_ft_light);
Serial.print(" ");
Serial.print(storage.fifteen_ft_light);
Serial.print(" ");
Serial.print(storage.fifteen_ft_temp);
Serial.print("\n");
}
}
void rsprintf(const char *format, ...) {
static unsigned char inited = 0;
char msg[100];
va_list ap;
if (!inited) {
Serial.begin(9600);
inited = 1;
}
va_start(ap, format);
vsnprintf(msg, sizeof(msg)-1, format, ap);
Serial.print(msg);
va_end(ap);
} </source>

