Go Down

Topic: Arduino Stalker Board Help (Read 901 times) previous topic - next topic

andyh

Dec 19, 2010, 09:32 pm Last Edit: Dec 19, 2010, 09:33 pm by andyh Reason: 1
I'm brand new to Arduino and electronics in general.  I purchased a Stalker Electricity Monitor kit from airsensor.co.uk to log electricity useage using pulses from the electricity meter LED.

I put the board together today with all the required components and managed to get the sketch uploaded.  However the output to the SD Card does not contain the correct time or date.  I've been trying to work out how to set the time on the board.  The sketch has the following at the beginning:
Quote

#include <Fat16.h>      // the SD Card library]
#include <Wire.h>  
#include <Time.h>  
#include <DS1307RTC.h> // a basic DS1307 library]



I thought this would allow the time to be set during the upload but obviously not ::).

I know this is basic stuff but if anyone can help me get the time and date set correctly it would be appreciated.  I'm using a MAC for the uploads.

The other problem I'm having is with the sensor detecting the pulses from the meter.  One of the LEDs is supposed to blink everytime it receives a pulse.  However mine is staying on constantly.  Again any advice much appreciated.

Thanks for any replies and apologies for the basic questions.  :)

James C4S

In the future, it would be more helpful to post the full code you are using instead of just a few lines.  Also, in the message editor there is a icon you can use to post code ().

Regardless, I found the project code.  Looking through it, there is nothing in the project to set the clock.  So you might want to search this forum or google for the DS1307 library examples.  It isn't difficult to set the time on a DS1307, but you need to do it any time the chip loses power.

You should be able to compile a sketch to set the RTC and then reload the sketch you are using to do the data logging.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

andyh

#2
Dec 19, 2010, 11:03 pm Last Edit: Dec 19, 2010, 11:14 pm by andyh Reason: 1
Points noted and thanks for the tip - have included the full code below.

I have downloaded the Time library from the Playground but I obviously need library examples for the DS1307.  I shall look around and see what I can find.

Code: [Select]

/*
* SimpleMeterLogger.pde - Meter reading logic for logging
* This version logs pulses on digital pin 3
*
* Copyright Michael Margolis 2010
* In association with www.airsensor.co.uk
*/


#include <Fat16.h>      // the SD Card library - http://code.google.com/p/fat16lib/
#include <Wire.h>  
#include <Time.h>  
#include <DS1307RTC.h> // a basic DS1307 library - http://www.arduino.cc/playground/Code/Time


const long logInterval = 10; // the interval in seconds between each log

// LED pins
const int  dataLedPin    =  4;  // LED indicating sensor data is received
const int  logLedPin     =  5;  // LED flashes during a log attemp
const int  fileLedPin    =  6;  // LED turns on if a file error occurs

const char *fileName = "logdata.csv";  // the name of the log file

const int logInterrupt = 1; // ATmega 168 and 328 - interrupt 0 = pin 2, 1 = pin 3
const int interruptPin = 3;

SdCard card;
Fat16 LogFile;

volatile unsigned long wattSensor = 0;  // Counts power pulses in interrupt, 1 pulse = 1 watt
volatile byte          dataState  = 0;  // used to blink the Led to indicate data updates
unsigned long totalWatts = 0;  // Total power used since the sketch started ???

time_t nextTrigger;


void setup(void)
{
 setSyncProvider(RTC.get);   // the function to get the time from the RTC

 pinMode(dataLedPin, OUTPUT);    // LED interrupt indicator initialization
 pinMode(logLedPin, OUTPUT);      
 pinMode(fileLedPin, OUTPUT);  

 pinMode(interruptPin, INPUT);    
 digitalWrite(interruptPin, HIGH);  
 
 attachInterrupt(logInterrupt, interruptHandler, FALLING);  
 nextTrigger = now() + logInterval; // schedule the logging time
 
 // initialize the SD card
 if (!card.init())
    error(1);
 
 // initialize a FAT16 volume
 if (!Fat16::init(&card))
     error(2);

 // open file for append, create if it doesn't exist  
 if (!LogFile.open(fileName, O_CREAT | O_APPEND | O_WRITE))
     error(3);

 // clear write error
 LogFile.writeError = false;
 LogFile.println("Start");
}

void loop(void)
{  
 time_t timeStamp = now();  
 if( timeStamp >= nextTrigger)
 {
    digitalWrite(logLedPin, HIGH);
    printDateTime(timeStamp);
    MeterPulseLog();
    LogFile.println();
   // write the data to the card at the end of every line
    if (!LogFile.sync())
       error(4);
       
    nextTrigger += logInterval;// schedule the next log time        
    digitalWrite(logLedPin, LOW);
 }
 // blink the LED to show meter pulses
 if( digitalRead(interruptPin)== LOW ) // Light the LED when data pin is LOW
 {
   digitalWrite(dataLedPin, HIGH);    
   delay(100);  // light for at least 100 ms
 }
 else
    digitalWrite(dataLedPin, LOW);  
}

void MeterPulseLog()
{
   unsigned long wattSensorCount; //number of watts during this logging interval
   uint8_t oldSREG = SREG;   // save interrupt register
   cli();                    // prevent interrupts while accessing the count  
   wattSensorCount = wattSensor; //get the count from the interrupt handler
   wattSensor = 0;           //reset the watts count
   SREG = oldSREG;           // restore interrupts

   totalWatts = totalWatts + wattSensorCount; //total watts counter
   LogFile.print(',');   // print comma to seprate from previous data
   LogFile.print(wattSensorCount);
   LogFile.print(',');
   LogFile.print(totalWatts);        
}

// routine handle file errors
void error(int err)
{
 // blink forever
while(1)
{
  digitalWrite(fileLedPin, HIGH);
  delay(err * 200);
  digitalWrite(fileLedPin, LOW);
  delay(200);  
}    
}

void printDateTime(time_t t)
{
 printDigits(hour(t),':' );
 printDigits(minute(t),':' );
 printDigits(second(t),',' );  // uncomment this to print seconds (and replace the comma above with colon)
 // print a space here if you need it
 printDigits(day(t),'//' );  
 printDigits(month(t),'//' );  
//  LogFile.print(year(t));  // prints year using 4 digits
 LogFile.print(year(t)-2000);  // prints year using 2 digits
 LogFile.print(',');
}

void printDigits(int digits, char seperator)
{
 // utility function time with leading 0
 if(digits < 10)
   LogFile.print('0');
 LogFile.print(digits);
 LogFile.print(seperator);  
}

void interruptHandler() // routine called when external interrupt is triggered
{
 wattSensor = wattSensor + 1;  //Update number of pulses, 1 pulse = 1 watt
}

Go Up