Skip to content

Instantly share code, notes, and snippets.

@pbosetti
Created August 6, 2009 08:31
Show Gist options
  • Save pbosetti/163196 to your computer and use it in GitHub Desktop.
Save pbosetti/163196 to your computer and use it in GitHub Desktop.
#include <MsTimer2.h>
#include <EEPROM.h>
// Serial data rate
#define BAUD 115200
// Version number
#define CODEID "Sampler.1"
// Constants
unsigned int const STATUS_LED = 13; // Pin of status LED
unsigned int const RESET_LED = 2; // When HIGH resets the parameters stored in EEPROM
unsigned int const PERIOD = 10; // Period of acquisitions in ms
unsigned short int const PERIOD_ADR = 0; // Period EEPROM address
unsigned int const POINTS = 500; // Number of points to collect
unsigned short int const POINTS_ADR = 2; // Number of points EEPROM address
unsigned int const LOOP_DELAY = 100; // Main loop delay
#define LOG
// Globals
bool running = false;
unsigned long start = 0;
unsigned int period = 0;
unsigned int points = 0;
// Functions
template <class T> int EEPROM_write(int ee, const T& value)
{
byte const *p = reinterpret_cast<byte const *>(&value);
int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
return i;
}
template <class T> int EEPROM_read(int ee, T& value)
{
byte *p = reinterpret_cast<byte *>(&value);
int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
void pulse() {
static int i = 0;
Serial.print(i);
Serial.print(" ");
Serial.print(millis());
#ifdef LOG
int p;
for (p = 0; p < 4; p++)
{
Serial.print(" ");
Serial.print(analogRead(p));
}
#endif
Serial.println();
if (i++ == points)
{
Serial.print("Duration: ");
Serial.print(millis()-start);
Serial.println("---END");
i = 0;
MsTimer2::stop();
running = false;
digitalWrite(STATUS_LED, LOW);
}
}
void toggle() {
if (running)
{
MsTimer2::stop();
running = false;
digitalWrite(STATUS_LED, LOW);
}
else
{
MsTimer2::start();
start = millis();
running = true;
digitalWrite(STATUS_LED, HIGH);
}
}
void status()
{
Serial.print("Period: ");
Serial.println(period);
Serial.print("Points: ");
Serial.println(points);
}
// SETUP AND LOOP
void setup() {
pinMode(STATUS_LED, OUTPUT);
Serial.begin(BAUD);
Serial.print("Code ID is: ");
Serial.println(CODEID);
if (digitalRead(RESET_LED))
{
Serial.println("Resetting defaults...");
EEPROM_write(PERIOD_ADR, PERIOD);
EEPROM_write(POINTS_ADR, POINTS);
}
Serial.println("Loading parameters...");
EEPROM_read(PERIOD_ADR, period);
EEPROM_read(POINTS_ADR, points);
MsTimer2::set(period, pulse);
Serial.println("Vodafone FreqMeter is ready");
}
void loop() {
static unsigned int v = 0;
char ch;
if (Serial.available()) {
ch = Serial.read();
// Serial command parsing:
switch(ch) {
case '/':
toggle();
break;
case '?':
status();
break;
case '0'...'9': // Accumulates values
v = v * 10 + ch - '0';
break;
case 'p':
points = v;
v = 0;
EEPROM_write(POINTS_ADR, points);
break;
case 't':
period = v;
v = 0;
EEPROM_write(PERIOD_ADR, period);
break;
}
}
delay(LOOP_DELAY);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment