-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_sender.ino
179 lines (157 loc) · 3.92 KB
/
generic_sender.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Generic Sender code : Send a value (counter) over RF 433.92 mhz
* Fréquence : 433.92 mhz
* Protocole : homepi
* Licence : CC -by -sa
* Auteur : Yves Grange
* Version : 0.1
* Lase update : 10/10/2014
*
* Based on: Valentin CARRUESCO aka idleman and Manuel Esteban aka Yaug (http://manuel-esteban.com) work
*/
#define senderPin 12
long codeKit = 1000; // Your unique ID for your Arduino node
int BytesType[] = {1,0,1,0}; // Protocol Code: here it's 10d (1010b) can be what you want
int Bytes[30];
int BytesData[30];
boolean Timing = false; // Print timings on serial when eq to true
// This function will return the value you want to send over RF, insert here code to calculate the value you want to send over the air
boolean getCounter(unsigned long *CounterValue){
*CounterValue = 1073741823;
//*CounterValue = 67129;
return true;
}
void itob(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
Bytes[i]=1;
}
else Bytes[i]=0;
}
}
void itobCounter(unsigned long integer, int length)
{
for (int i=0; i<length; i++){
if ((integer / power2(length-1-i))==1){
integer-=power2(length-1-i);
BytesData[i]=1;
}
else BytesData[i]=0;
}
}
unsigned long power2(int power){ //gives 2 to the (power)
unsigned long integer=1;
for (int i=0; i<power; i++){
integer*=2;
}
return integer;
}
/**
* Crée notre signal sous forme binaire
**/
void buildSignal()
{
Serial.println(codeKit);
// Converti les codes respectifs pour le signal en binaire
itob(codeKit, 14);
for(int j=0;j < 14; j++){
Serial.print(Bytes[j]);
}
Serial.println();
}
// Convert 0 in 01 and 1 in 10 (Manchester conversion)
void sendPair(bool b) {
if(b)
{
sendBit(true);
sendBit(false);
}
else
{
sendBit(false);
sendBit(true);
}
}
//Envois d'une pulsation (passage de l'etat haut a l'etat bas)
//1 = 310µs haut puis 1340µs bas
//0 = 310µs haut puis 310µs bas
void sendBit(bool b) {
if (b) {
digitalWrite(senderPin, HIGH);
delayMicroseconds(506); //305 orinally, but tweaked.
if (Timing) Serial.print("506 ");
digitalWrite(senderPin, LOW);
delayMicroseconds(2024); //1225 orinally, but tweaked.
if (Timing) Serial.print("2024 ");
}
else {
digitalWrite(senderPin, HIGH);
delayMicroseconds(506); //305 orinally, but tweaked.
if (Timing) Serial.print("506 ");
digitalWrite(senderPin, LOW);
delayMicroseconds(4301); //305 orinally, but tweaked.
if (Timing) Serial.print("4301 ");
}
}
/**
* Transmit data
* @param boolean positive : if the value you send is a positive or negative one
* @param long Counter : the value you want to send
**/
void transmit(boolean positive, unsigned long Counter)
{
int i;
itobCounter(Counter, 30);
// Send the unique ID of your Arduino node
for(i=0; i<14;i++)
{
sendPair(Bytes[i]);
}
// Send protocol type
for(int j = 0; j<4; j++)
{
sendPair(BytesType[j]);
}
// Send the flag to mark the value as positive or negative
sendPair(positive);
// Send value (ie your counter)
for(int j = 0; j<30; j++)
{
sendPair(BytesData[j]);
}
// Send the flag "End of the transmission"
digitalWrite(senderPin, HIGH);
if (Timing) Serial.print("506 ");
delayMicroseconds(506);
digitalWrite(senderPin, LOW);
if (Timing) Serial.print("8602 ");
delayMicroseconds(8602);
}
void setup()
{
pinMode(senderPin, OUTPUT);
Serial.begin(115200);
Serial.println("setup");
buildSignal();
}
void loop()
{
unsigned long CounterValue;
Serial.println("in loop");
// Get Counter value
if(getCounter(&CounterValue)) {
// Display data on serial
Serial.print("Compteur : ");
Serial.print(CounterValue);
Serial.println();
transmit(true, CounterValue);
Serial.println();
transmit(true, CounterValue);
Serial.println();
transmit(true, CounterValue);
Serial.println();
}
delay(3000);
}