Go Down

Topic: Help me control 1 led strip by 2 microcontroller (Read 500 times) previous topic - next topic

sterretje

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


kaitoukid93

hello again, it's me asking stupid question
I've done the coding and the hardware, things worked as I expect
but there's 1 problem that I cant solve

here is the code
the switching  between fastled color palette and ambilight mode from stm32f4 is fine but when I press button to enter music mode(using FHT and fastled) (modes are defined using a "palettecounter")
FHT sequence reported to serial as expected but after enter this mode, I cant switch back because no IR signal recorded
I think there's problem with interrupt to handle IR
please help me
thanks
Code: [Select]
 


    #define LOG_OUT 0       
    #define LIN_OUT 1
    #define FHT_N 256     
    #define UPDATES_PER_SECOND 100
   
    #include <IRremote.h>
    #include <FastLED.h>
    #include <FHT.h>   
    #include <math.h> 
    #define NUM_LEDS 42
    #define DATA_PIN 8 
    CRGB leds[NUM_LEDS];
    float faktoren[7] = {1.75, 1.55, 1.45, 1.25, 1.45, 1.75};
    unsigned char hs[7] = {8, 8, 8, 8,8,8,8}; //height of each column
    float hue;
    int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
    int led1 = 7;  // fastled control pin
    int led2 = 10;//Stm32f4 control pin
    int led3 = 2;//status led pin
   
    int itsONled[] = {0,1};
    int palettecounter;
    unsigned long previousMillis = 0;
    unsigned long previousMillis2 = 0;
    unsigned long previousMillis3 = 0;
    int ledState = LOW;
    int videosignal=0;
    CRGBPalette16 currentPalette;
    TBlendType    currentBlending;

    extern CRGBPalette16 myRedWhiteBluePalette;
    extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
    void setBalken(unsigned char column, unsigned char height){                   //calculation of the height of each column
    unsigned char h = (unsigned char)map(height, 0, 255, 0, 7);
    h = (unsigned char)(h * faktoren[column]);
    if (h < hs[column]){
        hs[column]--;
    }
    else if (h > hs[column]){
        hs[column] = h;
    }
   if (height > 250){
      hue+=2;                     //CHANGE THIS VALUE IF YOU WANT THE DIFFERENCE BETWEEN THE COLORS TO BE BIGGER
      if(hue > 25) hue=0;
   }

    for(unsigned char y = 0; y < 8; y++){                          //set colors of pixels according to column and hue
       if(hs[column] > y){
        leds[y+(column*6)] = CHSV((hue*10)+(column*6), 255, 180);
       
       
       } else {
        leds[y+(column*6)] = CRGB::Black;
       }
    }
}
   unsigned char grenzen[8] = {0,3,5,7,9,11,13,16};          //borders of the frequency areas
    /* the initial state of LEDs is OFF (zero)
    the first zero must remain zero but you can
    change the others to 1's if you want a certain
    led to light when the board is powered */

    IRrecv irrecv(RECV_PIN);
     
    decode_results results;
     
    void setup()
    {
      delay(3000);
       Serial.begin(115200);   // you can comment this line
      irrecv.enableIRIn();  // Start the receiver
      FastLED.addLeds<WS2812B, DATA_PIN, GRB> (leds, NUM_LEDS);
      ADCSRA = 0xe5;                                                   
      ADMUX = 0b01000111;                                               
      DIDR0 = 0x01;                                                   
      analogReference(EXTERNAL);                                       
      currentPalette = RainbowColors_p;
      currentBlending = LINEARBLEND;
     
      pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      palettecounter=0;
      digitalWrite(led2, HIGH);
      digitalWrite(led1,LOW);
      itsONled[2]=1;
      itsONled[1]=0;
     
    }
    bool IR_idle() {
  return irrecv.decode(&results) || results.rawlen == 0;
}
 bool IR_notidle() {
  return irrecv.decode(&results) || results.rawlen != 0;
}
     
    void loop() {
      IR();
      fastled();
     
   
    }
   
    void IR()
    {
       if (irrecv.decode(&results)) {
       
        switch(results.value) {
           case 0xFF22DD: // M button on the remote
             if((itsONled[1] == 0)&&(itsONled[2]==1)) {        // if Ambilight mode is off and Music mode is on
                digitalWrite(led1, HIGH);   // turn off the music LED by pulling the Output Enable (D7) to HIGH
                digitalWrite(led2, LOW);    // turn on the Ambilight LED by pulling the Output Enable (D10) to LOW
                itsONled[1] = 1;           //
                itsONled[2] = 0;
                Serial.print("ambimode");
                delay(1000);
               
             }
             else if((itsONled[1] == 1)&&(itsONled[2]==0)) {  // else if Ambilight mode is on and Music mode is off
                 digitalWrite(led2, HIGH);// turn Ambilight mode off
                 digitalWrite(led1, LOW); // turn Music mode on
                 itsONled[1] = 0;          //
                 itsONled[2] = 1;
                 Serial.print("musicmode");
                 
             }
              break;
           
                 
               case 0xFF827D://playbutton
           // affect the system only when Music mode is on (LED data took from ATMEGA328)
              if((itsONled[1] == 0)&&(itsONled[2]==1)) {
               if (palettecounter < 3&& palettecounter>=0 ){
         palettecounter += 1;
                if(palettecounter>2){
                  palettecounter=0;
                }
               Serial.print (palettecounter);
               
             }
              break;

         
       }
       }
       
        irrecv.resume(); // Receive the next value
      }
    }

    void Status()// blink green led when STM32 report "6" to serial port
    {
      unsigned long currentMillis = millis();
      if (Serial.available() > 0) {
        videosignal=Serial.parseInt();
        if (videosignal==250){
          if (currentMillis - previousMillis >= 200) {
    previousMillis = currentMillis;
          if (ledState == LOW) {
         ledState = HIGH;
         } else {
          ledState = LOW;
          }
            digitalWrite(led3, ledState);
          }
         }
      }
    }
    void fastled()// fastled program for displaying effect ( music or color palette mode
                 //is defined by palettecounter's value
    {
      if (palettecounter==0){
unsigned long currentMillis2 = millis();
   if (currentMillis2 - previousMillis2 >=10) {//color palette update speed
 
    currentPalette = RainbowStripeColors_p;
    currentBlending = LINEARBLEND;
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* motion speed */
    FillLEDsFromPaletteColors( startIndex);
    // check if "interval" time has passed (1000 milliseconds)

     if(IR_idle()){     
    FastLED.show();
     }
    previousMillis2 = millis(); }
 }
   
 if (palettecounter==2)
   {
                                                 
    cli();                                                       
    for (int i = 0 ; i < 256 ; i+=1) {                           
      while(!(ADCSRA & 0x10));                                     
      ADCSRA = 0xf5;                                               
      byte m = ADCL;                                               
      byte j = ADCH;
      int k = (j << 8) | m;                                       
      k -= 0x0200;                                                 
      k <<= 6;                                                     
      fht_input[i] = k;                                           
    }

    fht_window();                                                   
    fht_reorder();                                                 
    fht_run();                                                     
    fht_mag_lin();                                                 
    sei();


    fht_lin_out[0] = 0;
    fht_lin_out[1] = 0;

    for(unsigned char i = 0; i <= 6; i++){
      unsigned char maxW = 0;
        for(unsigned char x = grenzen[i]; x < grenzen[i+1];x++){
 
           if((unsigned char)fht_lin_out[x] > maxW){
            maxW = (unsigned char)fht_lin_out[x];
           }
        }

unsigned long currentMillis3=millis;
if (currentMillis3-previousMillis3>=1){
  hue++;
  previousMillis3=millis;
 
}
        setBalken(i, maxW);
          Serial.print(maxW);
      Serial.print(" ");
      Serial.println("");
   
       if(IR_idle()){     
    FastLED.show();
     }
}
}
    }
    void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
    uint8_t brightness = 255;
   
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;
    }
}



Go Up