forked from Sirus10/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tank_water_level_low_power.ino
148 lines (120 loc) · 3.6 KB
/
tank_water_level_low_power.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************************
# This script aim at reporting tack water level
# Site : http://domotique.web2diz.net/
# Detail : http://domotique.web2diz.net/?p=596
#
# License : CC BY-SA 4.0
#
# This script use the x10rf library
# See source here :
# https://github.com/p2baron/x10rf
#
#
/*******************************************/
// including x10rf library
#include <x10rf.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
// define I/O PINS
#define SENSOR_LEVEL_0_PIN 0 // SENSOR LEVEL 0
#define SENSOR_LEVEL_1_PIN 1 // SENSOR LEVEL 1
#define SENSOR_LEVEL_2_PIN 2 // SENSOR LEVEL 2
#define SENSOR_LEVEL_3_PIN 3 // SENSOR LEVEL 3
#define RF_OUT 4 // OUTPUT RF
x10rf myx10 = x10rf(RF_OUT,0,5);
int level = 0;
volatile boolean f_wdt = 1;
// The setup
void setup() {
ADCSRA &= ~(1<<ADEN);// disable ADC (before power-off)
// SETING UP THE I/O PINS
pinMode(SENSOR_LEVEL_0_PIN, INPUT);
pinMode(SENSOR_LEVEL_1_PIN, INPUT);
pinMode(SENSOR_LEVEL_2_PIN, INPUT);
pinMode(SENSOR_LEVEL_3_PIN, INPUT);
pinMode(RF_OUT, OUTPUT);
// MYX10 INITIALIZATION
myx10.begin();
setup_watchdog(9); // approximately 8 seconds sleep
}
// The loop
void loop() {
get_and_sent_water_level();
delay(100);
// Sleep for 8 s 8 x 75 = 600 s = 10m
for (int i=0; i<75; i++){
system_sleep();
}
delay(100);
}
/*
FUNCTION
get_and_sent_water_level
*/
int get_and_sent_water_level(){
// INTERNAL PULL-UP ENABLING TO BE ABLE TO READ
digitalWrite(SENSOR_LEVEL_0_PIN, HIGH);
digitalWrite(SENSOR_LEVEL_1_PIN, HIGH);
digitalWrite(SENSOR_LEVEL_2_PIN, HIGH);
digitalWrite(SENSOR_LEVEL_3_PIN, HIGH);
delay(100);
/*
Serial.print(" LEVELLEVELLEVEL : ");
Serial.println(digitalRead(SENSOR_LEVEL_0_PIN));
Serial.print(" LEVELLEVELLEVEL : ");
Serial.println(digitalRead(SENSOR_LEVEL_1_PIN));
Serial.print(" LEVELLEVEL : ");
Serial.println(digitalRead(SENSOR_LEVEL_2_PIN));
Serial.print(" LEVEL : ");
Serial.println(digitalRead(SENSOR_LEVEL_3_PIN));
*/
int level = (1-digitalRead(SENSOR_LEVEL_0_PIN)) + (1-digitalRead(SENSOR_LEVEL_1_PIN)) + (1-digitalRead(SENSOR_LEVEL_2_PIN)) + (1-digitalRead(SENSOR_LEVEL_3_PIN)) ;
/*
Serial.print(" LEVEL OF WATER : ");
Serial.println(level);
*/
// INTERNAL PULL-UP DESABLING TO AVOID ELECTROLYSE
digitalWrite(SENSOR_LEVEL_0_PIN, LOW);
digitalWrite(SENSOR_LEVEL_1_PIN, LOW);
digitalWrite(SENSOR_LEVEL_2_PIN, LOW);
digitalWrite(SENSOR_LEVEL_3_PIN, LOW);
// SENDING THE LEVEL OVER RF
myx10.RFXmeter(12,0,level);
}
// set system into the sleep state
// system wakes up when wtchdog is timed out
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
// sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
f_wdt=1; // set global flag
}