Beste mede programmeurs,
Ik ben bezig met een code voor het aansturen van een somfy controller. Het idee is dat er 4 drukknoppen aangesloten worden (up,down,stop en select) En 4leds voor het uitlezen van het kanaal dat actief is. De Arduino zou moeten schakelen naar het juiste kanaal als deze er niet in bevind. EN dan het zonnescherm omhoog/omlaag sturen. Als ik via de serial monitor bijvoorbeeld c2u stuur gebreurt er niets. Het lijkt net of de case niet gestart word. Kan iemand me er weer mee op weg helpen.
Relevante software en hardware die ik gebruik
Arduino software en de Arduino nano v3
Wat ik al gevonden of geprobeerd heb
Ik heb al de switch cace in de void loop verplaats maar dit heeft geen positief effect.
De Arduino code >
Ik ben bezig met een code voor het aansturen van een somfy controller. Het idee is dat er 4 drukknoppen aangesloten worden (up,down,stop en select) En 4leds voor het uitlezen van het kanaal dat actief is. De Arduino zou moeten schakelen naar het juiste kanaal als deze er niet in bevind. EN dan het zonnescherm omhoog/omlaag sturen. Als ik via de serial monitor bijvoorbeeld c2u stuur gebreurt er niets. Het lijkt net of de case niet gestart word. Kan iemand me er weer mee op weg helpen.
Relevante software en hardware die ik gebruik
Arduino software en de Arduino nano v3
Wat ik al gevonden of geprobeerd heb
Ik heb al de switch cace in de void loop verplaats maar dit heeft geen positief effect.
De Arduino code >
code:
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
| // Test Somfy // digital pin numbers for LED connections #define SOMFY_LED1_APIN 17 #define SOMFY_LED2_APIN 16 #define SOMFY_LED3_APIN 15 #define SOMFY_LED4_APIN 14 // digital pin numbers for button connections #define SOMFY_BUTTON_UP 6 #define SOMFY_BUTTON_DOWN 8 #define SOMFY_BUTTON_MY 7 #define SOMFY_BUTTON_MENU 9 boolean upPress = false; boolean downPress = false; boolean myPress = false; boolean progPress = false; boolean selectPress = false; unsigned long holdTime = 0; int repeatSend = 0; char incomingChar; // the setup routine runs once when you press reset: void setup() { // initialize the digital pins as an output. pinMode(SOMFY_BUTTON_UP, OUTPUT); pinMode(SOMFY_BUTTON_DOWN, OUTPUT); pinMode(SOMFY_BUTTON_MY, OUTPUT); pinMode(SOMFY_BUTTON_MENU, OUTPUT); // delay setup to prevent digital outs to go high during serial initialization delay(1000); Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Arduino started!"); } // function to press a button on the remote int press_button(int button){ digitalWrite(button, HIGH); delay(150); digitalWrite(button, LOW); } // function to get the current channel on the remote // LEDs need to be blinking, i.e. channel selection button must have been pressed int get_channel(){ int sel_channel=0; // the current channel int iLED1=1; // status LED1: 1=OFF, 0=ON int iLED2=1; // status LED2: 1=OFF, 0=ON int iLED3=1; // status LED3: 1=OFF, 0=ON int iLED4=1; // status LED4: 1=OFF, 0=ON int iLEDs; // status LED1-4 in a byte // read the LEDs from the remote and determine the current channel // make sure we detect a blinking LED, they go on and off for about 70ms // the body of the loop takes <1ms, so check every 10ms during 100ms to capture a full cycle // time1=millis(); for (int i=0; i<10; i++){ iLED1 &= digitalRead(SOMFY_LED1_APIN); iLED2 &= digitalRead(SOMFY_LED2_APIN); iLED3 &= digitalRead(SOMFY_LED3_APIN); iLED4 &= digitalRead(SOMFY_LED4_APIN); delay(10); } iLEDs = iLED4<<3 | iLED3<<2 | iLED2<<1 | iLED1; switch(iLEDs){ case 14: sel_channel=1; break; // LED1, channel 1 selected case 13: sel_channel=2; break; // LED2, channel 2 selected case 11: sel_channel=3; break; // LED3, channel 3 selected case 7: sel_channel=4; break; // LED4, channel 4 selected case 0: sel_channel=5; break; // ALL, channel 5 selected default: sel_channel=0; // no LEDs are blinking } return sel_channel; } // function to select a channel on the remote // return true if succeeded // return false if something has gone wrong bool select_channel(int ch){ int curr; // current channel int prev=-1; // previous channel int t=0; // counter, if after 10 channel reads after button press the channel is not OK, something is wrong // while the current channel is not equal to channel 'ch' go to the next channel by pressing the channel select button // the button is only pressed when the previous channel selection changed, because get_channel while((curr=get_channel())!=ch && t<10){ t++; if (prev<0 || (curr!=prev && curr>0)){ press_button(SOMFY_BUTTON_MENU); prev=curr; t=0; } } return t<10 || curr==ch; } // process the URL command send by the web client int process_cmd(char* cmd, char* msg){ // do something with the 'cmd' received from the client // // A command consists of max 3 characters: // // SOMFY CHANNEL COMMAND: // // c<channel><action> // // <channel>: number between 1-5 // <action>: 'u' for up // 'd' for down // 's' for stop // // example: "c2u" sends an up command to the Somfy remote on channel 2 char ich; // the channel to select msg[0]=0; if (cmd == 'c' ){ switch (cmd[0]){ case 'c': // Somfy channel command // get the channel number ich=cmd[1]-'1'+1; // select the channel if (ich>=1 && ich<=5){ if(select_channel(ich)){ // perform the action switch(cmd[2]){ case 'u': press_button(SOMFY_BUTTON_UP); Serial.println("Selected channel %d, pressed button UP"); break; case 'd': press_button(SOMFY_BUTTON_DOWN); Serial.println("Selected channel %d, pressed button DOWN"); break; case 's': press_button(SOMFY_BUTTON_MY); Serial.println("Selected channel %d, pressed button MY"); break; default: Serial.println("Unknown command: "); } } else{ Serial.println("Channel selection failed"); } } break; } } } void loop(){ char cmd; // buffer to store the command from web client while (Serial.available() == 0) { delay(50); } // wait until serial command comes in if (Serial.available() > 0) { cmd = Serial.read(); Serial.println(cmd); if (incomingChar == 'u') //{upPress = true;} if (incomingChar == 'd') //{downPress = true;} if (incomingChar == 'm') //{myPress = true;} if (incomingChar == 'p') //{progPress = true;} if (incomingChar == 's') //{selectPress = true;} if (incomingChar == '1') //{holdTime += 100;} if (incomingChar == '2') //{holdTime += 200;} if (incomingChar == '3') //{holdTime += 300;} if (incomingChar == '4') //{holdTime += 400;} if (incomingChar == '5') //{holdTime += 500;} if (incomingChar == '6') //{holdTime += 600;} if (incomingChar == '7') //{holdTime += 700;} if (incomingChar == '8') //{holdTime += 800;} if (incomingChar == '9') //{holdTime += 900;} if (incomingChar == 'r') //{repeatSend++;} if (incomingChar == 'e') {somSend();} } } void somSend() { Serial.print("Pressing: "); for(int i=0; i <= repeatSend; i++) { if (upPress) {digitalWrite(SOMFY_BUTTON_UP, LOW); upPress = false; Serial.print("up ");} if (downPress) {digitalWrite(SOMFY_BUTTON_DOWN, LOW); downPress = false; Serial.print("down ");} if (myPress) {digitalWrite(SOMFY_BUTTON_MY, LOW); myPress = false; Serial.print("my ");} if (selectPress) {digitalWrite(SOMFY_BUTTON_MENU, LOW); selectPress = false; Serial.print("select ");} Serial.print("for "); Serial.print(holdTime); Serial.println(" ms"); delay(holdTime); digitalWrite(SOMFY_BUTTON_UP, HIGH); digitalWrite(SOMFY_BUTTON_DOWN, HIGH); digitalWrite(SOMFY_BUTTON_MY, HIGH); digitalWrite(SOMFY_BUTTON_MENU, HIGH); delay(holdTime); } holdTime = 0; repeatSend = 0; Serial.println("done."); } |