Commit 44649a2a authored by Patrick's avatar Patrick

first dump of example code

parent aaa26a5c
#include "Lewis.h"
Lewis Morse;
void setup() {
// receive on pin 2, transmit on pin 13, 10 WPM
Morse.begin(2, 13, 10);
}
void loop() {
Morse.print("cq");
delay(2000);
Morse.write('k');
delay(2000);
}
#include "red_peg.h"
red_peg RP;
void setup() {
Serial.begin(BAUD);
RP.begin();
}
void loop() {
t_SensorData depth = RP.get(MA4_20);
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);
}
#include <SPI.h>
#include <SdFat.h>
//#include <red_peg.h>
#define RP_SD_SS 5
#define BAUD 115200
SdFat SD;
File myFile;
//red_peg RP;
void setup() {
Serial.begin(BAUD);
Serial.println(F("starting rp_sd_logging"));
Serial.print(F("initialise SD card…"));
pinMode(10, OUTPUT);
if (!SD.begin(RP_SD_SS)) {
Serial.println("initialization failed!");
// do nothing forever
//while (1);
}
Serial.println("initialization done.");
//RP.begin();
}
void loop() {
//t_SensorData depth = RP.get(MA4_20);
// 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);
}
#include <red_peg.h>
red_peg RP;
#define SENSOR_ACTIVE_PIN 6
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(BAUD);
Serial.println(F("start rp_sensor_active_blink"));
delay(100);
RP.begin();
}
// the loop function runs over and over again forever
void loop() {
RP.sensorsOn();
delay(1000); // wait for a second
RP.sensorsOff();
delay(1000); // wait for a second
}
#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);
}
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(115200);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7,12,30,6));
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
/*
SD card read/write
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
created Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
*/
#define SD_CS_PIN 5
#include <SPI.h>
//#include <SD.h>
#include <SdFat.h>
#include <red_peg.h>
SdFat SD;
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
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);
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.");
delay(100);
// 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("opening test.txt for reading");
Serial.println("File contains:");
// 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()
{
// nothing happens after setup
}
#define SENSOR_ACTIVE_PIN 6
// 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);
}
// 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
}
#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();
Serial.println(F("To set the time send \"T YYYY MM DD hh mm s\""));
}
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('-');
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.println();
}
#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);
}
[bumpversion]
current_version = 1.0.2
commit = True
tag = True
[bumpversion:file:library.properties]
[bumpversion:file:src/MCP342x.h]
#include <Wire.h>
#include <MCP342x.h>
/* Demonstrate the use of convertAndRead().
*/
// 0x68 is the default address for all MCP342x devices
uint8_t address = 0x6E;
MCP342x adc = MCP342x(address);
void setup(void)
{
Serial.begin(9600);
Wire.begin();
// Enable power for MCP342x (needed for FL100 shield only)
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
// 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)
;
}
}
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::channel1, MCP342x::oneShot,
MCP342x::resolution16, MCP342x::gain1,
1000000, value, status);
if (err) {
Serial.print("Convert error: ");
Serial.println(err);
}
else {
Serial.print("Value: ");
Serial.println(value);
}
delay(1000);
}
#include <Wire.h>
#include <MCP342x.h>
/* Demonstrate the use of read() and convert(). If read() is called
* immediately after convert then the conversion will not have
* completed. Two approaches to avoid this problem are possible, use
* delay() or similar to wait a fixed amount of time, or to
* periodically read the device and check the config result.
*/
// 0x68 is the default address for all MCP342x devices
uint8_t address = 0x6E;
MCP342x adc = MCP342x(address);
// Configuration settings
MCP342x::Config config(MCP342x::channel1, MCP342x::oneShot,
MCP342x::resolution18, MCP342x::gain1);
// Configuration/status read back from the ADC
MCP342x::Config status;
// Inidicate if a new conversion should be started
bool startConversion = false;
// LED details
#ifdef LED_BUILTIN
int led = LED_BUILTIN;
#else
int led = 13;
#endif
bool ledLevel = false;
void setup(void)
{
Serial.begin(9600);
Wire.begin();
// Enable power for MCP342x (needed for FL100 shield only)
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(led, OUTPUT);
// Reset devices
MCP342x::generalCallReset();
delay(1); // MC342x needs 300us to settle
// 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)
;
}
// First time loop() is called start a conversion
startConversion = true;
}
unsigned long lastLedFlash = 0;
void loop(void)
{
long value = 0;
uint8_t err;
if (startConversion) {
Serial.println("Convert");
err = adc.convert(config);
if (err) {
Serial.print("Convert error: ");
Serial.println(err);
}
startConversion = false;
}
err = adc.read(value, status);
if (!err && status.isReady()) {
// For debugging purposes print the return value.
Serial.print("Value: ");
Serial.println(value);
Serial.print("Config: 0x");
Serial.println((int)config, HEX);
Serial.print("Convert error: ");
Serial.println(err);
startConversion = true;
}
// Do other stuff here, such as flash an LED
if (millis() - lastLedFlash > 50) {
ledLevel = !ledLevel;
digitalWrite(led, ledLevel);
lastLedFlash = millis();
}
}
#include <Wire.h>
#include <MCP342x.h>
/* Demonstrate the use of generalCallConversion(). This method is
* useful to simultaneously instruct several MCP342x devices to begin
* a one-shot analogue conversion, using the most recent
* configuration. The results must be read back individually from each
* device.
*/
// 0x68 is the default address for all MCP342x devices
uint8_t address = 0x6E;
MCP342x adc = MCP342x(address);
// Configuration settings
MCP342x::Config config(MCP342x::channel1, MCP342x::oneShot,
MCP342x::resolution18, MCP342x::gain1);
// Configuration/status read back from the ADC
MCP342x::Config status;
// Inidicate if a new conversion should be started
bool startConversion = false;
// LED details