Various fixes for SAMD (#510)

* Only serial GW should hold back on USB

* Fixing some examples on archs other than avr

* HwInit() is needed on samd, to init I2C
This commit is contained in:
Thomas Mørch
2016-07-16 10:01:28 +02:00
committed by Olivier
parent 54c79308e9
commit f0f2542df4
10 changed files with 352 additions and 361 deletions
+4 -4
View File
@@ -68,7 +68,7 @@ void hwReadConfigBlock(void* buf, void* adr, size_t length)
int offs = reinterpret_cast<int>(adr); int offs = reinterpret_cast<int>(adr);
while (length-- > 0) while (length-- > 0)
{ {
*dst++ = i2c_eeprom_read_byte(offs++); *dst++ = i2c_eeprom_read_byte(offs++);
} }
} }
@@ -100,7 +100,7 @@ void hwWriteConfig(int adr, uint8_t value)
void hwInit() { void hwInit() {
MY_SERIALDEVICE.begin(MY_BAUD_RATE); MY_SERIALDEVICE.begin(MY_BAUD_RATE);
#ifndef MY_GATEWAY_W5100 #if defined(MY_GATEWAY_SERIAL)
while (!MY_SERIALDEVICE) {} while (!MY_SERIALDEVICE) {}
#endif #endif
Wire.begin(); Wire.begin();
@@ -142,12 +142,12 @@ uint16_t hwCPUVoltage() {
// TODO: Not supported! // TODO: Not supported!
return 0; return 0;
} }
uint16_t hwCPUFrequency() { uint16_t hwCPUFrequency() {
// TODO: Not supported! // TODO: Not supported!
return 0; return 0;
} }
uint16_t hwFreeMem() { uint16_t hwFreeMem() {
// TODO: Not supported! // TODO: Not supported!
return 0; return 0;
+59 -59
View File
@@ -21,30 +21,30 @@
* DESCRIPTION * DESCRIPTION
* *
* Connect the MQ2 sensor as follows : * Connect the MQ2 sensor as follows :
* *
* A H A >>> 5V * A H A >>> 5V
* B >>> A0 * B >>> A0
* H >>> GND * H >>> GND
* B >>> 10K ohm >>> GND * B >>> 10K ohm >>> GND
* *
* Contribution: epierre * Contribution: epierre
* Based on http://sandboxelectronics.com/?p=165 * Based on http://sandboxelectronics.com/?p=165
* License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0) * License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
* Modified by HEK to work in 1.4 * Modified by HEK to work in 1.4
* *
*/ */
// Enable debug prints to serial monitor // Enable debug prints to serial monitor
#define MY_DEBUG #define MY_DEBUG
// Enable and select radio type attached // Enable and select radio type attached
#define MY_RADIO_NRF24 #define MY_RADIO_NRF24
//#define MY_RADIO_RFM69 //#define MY_RADIO_RFM69
#include <SPI.h> #include <SPI.h>
#include <MySensors.h> #include <MySensors.h>
#define CHILD_ID_MQ 0 #define CHILD_ID_MQ 0
/************************Hardware Related Macros************************************/ /************************Hardware Related Macros************************************/
#define MQ_SENSOR_ANALOG_PIN (0) //define which analog input channel you are going to use #define MQ_SENSOR_ANALOG_PIN (0) //define which analog input channel you are going to use
#define RL_VALUE (5) //define the load resistance on the board, in kilo ohms #define RL_VALUE (5) //define the load resistance on the board, in kilo ohms
@@ -55,7 +55,7 @@
#define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interal(in milisecond) between each samples in the #define CALIBRATION_SAMPLE_INTERVAL (500) //define the time interal(in milisecond) between each samples in the
//cablibration phase //cablibration phase
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation #define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interal(in milisecond) between each samples in #define READ_SAMPLE_TIMES (5) //define the time interal(in milisecond) between each samples in
//normal operation //normal operation
/**********************Application Related Macros**********************************/ /**********************Application Related Macros**********************************/
#define GAS_LPG (0) #define GAS_LPG (0)
@@ -68,25 +68,25 @@ float Ro = 10000.0; // this has to be tuned 10K Ohm
int val = 0; // variable to store the value coming from the sensor int val = 0; // variable to store the value coming from the sensor
float valMQ =0.0; float valMQ =0.0;
float lastMQ =0.0; float lastMQ =0.0;
float LPGCurve[3] = {2.3,0.21,-0.47}; //two points are taken from the curve. float LPGCurve[3] = {2.3,0.21,-0.47}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent" //with these two points, a line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59)
float COCurve[3] = {2.3,0.72,-0.34}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent"
//to the original curve. //to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000, 0.15) //data format:{ x, y, slope}; point1: (lg200, 0.21), point2: (lg10000, -0.59)
float SmokeCurve[3] ={2.3,0.53,-0.44}; //two points are taken from the curve. float COCurve[3] = {2.3,0.72,-0.34}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent" //with these two points, a line is formed which is "approximately equivalent"
//to the original curve. //to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.53), point2:(lg10000,-0.22) //data format:{ x, y, slope}; point1: (lg200, 0.72), point2: (lg10000, 0.15)
float SmokeCurve[3] ={2.3,0.53,-0.44}; //two points are taken from the curve.
//with these two points, a line is formed which is "approximately equivalent"
//to the original curve.
//data format:{ x, y, slope}; point1: (lg200, 0.53), point2:(lg10000,-0.22)
MyMessage msg(CHILD_ID_MQ, V_LEVEL); MyMessage msg(CHILD_ID_MQ, V_LEVEL);
void setup() void setup()
{ {
Ro = MQCalibration(MQ_SENSOR_ANALOG_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air Ro = MQCalibration(MQ_SENSOR_ANALOG_PIN); //Calibrating the sensor. Please make sure the sensor is in clean air
} }
void presentation() { void presentation() {
@@ -94,33 +94,33 @@ void presentation() {
sendSketchInfo("Air Quality Sensor", "1.0"); sendSketchInfo("Air Quality Sensor", "1.0");
// Register all sensors to gateway (they will be created as child devices) // Register all sensors to gateway (they will be created as child devices)
present(CHILD_ID_MQ, S_AIR_QUALITY); present(CHILD_ID_MQ, S_AIR_QUALITY);
} }
void loop() void loop()
{ {
uint16_t valMQ = MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO); uint16_t valMQ = MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO);
Serial.println(val); Serial.println(val);
Serial.print("LPG:"); Serial.print("LPG:");
Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) ); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_LPG) );
Serial.print( "ppm" ); Serial.print( "ppm" );
Serial.print(" "); Serial.print(" ");
Serial.print("CO:"); Serial.print("CO:");
Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) ); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_CO) );
Serial.print( "ppm" ); Serial.print( "ppm" );
Serial.print(" "); Serial.print(" ");
Serial.print("SMOKE:"); Serial.print("SMOKE:");
Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) ); Serial.print(MQGetGasPercentage(MQRead(MQ_SENSOR_ANALOG_PIN)/Ro,GAS_SMOKE) );
Serial.print( "ppm" ); Serial.print( "ppm" );
Serial.print("\n"); Serial.print("\n");
if (valMQ != lastMQ) { if (valMQ != lastMQ) {
send(msg.set((int)ceil(valMQ))); send(msg.set((int16_t)ceil(valMQ)));
lastMQ = ceil(valMQ); lastMQ = ceil(valMQ);
} }
sleep(SLEEP_TIME); //sleep for: sleepTime sleep(SLEEP_TIME); //sleep for: sleepTime
} }
/****************** MQResistanceCalculation **************************************** /****************** MQResistanceCalculation ****************************************
@@ -129,35 +129,35 @@ Output: the calculated sensor resistance
Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage Remarks: The sensor and the load resistor forms a voltage divider. Given the voltage
across the load resistor and its resistance, the resistance of the sensor across the load resistor and its resistance, the resistance of the sensor
could be derived. could be derived.
************************************************************************************/ ************************************************************************************/
float MQResistanceCalculation(int raw_adc) float MQResistanceCalculation(int raw_adc)
{ {
return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc)); return ( ((float)RL_VALUE*(1023-raw_adc)/raw_adc));
} }
/***************************** MQCalibration **************************************** /***************************** MQCalibration ****************************************
Input: mq_pin - analog channel Input: mq_pin - analog channel
Output: Ro of the sensor Output: Ro of the sensor
Remarks: This function assumes that the sensor is in clean air. It use Remarks: This function assumes that the sensor is in clean air. It use
MQResistanceCalculation to calculates the sensor resistance in clean air MQResistanceCalculation to calculates the sensor resistance in clean air
and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about and then divides it with RO_CLEAN_AIR_FACTOR. RO_CLEAN_AIR_FACTOR is about
10, which differs slightly between different sensors. 10, which differs slightly between different sensors.
************************************************************************************/ ************************************************************************************/
float MQCalibration(int mq_pin) float MQCalibration(int mq_pin)
{ {
int i; int i;
float val=0; float val=0;
for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples for (i=0;i<CALIBARAION_SAMPLE_TIMES;i++) { //take multiple samples
val += MQResistanceCalculation(analogRead(mq_pin)); val += MQResistanceCalculation(analogRead(mq_pin));
delay(CALIBRATION_SAMPLE_INTERVAL); delay(CALIBRATION_SAMPLE_INTERVAL);
} }
val = val/CALIBARAION_SAMPLE_TIMES; //calculate the average value val = val/CALIBARAION_SAMPLE_TIMES; //calculate the average value
val = val/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro val = val/RO_CLEAN_AIR_FACTOR; //divided by RO_CLEAN_AIR_FACTOR yields the Ro
//according to the chart in the datasheet //according to the chart in the datasheet
return val; return val;
} }
/***************************** MQRead ********************************************* /***************************** MQRead *********************************************
Input: mq_pin - analog channel Input: mq_pin - analog channel
@@ -166,29 +166,29 @@ Remarks: This function use MQResistanceCalculation to caculate the sensor resist
The Rs changes as the sensor is in the different consentration of the target The Rs changes as the sensor is in the different consentration of the target
gas. The sample times and the time interval between samples could be configured gas. The sample times and the time interval between samples could be configured
by changing the definition of the macros. by changing the definition of the macros.
************************************************************************************/ ************************************************************************************/
float MQRead(int mq_pin) float MQRead(int mq_pin)
{ {
int i; int i;
float rs=0; float rs=0;
for (i=0;i<READ_SAMPLE_TIMES;i++) { for (i=0;i<READ_SAMPLE_TIMES;i++) {
rs += MQResistanceCalculation(analogRead(mq_pin)); rs += MQResistanceCalculation(analogRead(mq_pin));
delay(READ_SAMPLE_INTERVAL); delay(READ_SAMPLE_INTERVAL);
} }
rs = rs/READ_SAMPLE_TIMES; rs = rs/READ_SAMPLE_TIMES;
return rs; return rs;
} }
/***************************** MQGetGasPercentage ********************************** /***************************** MQGetGasPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro Input: rs_ro_ratio - Rs divided by Ro
gas_id - target gas type gas_id - target gas type
Output: ppm of the target gas Output: ppm of the target gas
Remarks: This function passes different curves to the MQGetPercentage function which Remarks: This function passes different curves to the MQGetPercentage function which
calculates the ppm (parts per million) of the target gas. calculates the ppm (parts per million) of the target gas.
************************************************************************************/ ************************************************************************************/
int MQGetGasPercentage(float rs_ro_ratio, int gas_id) int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
{ {
if ( gas_id == GAS_LPG ) { if ( gas_id == GAS_LPG ) {
@@ -197,20 +197,20 @@ int MQGetGasPercentage(float rs_ro_ratio, int gas_id)
return MQGetPercentage(rs_ro_ratio,COCurve); return MQGetPercentage(rs_ro_ratio,COCurve);
} else if ( gas_id == GAS_SMOKE ) { } else if ( gas_id == GAS_SMOKE ) {
return MQGetPercentage(rs_ro_ratio,SmokeCurve); return MQGetPercentage(rs_ro_ratio,SmokeCurve);
} }
return 0; return 0;
} }
/***************************** MQGetPercentage ********************************** /***************************** MQGetPercentage **********************************
Input: rs_ro_ratio - Rs divided by Ro Input: rs_ro_ratio - Rs divided by Ro
pcurve - pointer to the curve of the target gas pcurve - pointer to the curve of the target gas
Output: ppm of the target gas Output: ppm of the target gas
Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm) Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
of the line could be derived if y(rs_ro_ratio) is provided. As it is a of the line could be derived if y(rs_ro_ratio) is provided. As it is a
logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
value. value.
************************************************************************************/ ************************************************************************************/
int MQGetPercentage(float rs_ro_ratio, float *pcurve) int MQGetPercentage(float rs_ro_ratio, float *pcurve)
{ {
return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0]))); return (pow(10,( ((log(rs_ro_ratio)-pcurve[1])/pcurve[2]) + pcurve[0])));
@@ -22,16 +22,16 @@
* *
* Interrupt driven binary switch example with dual interrupts * Interrupt driven binary switch example with dual interrupts
* Author: Patrick 'Anticimex' Fallberg * Author: Patrick 'Anticimex' Fallberg
* Connect one button or door/window reed switch between * Connect one button or door/window reed switch between
* digitial I/O pin 3 (BUTTON_PIN below) and GND and the other * digitial I/O pin 3 (BUTTON_PIN below) and GND and the other
* one in similar fashion on digital I/O pin 2. * one in similar fashion on digital I/O pin 2.
* This example is designed to fit Arduino Nano/Pro Mini * This example is designed to fit Arduino Nano/Pro Mini
* *
*/ */
// Enable debug prints to serial monitor // Enable debug prints to serial monitor
#define MY_DEBUG #define MY_DEBUG
// Enable and select radio type attached // Enable and select radio type attached
#define MY_RADIO_NRF24 #define MY_RADIO_NRF24
@@ -62,14 +62,14 @@
#if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID) #if (PRIMARY_CHILD_ID == SECONDARY_CHILD_ID)
#error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same #error PRIMARY_CHILD_ID and SECONDARY_CHILD_ID cannot be the same
#endif #endif
// Change to V_LIGHT if you use S_LIGHT in presentation below // Change to V_LIGHT if you use S_LIGHT in presentation below
MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED); MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED); MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
void setup() void setup()
{ {
// Setup the buttons // Setup the buttons
pinMode(PRIMARY_BUTTON_PIN, INPUT); pinMode(PRIMARY_BUTTON_PIN, INPUT);
pinMode(SECONDARY_BUTTON_PIN, INPUT); pinMode(SECONDARY_BUTTON_PIN, INPUT);
@@ -84,14 +84,14 @@ void presentation() {
sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER); sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
// Register binary input sensor to sensor_node (they will be created as child devices) // Register binary input sensor to sensor_node (they will be created as child devices)
// You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
// If S_LIGHT is used, remember to update variable type you send in. See "msg" above. // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
present(PRIMARY_CHILD_ID, S_DOOR); present(PRIMARY_CHILD_ID, S_DOOR);
present(SECONDARY_CHILD_ID, S_DOOR); present(SECONDARY_CHILD_ID, S_DOOR);
} }
// Loop will iterate on changes on the BUTTON_PINs // Loop will iterate on changes on the BUTTON_PINs
void loop() void loop()
{ {
uint8_t value; uint8_t value;
static uint8_t sentValue=2; static uint8_t sentValue=2;
@@ -99,23 +99,23 @@ void loop()
// Short delay to allow buttons to properly settle // Short delay to allow buttons to properly settle
sleep(5); sleep(5);
value = digitalRead(PRIMARY_BUTTON_PIN); value = digitalRead(PRIMARY_BUTTON_PIN);
if (value != sentValue) { if (value != sentValue) {
// Value has changed from last transmission, send the updated value // Value has changed from last transmission, send the updated value
send(msg.set(value==HIGH ? 1 : 0)); send(msg.set(value==HIGH));
sentValue = value; sentValue = value;
} }
value = digitalRead(SECONDARY_BUTTON_PIN); value = digitalRead(SECONDARY_BUTTON_PIN);
if (value != sentValue2) { if (value != sentValue2) {
// Value has changed from last transmission, send the updated value // Value has changed from last transmission, send the updated value
send(msg2.set(value==HIGH ? 1 : 0)); send(msg2.set(value==HIGH));
sentValue2 = value; sentValue2 = value;
} }
// Sleep until something happens with the sensor // Sleep until something happens with the sensor
sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0); sleep(PRIMARY_BUTTON_PIN-2, CHANGE, SECONDARY_BUTTON_PIN-2, CHANGE, 0);
} }
@@ -20,30 +20,30 @@
* *
* REVISION HISTORY * REVISION HISTORY
* Version 1.0 - February 15, 2014 - Bruce Lacey * Version 1.0 - February 15, 2014 - Bruce Lacey
* Version 1.1 - August 13, 2014 - Converted to 1.4 (hek) * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
* *
* DESCRIPTION * DESCRIPTION
* This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
* <henrik.ekblad@gmail.com> Vera Arduino Sensor project. * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
* Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches. * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
* *
* The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip. * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
* The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
* to the LED negative terminal and the MOSFET Source pin is connected to ground. * to the LED negative terminal and the MOSFET Source pin is connected to ground.
* *
* This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit. * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
* http://www.mysensors.org/build/dimmer * http://www.mysensors.org/build/dimmer
*/ */
// Enable debug prints to serial monitor // Enable debug prints to serial monitor
#define MY_DEBUG #define MY_DEBUG
// Enable and select radio type attached // Enable and select radio type attached
#define MY_RADIO_NRF24 #define MY_RADIO_NRF24
//#define MY_RADIO_RFM69 //#define MY_RADIO_RFM69
#include <SPI.h> #include <SPI.h>
#include <MySensors.h> #include <MySensors.h>
#define SN "DimmableLED" #define SN "DimmableLED"
#define SV "1.1" #define SV "1.1"
@@ -51,7 +51,7 @@
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin #define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim) #define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
static int currentLevel = 0; // Current dim level... static int16_t currentLevel = 0; // Current dim level...
MyMessage dimmerMsg(0, V_DIMMER); MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT); MyMessage lightMsg(0, V_LIGHT);
@@ -59,8 +59,8 @@ MyMessage lightMsg(0, V_LIGHT);
/*** /***
* Dimmable LED initialization method * Dimmable LED initialization method
*/ */
void setup() void setup()
{ {
// Pull the gateway's current dim level - restore light level upon sendor node power-up // Pull the gateway's current dim level - restore light level upon sendor node power-up
request( 0, V_DIMMER ); request( 0, V_DIMMER );
} }
@@ -68,14 +68,14 @@ void setup()
void presentation() { void presentation() {
// Register the LED Dimmable Light with the gateway // Register the LED Dimmable Light with the gateway
present( 0, S_DIMMER ); present( 0, S_DIMMER );
sendSketchInfo(SN, SV); sendSketchInfo(SN, SV);
} }
/*** /***
* Dimmable LED main processing loop * Dimmable LED main processing loop
*/ */
void loop() void loop()
{ {
} }
@@ -83,31 +83,31 @@ void loop()
void receive(const MyMessage &message) { void receive(const MyMessage &message) {
if (message.type == V_LIGHT || message.type == V_DIMMER) { if (message.type == V_LIGHT || message.type == V_DIMMER) {
// Retrieve the power or dim level from the incoming request message // Retrieve the power or dim level from the incoming request message
int requestedLevel = atoi( message.data ); int requestedLevel = atoi( message.data );
// Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on] // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 ); requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
// Clip incoming level to valid range of 0 to 100 // Clip incoming level to valid range of 0 to 100
requestedLevel = requestedLevel > 100 ? 100 : requestedLevel; requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
requestedLevel = requestedLevel < 0 ? 0 : requestedLevel; requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
Serial.print( "Changing level to " ); Serial.print( "Changing level to " );
Serial.print( requestedLevel ); Serial.print( requestedLevel );
Serial.print( ", from " ); Serial.print( ", from " );
Serial.println( currentLevel ); Serial.println( currentLevel );
fadeToLevel( requestedLevel ); fadeToLevel( requestedLevel );
// Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value... // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
send(lightMsg.set(currentLevel > 0 ? 1 : 0)); send(lightMsg.set(currentLevel > 0));
// hek comment: Is this really nessesary? // hek comment: Is this really nessesary?
send( dimmerMsg.set(currentLevel) ); send( dimmerMsg.set(currentLevel) );
} }
} }
@@ -117,11 +117,10 @@ void receive(const MyMessage &message) {
void fadeToLevel( int toLevel ) { void fadeToLevel( int toLevel ) {
int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1; int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
while ( currentLevel != toLevel ) { while ( currentLevel != toLevel ) {
currentLevel += delta; currentLevel += delta;
analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) ); analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
delay( FADE_DELAY ); delay( FADE_DELAY );
} }
} }
+19 -20
View File
@@ -20,13 +20,13 @@
* *
* REVISION HISTORY * REVISION HISTORY
* Version 1.0 - January 30, 2015 - Developed by GizMoCuz (Domoticz) * Version 1.0 - January 30, 2015 - Developed by GizMoCuz (Domoticz)
* *
* DESCRIPTION * DESCRIPTION
* This sketch provides an example how to implement a Dimmable Light * This sketch provides an example how to implement a Dimmable Light
* It is pure virtual and it logs messages to the serial output * It is pure virtual and it logs messages to the serial output
* It can be used as a base sketch for actual hardware. * It can be used as a base sketch for actual hardware.
* Stores the last light state and level in eeprom. * Stores the last light state and level in eeprom.
* *
*/ */
// Enable debug prints // Enable debug prints
@@ -37,7 +37,7 @@
//#define MY_RADIO_RFM69 //#define MY_RADIO_RFM69
#include <SPI.h> #include <SPI.h>
#include <MySensors.h> #include <MySensors.h>
#define CHILD_ID_LIGHT 1 #define CHILD_ID_LIGHT 1
@@ -50,19 +50,19 @@
#define SN "Dimable Light" #define SN "Dimable Light"
#define SV "1.0" #define SV "1.0"
int LastLightState=LIGHT_OFF; int16_t LastLightState=LIGHT_OFF;
int LastDimValue=100; int16_t LastDimValue=100;
MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT); MyMessage lightMsg(CHILD_ID_LIGHT, V_LIGHT);
MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER); MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
void setup() void setup()
{ {
//Retreive our last light state from the eprom //Retreive our last light state from the eprom
int LightState=loadState(EPROM_LIGHT_STATE); int LightState=loadState(EPROM_LIGHT_STATE);
if (LightState<=1) { if (LightState<=1) {
LastLightState=LightState; LastLightState=LightState;
int DimValue=loadState(EPROM_DIMMER_LEVEL); int DimValue=loadState(EPROM_DIMMER_LEVEL);
if ((DimValue>0)&&(DimValue<=100)) { if ((DimValue>0)&&(DimValue<=100)) {
//There should be no Dim value of 0, this would mean LIGHT_OFF //There should be no Dim value of 0, this would mean LIGHT_OFF
LastDimValue=DimValue; LastDimValue=DimValue;
@@ -71,8 +71,8 @@ void setup()
//Here you actualy switch on/off the light with the last known dim level //Here you actualy switch on/off the light with the last known dim level
SetCurrentState2Hardware(); SetCurrentState2Hardware();
Serial.println( "Node ready to receive messages..." ); Serial.println( "Node ready to receive messages..." );
} }
void presentation() { void presentation() {
@@ -82,7 +82,7 @@ void presentation() {
present(CHILD_ID_LIGHT, S_DIMMER ); present(CHILD_ID_LIGHT, S_DIMMER );
} }
void loop() void loop()
{ {
} }
@@ -90,7 +90,7 @@ void receive(const MyMessage &message)
{ {
if (message.type == V_LIGHT) { if (message.type == V_LIGHT) {
Serial.println( "V_LIGHT command received..." ); Serial.println( "V_LIGHT command received..." );
int lstate= atoi( message.data ); int lstate= atoi( message.data );
if ((lstate<0)||(lstate>1)) { if ((lstate<0)||(lstate>1)) {
Serial.println( "V_LIGHT data invalid (should be 0/1)" ); Serial.println( "V_LIGHT data invalid (should be 0/1)" );
@@ -98,7 +98,7 @@ void receive(const MyMessage &message)
} }
LastLightState=lstate; LastLightState=lstate;
saveState(EPROM_LIGHT_STATE, LastLightState); saveState(EPROM_LIGHT_STATE, LastLightState);
if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) { if ((LastLightState==LIGHT_ON)&&(LastDimValue==0)) {
//In the case that the Light State = On, but the dimmer value is zero, //In the case that the Light State = On, but the dimmer value is zero,
//then something (probably the controller) did something wrong, //then something (probably the controller) did something wrong,
@@ -106,13 +106,13 @@ void receive(const MyMessage &message)
LastDimValue=100; LastDimValue=100;
saveState(EPROM_DIMMER_LEVEL, LastDimValue); saveState(EPROM_DIMMER_LEVEL, LastDimValue);
} }
//When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value //When receiving a V_LIGHT command we switch the light between OFF and the last received dimmer value
//This means if you previously set the lights dimmer value to 50%, and turn the light ON //This means if you previously set the lights dimmer value to 50%, and turn the light ON
//it will do so at 50% //it will do so at 50%
} }
else if (message.type == V_DIMMER) { else if (message.type == V_DIMMER) {
Serial.println( "V_DIMMER command received..." ); Serial.println( "V_DIMMER command received..." );
int dimvalue= atoi( message.data ); int dimvalue= atoi( message.data );
if ((dimvalue<0)||(dimvalue>100)) { if ((dimvalue<0)||(dimvalue>100)) {
Serial.println( "V_DIMMER data invalid (should be 0..100)" ); Serial.println( "V_DIMMER data invalid (should be 0..100)" );
@@ -128,7 +128,7 @@ void receive(const MyMessage &message)
} }
} }
else { else {
Serial.println( "Invalid command received..." ); Serial.println( "Invalid command received..." );
return; return;
} }
@@ -153,10 +153,9 @@ void SetCurrentState2Hardware()
void SendCurrentState2Controller() void SendCurrentState2Controller()
{ {
if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) { if ((LastLightState==LIGHT_OFF)||(LastDimValue==0)) {
send(dimmerMsg.set(0)); send(dimmerMsg.set((int16_t)0));
} }
else { else {
send(dimmerMsg.set(LastDimValue)); send(dimmerMsg.set(LastDimValue));
} }
} }
+1 -1
View File
@@ -94,7 +94,7 @@ void loop() {
Serial.println(dustDensity); // unit: ug/m3 Serial.println(dustDensity); // unit: ug/m3
if (ceil(dustDensity) != lastDUST) { if (ceil(dustDensity) != lastDUST) {
send(dustMsg.set((int)ceil(dustDensity))); send(dustMsg.set((int16_t)ceil(dustDensity)));
lastDUST = ceil(dustDensity); lastDUST = ceil(dustDensity);
} }
+1 -1
View File
@@ -55,7 +55,7 @@ void presentation() {
void loop() void loop()
{ {
int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; int16_t lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
Serial.println(lightLevel); Serial.println(lightLevel);
if (lightLevel != lastLightLevel) { if (lightLevel != lastLightLevel) {
send(msg.set(lightLevel)); send(msg.set(lightLevel));
File diff suppressed because it is too large Load Diff
@@ -879,7 +879,7 @@ void setup()
(void)key; (void)key;
Serial.begin(115200); Serial.begin(115200);
hwInit();
Serial.println(F("Personalization sketch for MySensors usage.")); Serial.println(F("Personalization sketch for MySensors usage."));
Serial.println(F("-------------------------------------------")); Serial.println(F("-------------------------------------------"));
+10 -10
View File
@@ -33,7 +33,7 @@
**/ **/
// Enable debug prints to serial monitor // Enable debug prints to serial monitor
#define MY_DEBUG #define MY_DEBUG
// Enable and select radio type attached // Enable and select radio type attached
#define MY_RADIO_NRF24 #define MY_RADIO_NRF24
@@ -57,7 +57,7 @@ unsigned char state = 0;
MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL); MyMessage vibrationMsg(CHILD_ID_VIBRATION, V_LEVEL);
void setup() void setup()
{ {
pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT); pinMode(VIBRATION_SENSOR_DIGITAL_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(VIBRATION_SENSOR_DIGITAL_PIN), blink, FALLING); // Trigger the blink function when the falling edge is detected attachInterrupt(digitalPinToInterrupt(VIBRATION_SENSOR_DIGITAL_PIN), blink, FALLING); // Trigger the blink function when the falling edge is detected
@@ -72,19 +72,19 @@ void presentation() {
present(CHILD_ID_VIBRATION, S_VIBRATION); present(CHILD_ID_VIBRATION, S_VIBRATION);
} }
void loop() void loop()
{ {
if(state>=40){ // basically below 40 so ignire basic level if(state>=40){ // basically below 40 so ignire basic level
send(vibrationMsg.set(int(state))); send(vibrationMsg.set(int16_t(state)));
state = 0; state = 0;
digitalWrite(SensorLED,HIGH); digitalWrite(SensorLED,HIGH);
} else { } else {
state = 0; state = 0;
digitalWrite(SensorLED,LOW); digitalWrite(SensorLED,LOW);
} }
// Power down the radio. Note that the radio will get powered back up // Power down the radio. Note that the radio will get powered back up
// on the next write() call. // on the next write() call.
delay(1000); //delay to allow serial to fully print before sleep delay(1000); //delay to allow serial to fully print before sleep