Commit 119aa124 authored by Patrick's avatar Patrick

revised examples in red_peg lib

parent 6f118a74
......@@ -48,7 +48,7 @@ void loop(void)
Serial.println(err);
}
else {
float voltage = value * (2.0 / 32575.0);
float voltage = value * (2.045 / 32575.0);
float temp = (voltage-0.5) * 100;
Serial.print("Value: ");
......
#include <Wire.h>
#include <MCP342x.h>
/*
Read a TMP36 sensor from MCP3428, channel 3
*/
#define BAUD 115200
// 0x68 is the default address for all MCP342x devices
// on Red-Peg, the MCP3428 address is set to 0x6F
uint8_t address = 0x6F;
MCP342x adc = MCP342x(address);
void setup(void)
{
Serial.begin(BAUD);
Serial.println("starting temp_mon");
Wire.begin();
// Reset devices
MCP342x::generalCallReset();
delay(1); // MC342x needs 300us to settle, wait 1ms
// Check device present
Wire.requestFrom(address, (uint8_t)1);
if (!Wire.available()) {
Serial.print("No device found at address ");
Serial.println(address, HEX);
while (1)
;
} else {
Serial.println("MCP3248 found");
}
}
void loop(void)
{
long value = 0;
MCP342x::Config status;
// Initiate a conversion; convertAndRead() will wait until it can be read
uint8_t err = adc.convertAndRead(MCP342x::channel3, MCP342x::oneShot,
MCP342x::resolution16, MCP342x::gain1,
1000000, value, status);
if (err) {
Serial.print("Convert error: ");
Serial.println(err);
}
else {
float voltage = value * (2.0 / 32575.0);
float temp = (voltage-0.5) * 100;
Serial.print("Value: ");
Serial.print(value);
Serial.print(", ");
Serial.print(voltage);
Serial.print("V, ");
Serial.print(temp);
Serial.print(" degC");
Serial.println();
}
delay(1000);
}
......@@ -5,6 +5,7 @@ red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
RP.sensorsOn();
}
void loop() {
......
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
}
void loop() {
RP.sensorsOn();
t_SensorData depth = RP.get(MA4_20);
RP.sensorsOff();
if (depth.sensor == MA4_20) {
Serial.print("Value: ");
Serial.print(depth.reading);
Serial.print(", ");
Serial.print(RP.mA(depth));
Serial.print(" mA, ");
Serial.print(RP.level(depth, 7000));
Serial.print(" mm");
Serial.println();
} else {
Serial.println("no return from ADC");
}
delay(1000);
}
......@@ -6,6 +6,8 @@ void setup () {
Serial.begin(115200);
Serial.println(F("start rp_rtc_ds1307"));
RP.begin();
RP.sensorsOn();
delay(100);
}
void loop () {
......
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <red_peg.h>
red_peg RP;
void setup () {
Serial.begin(115200);
Serial.println(F("start rp_rtc_ds1307"));
RP.begin();
}
void loop () {
RP.sensorsOn();
delay(100);
t_SensorData the_time = RP.get(RTC);
RP.sensorsOff();
if (the_time.sensor == RTC) {
// print the current time in ISO 8601 format
Serial.print(the_time.y);
Serial.print("-");
Serial.print(the_time.m);
Serial.print("-");
Serial.print(the_time.d);
Serial.print("T");
Serial.print(the_time.hh);
Serial.print(":");
Serial.print(the_time.mm);
Serial.print(":");
Serial.print(the_time.ss); // DS1307 has whole secconds only
// assume UTC timestamp
Serial.print("Z");
Serial.println();
} else {
Serial.println(F("no RTC message received. \nLast message is:"));
RP.print_data(the_time);
}
delay(5000);
}
/*
SD card read/write with Red-Peg
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
modified from sketch by
David A. Mellis & Tom Igoe
*/
#define SD_CS_PIN 5 //5
#include <SPI.h>
//#include <SD.h>
#include <SdFat.h>
SdFat SD;
#include <red_peg.h>
red_peg RP;
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(BAUD); // 115200
RP.begin();
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);
pinMode(SD_CS_PIN, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
RP.sensorsOn();
delay(100);
t_SensorData returned = RP.get(TMP);
RP.sensorsOff();
if (returned.sensor == TMP) {
myFile = SD.open("readings.csv", FILE_WRITE);
if (myFile) {
Serial.print("Writing to readings.csv: ");
myFile.print(returned.y);
myFile.print("-");
myFile.print(returned.m);
myFile.print("-");
myFile.print(returned.d);
myFile.print("-");
myFile.print("T");
myFile.print(returned.hh);
myFile.print(":");
myFile.print(returned.mm);
myFile.print(":");
myFile.print(returned.ss);
myFile.print("Z, ");
myFile.print(returned.value);
myFile.println();
// close the file:
myFile.close();
// Then print what we sent to the Serial port
RP.print_data(returned);
Serial.println("done.");
delay(2000);
}
}
}
#define SENSOR_ACTIVE_PIN 6
#include <red_peg.h>
red_peg RP;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
Serial.println(F("start sensor_active_blink"));
delay(100);
pinMode(SENSOR_ACTIVE_PIN, OUTPUT);
Serial.begin(BAUD);
Serial.println(F("start rp_sensor_active"));
RP.begin();
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(SENSOR_ACTIVE_PIN, HIGH);
RP.sensorsOn();
delay(1000); // wait for a second
digitalWrite(SENSOR_ACTIVE_PIN, LOW);
RP.sensorsOff();
delay(1000); // wait for a second
}
......@@ -5,6 +5,8 @@ red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
RP.sensorsOn();
delay(100);
}
void loop() {
......
#include <red_peg.h>
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
}
void loop() {
t_SensorData temp_sensor = RP.get(TMP);
if (temp_sensor.the_sensor == TMP) {
Serial.print("Value: ");
Serial.print(temp_sensor.reading);
Serial.print(", ");
Serial.print(RP.volts(temp_sensor));
Serial.print("V, ");
Serial.print(RP.degC(temp_sensor));
Serial.print(" degC");
Serial.println();
} else {
Serial.println("no return from TMP");
}
delay(5000);
}
......@@ -3,24 +3,25 @@
This example shows how to read and write data to and from an SD card file
The circuit:
* SD card attached to SPI bus as follows:
* the SD card is attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
** CS - pin 5
modified from sketch by
David A. Mellis & Tom Igoe
*/
#define SD_CS_PIN 5 //5
#include <SPI.h>
//#include <SD.h>
#include <SdFat.h>
SdFat SD;
File myFile;
#include <red_peg.h>
red_peg RP;
File myFile;
#define SD_CS_PIN 5
void setup()
{
......@@ -49,6 +50,7 @@ void setup()
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
// write a line of data to the sdcard
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
......@@ -60,14 +62,14 @@ void setup()
// re-open the file for reading:
myFile = SD.open("test.txt");
// check that worked fine
if (myFile) {
Serial.println("test.txt:");
Serial.println("test.txt contains:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
// then close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
......@@ -77,34 +79,7 @@ void setup()
void loop()
{
t_SensorData returned = RP.get(TMP);
if (returned.sensor == TMP) {
myFile = SD.open("readings.csv", FILE_WRITE);
if (myFile) {
Serial.print("Writing to vals.csv: ");
myFile.print(returned.y);
myFile.print("-");
myFile.print(returned.m);
myFile.print("-");
myFile.print(returned.d);
myFile.print("-");
myFile.print("T");
myFile.print(returned.hh);
myFile.print(":");
myFile.print(returned.mm);
myFile.print(":");
myFile.print(returned.ss);
myFile.print("Z, ");
myFile.print(returned.value);
myFile.println();
// close the file:
myFile.close();
// Then print what we sent to the Serial port
RP.print_data(returned);
Serial.println("done.");
delay(2000);
}
}
// there's nothing here
}
#define SENSOR_ACTIVE_PIN 6
#define BAUD 115200
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(BAUD);
Serial.println(F("start sensor_blink"));
delay(100);
pinMode(SENSOR_ACTIVE_PIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(SENSOR_ACTIVE_PIN, HIGH);
delay(1000); // wait for a second
digitalWrite(SENSOR_ACTIVE_PIN, LOW);
delay(1000); // wait for a second
}
......@@ -20,7 +20,7 @@ void setup()
}
reportTimeNow();
Serial.println(F("To set the time send \"T YYYY MM DD hh mm s\""));
Serial.println(F("To set the time send \"T YYYY-MM-DD hh:mm:ss\""));
}
void loop()
......@@ -28,6 +28,9 @@ void loop()
if (Serial.available()) {
// if we have serial input, check input for setting the time
setTime();
} else if (millis() - last_reading >= READING_DELAY) {
// otherwise read out the time at regular intervals
reportTimeNow();
}
}
......@@ -54,9 +57,14 @@ void setTime() {
Serial.print(F("Second: "));
sec = Serial.parseInt();
Serial.println(sec);
while (Serial.available()) {
// eat any remaining characters
Serial.read();
}
rtc.adjust(DateTime(year, month, day, hour, min, sec));
Serial.print(F("RTC set to: "));
reportTimeNow();
last_reading = millis();
}
}
......
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
#define SENSOR_ACTIVE_PIN 6
#define READING_DELAY 10000
uint32_t last_reading = READING_DELAY;
void setup()
{
Serial.begin(115200);
Serial.println(F("starting set_the_time"));
// check we have an RTC attached
if (!rtc.begin()) {
Serial.println(F("no RTC"));
die();
}
reportTimeNow();
Serial.println(F("To set the time send \"T YYYY MM DD hh mm ss\""));
}
void loop()
{
if (Serial.available()) {
// if we have serial input, check input for setting the time
setTime();
}
}
void setTime() {
char c = Serial.read();
if (c == 't' || c == 'T') {
uint16_t year;
uint8_t month, day, hour, min, sec;
Serial.print(F("Year: "));
year = Serial.parseInt();
Serial.println(year);
Serial.print(F("Month: "));
month = Serial.parseInt();
Serial.println(month);
Serial.print(F("Day: "));
day = Serial.parseInt();
Serial.println(day);
Serial.print(F("Hour: "));
hour = Serial.parseInt();
Serial.println(hour);
Serial.print(F("Minute: "));
min = Serial.parseInt();
Serial.println(min);
Serial.print(F("Second: "));
sec = Serial.parseInt();
Serial.println(sec);
rtc.adjust(DateTime(year, month, day, hour, min, sec));
Serial.print(F("RTC set to: "));
reportTimeNow();
}
}
void die() {
pinMode(LED_BUILTIN, OUTPUT);
while(1) {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
}
void reportTimeNow() {
DateTime now = rtc.now();
char buf[5];
sprintf(buf, "%04d", now.year());
Serial.print(buf);
Serial.write('-');
sprintf(buf, "%02d", now.month());
Serial.print(buf);
Serial.write('-');
sprintf(buf, "%02d", now.day());
Serial.print(buf);
Serial.write('T');
sprintf(buf, "%02d", now.hour());
Serial.print(buf);
Serial.write(':');
sprintf(buf, "%02d", now.minute());
Serial.print(buf);
Serial.write(':');
sprintf(buf, "%02d", now.second());
Serial.print(buf);
Serial.write('Z');
Serial.println();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment