IR HEX signaal te kort bv. FFA25D wordt 5D

Besten allemaal,

Al enige tijd ben ik bezig met een tundercloud lamp project.

Ik loop tegen een probleem aan met de IR remote. Er worden 2 arduino uno's gebruikt de eerste ontvangt het signaal en stuurt deze door naar de tweede die de juiste modus van de lamp aanstuurt.

Bij de serieel monitor ir ontvanger: FFA25D
Bij de serieel monitor cloud lamp: 5D

Het signaal wordt afgekort na de laatste 2 van het HEX signaal hierdoor is er veel 'ruis' van bijvoorbeeld de tv afstandsbediening.

waar komt die afkorting vandaan?

Alvast bedankt voor het lezen en hopelijk een oplossing

hieronder de 2 codes

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Wire.begin(); // Start I2C Bus as Master
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    
    if(results.value != 0xFFFFFFFF){
      Serial.println(results.value,HEX);
      //only transmit if its not a repeat value. theyre useless. you may need to 
      // adjust for your own remote repeat values
      Wire.beginTransmission(9);
      Wire.write(results.value);
      Wire.endTransmission();
    }
    irrecv.enableIRIn();
    irrecv.resume(); // Receive the next value
  }
}

de Code paste niet in een bericht :slight_smile:

/* 
Lighting Cloud Mood Lamp By James Bruce
View the full tutorial and build guide at http://www.makeuseof.com/

Sound sampling code originally by Adafruit Industries.  Distributed under the BSD license.
This paragraph must be included in any redistribution.
*/

#include <Wire.h>
#include "FastLED.h"
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
#include "Arduino.h"

// How many leds in your strip?
#define NUM_LEDS 85
#define DATA_PIN 6

int rxPin = 10;    // DFplayer RX to Arduino pin 10
int txPin = 11;    // DFplayer TX toArduinopin 11
int busyPin = 12;  // DFplayer BUSY connected to pin 12

SoftwareSerial mySoftwareSerial(rxPin, txPin);
DFRobotDFPlayerMini myDFPlayer;



// Mode enumeration - if you want to add additional party or colour modes, add them here; you'll need to map some IR codes to them later; 
// and add the modes into the main switch loop
enum Mode { CLOUD,ACID,OFF,ON,RED,GREEN,BLUE,FADE,WHITE,LUMENS_UP,LUMENS_DOWN,SOUND};
Mode mode = CLOUD;  
Mode lastMode = ON;

// Mic settings, shouldn't need to adjust these. 
#define MIC_PIN   0  // Microphone is attached to this analog pin
#define DC_OFFSET  0  // DC offset in mic signal - if unusure, leave 0
#define NOISE     10  // Noise/hum/interference in mic signal
#define SAMPLES   10  // Length of buffer for dynamic level adjustment
byte
  volCount  = 0;      // Frame counter for storing past volume data
int
  vol[SAMPLES];       // Collection of prior volume samples
int      n, total = 30;
float average = 0;
int LUMENS = 255; //helderheid instellen

 
// used to make basic mood lamp colour fading feature
int fade_h;
int fade_direction = 1;


// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() { 
  // this line sets the LED strip type - refer fastLED documeantion for more details https://github.com/FastLED/FastLED
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  // starts the audio samples array at volume 15. 
  memset(vol, 15, sizeof(vol));
  Serial.begin(4800);
  Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
  Wire.onReceive(receiveEvent); // register event

//geluid test deel

  pinMode(busyPin, INPUT);
 
  mySoftwareSerial.begin(9600);
  Serial.begin(115200);
  Serial.println(F("Initializing DFPlayer..."));
 
  if (!myDFPlayer.begin(mySoftwareSerial)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin. Check connection and SD card, or reset the Arduino."));
    while (true);
  }
 
  Serial.println(F("DFPlayer Mini online."));
 
  myDFPlayer.setTimeOut(500);                   // Set serial communictaion time out 500ms
  myDFPlayer.volume(30);                        // Set volume value (0~30).
  myDFPlayer.EQ(DFPLAYER_EQ_BASS);              // Set EQ to BASS (normal/pop/rock/jazz/classic/bass)
  myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);  // Set device we use SD as default
  myDFPlayer.enableDAC();                       // Enable On-chip DAC
}




void receiveEvent(int bytes) {
  
  // Here, we set the mode based on the IR signal received. Check the debug log when you press a button on your remote, and 
  // add the hex code here (you need 0x prior to each command to indicate it's a hex value)
  while(Wire.available())
   { 
      unsigned int received = Wire.read();
      Serial.print("Receiving IR hex: ");
      Serial.println(received,HEX);
      lastMode = mode;
      switch(received){
        case 0xE7:
          mode = ON; break;
        case 0xAD:
          mode = OFF; break;
        case 0xFD:
          mode = CLOUD; break;
        case 0xDD:
          mode = ACID; break;
        case 0x3D:
          mode = FADE; break;
        case 0x97:
          mode = BLUE; break;
        case 0x67:
          mode = GREEN; break;
        case 0x4F:
          mode = RED; break;
        case 0xCF:
          mode = WHITE; break;
        case 0xBD:
          mode = LUMENS_UP; break;
        case 0xB5:
          mode = LUMENS_DOWN; break;
        case 0xA5:
          mode = SOUND; break;
        
      }
   }

}
 
void loop() { 
  
  // Maps mode names to code functions. 
  switch(mode){
    case CLOUD: detect_thunder();reset();break;
    case ACID: acid_cloud();reset();break;
    case OFF:reset();break;
    case ON: constant_lightning();reset();break;
    case RED: single_colour(0);break;
    case BLUE: single_colour(160);break;
    case GREEN: single_colour(96);break;
    case FADE: colour_fade();break;
    case SOUND: play_sound();break;
    case WHITE: white_colour(0);break;
    case LUMENS_UP: lumens_minus();break;
    case LUMENS_DOWN: lumens_plus();break;
    default: detect_thunder(); reset();break; 
  }
  
}

// Makes all the LEDs a single colour, see https://raw.githubusercontent.com/FastLED/FastLED/gh-pages/images/HSV-rainbow-with-desc.jpg for H values
void single_colour(int H){
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( H, 255, LUMENS);
  }
  //avoid flickr which occurs when FastLED.show() is called - only call if the colour changes
  if(lastMode != mode){
    FastLED.show(); 
    lastMode = mode;
  }
  delay(50);
}

// Makes all the LEDs a single colour, see https://raw.githubusercontent.com/FastLED/FastLED/gh-pages/images/HSV-rainbow-with-desc.jpg for H values
void white_colour(int H){
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( H, 0, LUMENS);
  }
  //avoid flickr which occurs when FastLED.show() is called - only call if the colour changes
  if(lastMode != mode){
    FastLED.show(); 
    lastMode = mode;
  }
  delay(50);
}

void lumens_plus() {
  LUMENS = LUMENS + 10;
  if (LUMENS > 255) LUMENS = 255;
  if (LUMENS < 0) LUMENS = 0;
  mode = CLOUD;
}

void lumens_minus() {
  LUMENS = LUMENS - 10;
  if (LUMENS > 255) LUMENS = 255;
  if (LUMENS < 0) LUMENS = 0;
  mode = CLOUD;
}

void colour_fade(){
  //mood mood lamp that cycles through colours
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( fade_h, 255, LUMENS);
  }
  if(fade_h >254){
    fade_direction = -1; //reverse once we get to 254
  }
  else if(fade_h < 0){
    fade_direction = 1;
  }
    
  fade_h += fade_direction;
  FastLED.show();
  delay(100);
}

En helaas gaat de code ook over de 9000 tekens heen! tijd om hem wat schoner te schrijven
Hierbij het laatste deel

void detect_thunder() {
  
  n   = analogRead(MIC_PIN);                        // Raw reading from mic 
  n   = abs(n - 512 - DC_OFFSET); // Center on zero
  n   = (n <= NOISE) ? 0 : (n - NOISE);             // Remove noise/hum
  vol[volCount] = n;                      // Save sample for dynamic leveling
  if(++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
 
  total = 0;
  for(int i=0; i<SAMPLES; i++) {
    total += vol[i];
  }
  
  // If you're having trouble getting the cloud to trigger, uncomment this block to output a ton of debug on current averages. 
  // Note that this WILL slow down the program and make it less sensitive due to lower sample rate.
  
  /*
  for(int t=0; t<SAMPLES; t++) {
    //initial data is zero. to avoid initial burst, we ignore zero and just add the current l
    Serial.print("Sample item ");
    Serial.print(t);
    Serial.print(":");
    Serial.println(vol[t]);
  }
  Serial.print("total");
  Serial.println(total);
  Serial.print("divided by sample size of ");
  Serial.println(SAMPLES);
  
 
  Serial.print("average:");
  Serial.println(average);

  Serial.print("current:");
  Serial.println(n);
  */
  
  average = (total/SAMPLES)+2;
  if(n>average){
    Serial.println("TRIGGERED");
    reset();
     
   
    //I've programmed 3 types of lightning. Each cycle, we pick a random one. 
    switch(random(1,3)){
       //switch(3){
  
      case 1:
        thunderburst();
        delay(random(10,500));
         Serial.println("Thunderburst");
        break;
       
      case 2:
        rolling();
        Serial.println("Rolling");
        break;
        
      case 3:
        crack();
        delay(random(50,250));
        Serial.println("Crack");
        break;
        
      
    }
  }
}
 
 
// utility function to turn all the lights off.  
void reset(){
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( 0, 0, 0);
  }
  FastLED.show();
   
}

void acid_cloud(){
    // a modification of the rolling lightning which adds random colour. trippy. 
    //iterate through every LED
    for(int i=0;i<NUM_LEDS;i++){
      if(random(0,100)>90){
        leds[i] = CHSV( random(0,255), 255, LUMENS); 

      }
      else{
        leds[i] = CHSV(0,0,0);
      }
    }
    FastLED.show();
    delay(random(5,100));
    reset();
    
  //}
}

void rolling(){
  // a simple method where we go through every LED with 1/10 chance
  // of being turned on, up to 10 times, with a random delay wbetween each time
  for(int r=0;r<random(2,10);r++){
    //iterate through every LED
    for(int i=0;i<NUM_LEDS;i++){
      if(random(0,100)>90){
        leds[i] = CHSV( 0, 0, LUMENS); 

      }
      else{
        //dont need reset as we're blacking out other LEDs her 
        leds[i] = CHSV(0,0,0);
      }
    }
    FastLED.show();
    delay(random(5,100));
    reset();
    
  }
}

void crack(){
   //turn everything white briefly
   for(int i=0;i<NUM_LEDS;i++) {
      leds[i] = CHSV( 0, 0, LUMENS);  
   }
   FastLED.show();
   delay(random(10,100));
   reset();
}

void thunderburst(){

  // this thunder works by lighting two random lengths
  // of the strand from 10-20 pixels. 
  int rs1 = random(0,NUM_LEDS/2);
  int rl1 = random(10,20);
  int rs2 = random(rs1+rl1,NUM_LEDS);
  int rl2 = random(10,20);
  
  //repeat this chosen strands a few times, adds a bit of realism
  for(int r = 0;r<random(3,6);r++){
    
    for(int i=0;i< rl1; i++){
      leds[i+rs1] = CHSV( 0, 0, LUMENS);
    }
    
    if(rs2+rl2 < NUM_LEDS){
      for(int i=0;i< rl2; i++){
        leds[i+rs2] = CHSV( 0, 0, LUMENS);
      }
    }
    
    FastLED.show();
    //stay illuminated for a set time
    delay(random(10,50));
    
    reset();
    delay(random(10,50));
  }
  
}

// basically just a debug mode to show off the lightning in all its glory, no sound reactivity. 
void play_sound(){
  switch(random(1,10))
  switch(random(1,10)){
   case 1:
        thunderburst();
        delay(random(10,500));
         Serial.println("Thunderburst");
        break;
       
      case 2:
        rolling();
        Serial.println("Rolling");
        break;
        
      case 3:
        crack();
        delay(random(50,250));
        Serial.println("Crack");
        break;

          }
  int thunderDelay = random (500, 3000);  // Min. and max. delay between flashing and playing sound
  int thunderFile = random (1, 17);       // There are 17 soundfiles: 0001.mp3 ... 0017.mp3
  int loopDelay = random (5000, 30000);   // Min. and max. delay between each loop
 
 
  Serial.print(F("Pausing before playing thunder sound, milliseconds: "));
  Serial.println(thunderDelay);
  delay(thunderDelay);
 
  Serial.print(F("Playing thunder sound, file number: "));
  Serial.println(thunderFile);
  myDFPlayer.playMp3Folder(thunderFile);
  delay(1000); // Give the DFPlayer some time
 
  while (digitalRead(busyPin) == LOW) { // Wait for the DFPlayer to finish playing the MP3 file
  }
 
  Serial.print(F("Pausing before next loop, milliseconds: "));
  Serial.println(loopDelay);
  delay(loopDelay);
 
}

void constant_lightning(){
  switch(random(1,10)){
   case 1:
        thunderburst();
        delay(random(10,500));
         Serial.println("Thunderburst");
        break;
       
      case 2:
        rolling();
        Serial.println("Rolling");
        break;
        
      case 3:
        crack();
        delay(random(50,250));
        Serial.println("Crack");
        break;
  }
        
}

Op de manier waarop je Wire.write() gebruikt wordt er inderdaad slechts een byte verstuurd (het argument is een byte dus wordt de rest weggegooid). Er is een andere write methode die twee argumenten heeft, een pointer naar de data en het aantal bytes dat verstuurd moet worden.

Ik denk dat het onderstaande werkt; beetje te lui om te testen.

uint32_t value = results.value;
Wire.write((byte*)&value, sizeof(value));

PS
Je had een klein demo programma kunnen schrijven dat het probleem toonde. Of je had de code kunnen bijvoegen als aanhangsel.

Bedankt voor je antwoord sterretje,

Hiermee stuurt die inderdaad de hele hex code van de IR receiver door.

Dit komt alleen niet binnen als een code maar in stukken.

77E14036

Receiving IR hex: 36
Receiving IR hex: 40
Receiving IR hex: E1
Receiving IR hex: 77

Zend die nu niet goed uit of ontvangt de tweede arduino het niet goed :sweat_smile:

IR reviever

#include <Wire.h>
#include <IRremote.h>

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Wire.begin(); // Start I2C Bus as Master
  Serial.begin(115200);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    
    if(results.value != 0xFFFFFFFF){
      Serial.println(results.value,HEX);
      //only transmit if its not a repeat value. theyre useless. you may need to 
      // adjust for your own remote repeat values
      Wire.beginTransmission(9);
      uint32_t value = results.value;
      Wire.write((byte*)&value, sizeof(value));
      Wire.endTransmission();
    }
    irrecv.enableIRIn();
    irrecv.resume(); // Receive the next value
  }

Ontvanger

void receiveEvent(int bytes) {
  
  // Here, we set the mode based on the IR signal received. Check the debug log when you press a button on your remote, and 
  // add the hex code here (you need 0x prior to each command to indicate it's a hex value)
  while(Wire.available())
   { 
      unsigned int received = Wire.read();
      Serial.print("Receiving IR hex: ");
      Serial.println(received,HEX);
      lastMode = mode;
      switch(received){
        case 0xE7:
          mode = ON; break;
        case 0xAD:
          mode = OFF; break;
        case 0xFD:
          mode = CLOUD; break;
        case 0xDD:
          mode = ACID; break;
        case 0x3D:
          mode = FADE; break;
        case 0x97:
          mode = BLUE; break;
        case 0x67:
          mode = GREEN; break;
        case 0x4F:
          mode = RED; break;
        case 0xCF:
          mode = WHITE; break;
        case 0xBD:
          mode = LUMENS_UP; break;
        case 0xB5:
          mode = LUMENS_DOWN; break;
        case 0xA5:
          mode = SOUND; break;
        
      }
   }

}

Wire.read() leest maar een (1) byte. Ik ben niet zo bekend met I2C maar je hebt een argument in je receiveEvent functie die een hoeveelheid bytes aangeeft; je moet dat aantal bytes lezen en vervolgens kun je die combineren in een unsigned int.

Daarna kun je analyseren in je switch/case. Onderstaand een start die je kunt proberen.

void receiveEvent(int bytes) {

  // Here, we set the mode based on the IR signal received. Check the debug log when you press a button on your remote, and
  // add the hex code here (you need 0x prior to each command to indicate it's a hex value)

  // create a buffer that can store sufficient data
  byte buffer[bytes];
  // index in buffer
  byte index = 0;

  Serial.print("number of bytes = "); Serial.println(bytes);

  while (1)
  {
    if (Wire.available())
    {
      // read a byte from I2C
      buffer[index++] = Wire.read();

      // when we have read enough bytes
      if (index == bytes)
      {
        break;
      }
    }
  }

  Serial.print("received : ");
  for (byte index = 0; index < sizeof(buffer); index++)
  {
    Serial.print(buffer[index], HEX);
    Serial.print(" ");
  }
  Serial.println();

  uint32_t *received;
  received  = (uint32_t*)buffer;

  Serial.println(*received, HEX);

}

De code gaat ervan uit dat je buffer een uint32_t bevat; received is een pointer naar een uint32_t en via een cast kun je de pointer naar de buffer daaraan toekennen. Dus via received kun je de buffer behandelen als een uint32_t. Om te kunnen werken met received, heb je de * nodig in e.g. Serial.print.

Als je hier de juiste data te zien krijgt, kun je de switch / case implementeren; denk eraan, __*__received, niet received.

Indien het aantal bytes niet 4 is, heb je nog een probleem dat ik niet snel voor je kan oplossen.

Heb het even snel geprobeerd en het ziet er veel belovend uit!

Het ziet er nu als volgt uit

number of bytes = 4
received : 36 40 E1 77
77E14036

Hierdoor kan die de hele code perfect lezen! :smiley:

Super bedankt!

Had het zelf nooit kunnen bedenken moet het meer hebben van dingen die al eens gedaan zijn door iemand! Maar al doende wordt ik er steeds beter mee.

Ga lekker veder hobbyen heb nog 2 dingen in gedachten

  • sound to light (kan je veel van vinden op google alleen even zoeken naar alle leds 1 kleur)
  • led color fade stoppen op een bepaalde kleur door nogmaals te drukken op remote (dit is zo ver ik nu heb gevonden een stuk lastiger doordat het niet in de fastLED fade bibliotheek voorkomt volgensmij)

nogmaals bedankt voor de hulp!!