IR receiver op esp8266 software probleem

Pagina: 1
Acties:

Onderwerpen

Vraag


Acties:
  • 0 Henk 'm!

  • nanke
  • Registratie: Februari 2008
  • Laatst online: 10-09 19:36
Ik probeer een 3-kleuren led aan te sturen met een afstandsbediening, door gebruikt te maken van een infrarood sensor aangesloten op een esp8266. De bedoeling is dat wanneer een bepaalde code wordt ingevoerd, de LED gaat branden. Ik heb daarvoor het SimpleReceiver voorbeeld uit de Arduino-IRremote library gebruikt. Dit programma werkte goed, tot ik probeerde om de LED te programmeren. Ik denk dat het komt door de delay die ik gebruik, want als ik de LED functie wegcomment werkt het programma weer. Nadat ik een functie aanroep om de LED te laten branden, krijg ik geen verdere IR signalen meer binnen. Heeft iemand enig idee hoe ik dit probleem zou kunnen oplossen?

Hier is mijn 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Specify which protocol(s) should be used for decoding.
 * If no protocol is defined, all protocols (except Bang&Olufsen) are active.
 * This must be done before the #include <IRremote.hpp>
 */
//#define DECODE_DENON        // Includes Sharp
//#define DECODE_JVC
//#define DECODE_KASEIKYO
//#define DECODE_PANASONIC    // alias for DECODE_KASEIKYO
//#define DECODE_LG
#define DECODE_NEC          // Includes Apple and Onkyo
#define DECODE_SAMSUNG
//#define DECODE_SONY
#define DECODE_RC5
#define DECODE_RC6

//#define DECODE_BOSEWAVE
//#define DECODE_LEGO_PF
//#define DECODE_MAGIQUEST
//#define DECODE_WHYNTER
//#define DECODE_FAST

//#define DECODE_DISTANCE_WIDTH // Universal decoder for pulse distance width protocols
//#define DECODE_HASH         // special decoder for all protocols

//#define DECODE_BEO          // This protocol must always be enabled manually, i.e. it is NOT enabled if no protocol is defined. It prevents decoding of SONY!

//#define DEBUG               // Activate this for lots of lovely debug output from the decoders.

//#define RAW_BUFFER_LENGTH  180  // Default is 112 if DECODE_MAGIQUEST is enabled, otherwise 100.

#include <Arduino.h>

/*
 * This include defines the actual pin number for pins like IR_RECEIVE_PIN, IR_SEND_PIN for many different boards and architectures
 */
#include "PinDefinitionsAndMore.h"
#include <IRremote.hpp> // include the library

 char next_answer = '0';
 int fout_answer = 0;

const int PIN_RED   = 16;
const int PIN_GREEN = 5;
const int PIN_BLUE  = 4;

void setup() {
    Serial.begin(115200);
    // Just to know which program is running on my Arduino
    Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

    Serial.print(F("Ready to receive IR signals of protocols: "));
    printActiveIRProtocols(&Serial);
    Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));

  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
}

void loop() {
  char result;
    /*
     * Check if received data is available and if yes, try to decode it.
     * Decoded result is in the IrReceiver.decodedIRData structure.
     *
     * E.g. command is in IrReceiver.decodedIRData.command
     * address is in command is in IrReceiver.decodedIRData.address
     * and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
     */
    
    if (IrReceiver.decode()) {
        Serial.println("start loop");
        /*
         * Print a short summary of received data
         */
        //IrReceiver.printIRResultShort(&Serial);
        //IrReceiver.printIRSendUsage(&Serial);
        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
            // We have an unknown protocol here, print more info
            IrReceiver.printIRResultRawFormatted(&Serial, true);
        }
        Serial.println();

        /*
         * !!!Important!!! Enable receiving of the next value,
         * since receiving has stopped after the end of the current received data packet.
         */
        IrReceiver.resume(); // Enable receiving of the next value

        /*
         * Finally, check the received data and perform actions according to the received command
         */

        //Serial.print("protocol is: ");
        //Serial.println(IrReceiver.decodedIRData.protocol);
        // 073
        // samsung is 19: [0x11, 0xC, 0x6]
        // NEC (LG) is 8: [0x10, 0x17, 0x13]
        // Philips (RC5) is 17: []
        // Philips (RC6) is 18: [0x0, 0x7, 0x3]
        //unknown is 0

        result = ProtocolToNumber(IrReceiver.decodedIRData.protocol, IrReceiver.decodedIRData.command);
        Serial.println(result);

      if (result == next_answer){
        if (next_answer == '0'){
          next_answer = '7';
          fout_answer = 0;
        }
        else if (next_answer == '7'){
          next_answer = '3';
          fout_answer = 0;
        }
        else if (next_answer = '3'){
          Serial.println("de code is goed");
          next_answer = '0';
          fout_answer = 0;
          IrReceiver.stop();
          lichtshow();
          Serial.println("einde show");

          
        }
      }
      else {
          fout_answer ++;
          Serial.println(fout_answer);
        }

      if (fout_answer >= 5){
        next_answer = '0';
        fout_answer = 0;
        foutlicht();
        Serial.println("einde foutmelding");
        
      }
    }
}

int ProtocolToNumber(int protocol, uint16_t command){
  char result;
  if (protocol == 19){
    if (command == 0x11){
      result = '0';
    }
    else if (command == 0xC){
      result = '7';
    }
    else if (command == 0x6){
      result = '3';
    }
    else{
      result = 'x';
    }
  }

    if (protocol == 8){
    if (command == 0x10){
      result = '0';
    }
    else if (command == 0x17){
      result = '7';
    }
    else if (command == 0x13){
      result = '3';
    }
    else{
      result = 'x';
    }
  }

    if (protocol == 18){
       if (command == 0x0){
        result = '0';
      }
       else if (command == 0x7){
        result = '7';
      }
        else if (command == 0x3){
        result = '3';
      }
        else{
        result = 'x';
      }
    }

  return result;
}

void lichtshow(){
  for (int j=0 ;j <=1;j++){
    for (int i=10; i<=255;i++){
    analogWrite(PIN_RED,   10);
    analogWrite(PIN_GREEN, i);
    analogWrite(PIN_BLUE,  255-i);
    delay(7);
    }

    delay(30);

    for (int i=10; i<=255;i++){
    analogWrite(PIN_RED,   i);
    analogWrite(PIN_GREEN, 255-i);
    analogWrite(PIN_BLUE,  10);
    delay(7);
    }

    delay(30);

    for (int i=10; i<=255;i++){
    analogWrite(PIN_RED,   255-i);
    analogWrite(PIN_GREEN, 10);
    analogWrite(PIN_BLUE,  i);
    delay(7);
    }

    delay(30);
  }
    analogWrite(PIN_RED,   0);
    analogWrite(PIN_GREEN, 0);
    analogWrite(PIN_BLUE,  0);
  return;
}

void foutlicht(){
  /*for (int j=0 ;j <=5;j++){
    for (int i=10; i<=255;i++){
      analogWrite(PIN_RED, i);
      delay(1);
    }
    for (int i=10; i<=255;i++){
      analogWrite(PIN_RED, 255-i);
      delay(1);
    }
  }
  Serial.println("fout");
  analogWrite(PIN_RED,   0);*/
  return;
}

Alle reacties


Acties:
  • 0 Henk 'm!

  • Septillion
  • Registratie: Januari 2009
  • Laatst online: 22:59

Septillion

Moderator Wonen & Mobiliteit
Yep, je hebt nu les twee van embedded C geleerd: "Na blink gebruik je nooit meer delay()" :)

Tijdens een delay() kan je niets anders doen. De oplossing is er een state machine van maken icm millis() als timer. Zie daarvoor de bekende BLink without delay.

Acties:
  • 0 Henk 'm!

  • nanke
  • Registratie: Februari 2008
  • Laatst online: 10-09 19:36
Bedankt voor je antwoord. Ik heb alle delay's verwijderd en heb alle wachttijden met millis geprogrammeerd.
Helaas blijft het probleem bestaan.

Na de lichtshow reageert het programma niet meer op de afstandsbediening: hij ontvangt geen codes meer.

Acties:
  • +2 Henk 'm!

  • Goz3rr
  • Registratie: Januari 2010
  • Laatst online: 12-09 21:49
Je roept IrReceiver.stop() aan voor de lichtshow, en start hem volgens mij daarna nooit weer opnieuw.

Acties:
  • 0 Henk 'm!

  • nanke
  • Registratie: Februari 2008
  • Laatst online: 10-09 19:36
Goz3rr schreef op maandag 13 november 2023 @ 15:58:
Je roept IrReceiver.stop() aan voor de lichtshow, en start hem volgens mij daarna nooit weer opnieuw.
Bedankt voor je reactie. Hier heb ik al van alles geprobeerd na het doorlopen van de lichtshow:
IrReceiver.resume();
IrReceiver.begin(
geen IrReceiver.stop

Etc. Helaas werken alle opties niet: na de lichtshow krijg ik geen codes meer binnen.
Het rode ledje op het ir boardje knippert nog wel na het drukken op de afstandsbediening, dus in de hardware gebeurt wel iets, maar in de software niet.

Acties:
  • +1 Henk 'm!

  • nanke
  • Registratie: Februari 2008
  • Laatst online: 10-09 19:36
Het heeft even geduurd, maar ik ben er eindelijk achter wat er fout gaat. In mijn programma zaten twee fouten:

1. Het programma is niet geschikt voor een ESP8266, je krijgt problemen met de watchdog timer. Er moet gebruik worden gemaakt van non-blocking programmeren.
2. De AnalogWrite functie moet niet gebruikt worden samen met de IR receiver. Deze maken beide gebruik van interrupts en dit werkt niet samen.

Het hele programma is herschreven en nu werkt het!
Pagina: 1