Pages: [1]   Go Down
Author Topic: Arduino Stalker Board Help  (Read 834 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :smiley.

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.  smiley
« Last Edit: December 19, 2010, 03:33:20 pm by andyh » Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 * 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
}
« Last Edit: December 19, 2010, 05:14:31 pm by andyh » Logged

Pages: [1]   Go Up
Jump to: