Connecting LEDs and stuff

Post Reply
stoopkid
Posts: 6
Joined: Sat Mar 01, 2014 10:38 pm

Connecting LEDs and stuff

Post by stoopkid »

Is there anything special I need to do to, say for instance, have blinking LED's in my code? Could I simply add a "blink without delay" bit to the loop function and analogWrite to an unused pin? My goal is to use one of the AUX channels to dim some leds, while others would blink periodically. Is there anything I need to do to prevent conflict when using a PWM pin. And aside from making sure I don't make my code take too long, is there anything wrong with throwing stuff into the loop function?

I realize that LEDs might take more current than my ESCs can handle and more than the 328 can handle so I would switch them with an NPN and use a voltage regulator from my batter to source the current.

edit: I went from 2.1 to 2.3 and now the main sketch seems to be empy.

User avatar
Hamburger
Posts: 2582
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Re: Connecting LEDs and stuff

Post by Hamburger »

Just make sure your code is non-blocking.
Maybe better to have a look at the alarm code. It already does roughly what you want.

QuadBow
Posts: 532
Joined: Fri Jan 04, 2013 10:06 am

Re: Connecting LEDs and stuff

Post by QuadBow »

I have implemented the control of 3 LEDs in the alarm code handling section.

The blue LED indicates the status of the gps device.
The green one the mode (acro/horizon/angle) and
the red one is designated to error code handling (low voltage).

In function alarmPatternComposer of the file alarm.cpp:

Code: Select all

  
#if defined(LED_MONITOR)
    resource = 5;
    if (alarmArray[9] == 1)        setTiming(resource,100,100);                                    //all very fast blink -->I2C Error
    #if defined(FAILSAFE)
      else if (alarmArray[1] == 2) patternDecode(resource,100,100,100,100,1000);                   //all triple blink --> failsafe level2
      else if (alarmArray[1] == 1) patternDecode(resource,100,100,0,100,1000);                     //all double blink --> failsafe level1
    #endif
    else { 
      resource = 2;
      if (f.ARMED) {
         if (!f.ACC_CALIBRATED)    patternDecode(resource,100,100,100,100,1000);                   //green triple blink --> multiwii not stable or uncalibrated
         else if (f.ANGLE_MODE)    turnOn(resource);                                               //green static --> angle
         else if (f.HORIZON_MODE)  patternDecode(resource,200,200,0,100,1000);                     //green slow blink --> horizon
         else                      patternDecode(resource,100,100,0,100,1000);                     //green fast blink --> acro
         }
      else                         turnOff(resource);                                              //not armed --> switch off
      resource = 3;
      #if GPS
        if (alarmArray[2] == 1)    patternDecode(resource,100,100,100,100,1000);                   //blue triple blink --> no gps fix
        else if (f.GPS_HOLD_MODE)  patternDecode(resource,200,200,0,100,1000);                     //blue slow blink --> gps hold position
        else if (f.GPS_HOME_MODE)  patternDecode(resource,100,100,0,100,1000);                     //blue fast blink --> gps return to home
        else                       turnOn(resource);                                               //permanent blue --> gps fix ok
      #else
                                   turnOn(resource);
      #endif   
      resource = 4;
      #if defined(VBAT)
        if (alarmArray[6] == 4)    setTiming(resource,100,100);                                    //red very fast blink --> low bat warning2
        else if (alarmArray[6]==2) patternDecode(resource,100,100,100,100,1000);                   //red triple blink --> low bat warning1
        else
        #if defined(POWERMETER)
             if (alarmArray[4]==1) patternDecode(resource,200,200,0,100,1000);                     //red slow blink --> powermeter warning
             else                  turnOn(resource);
        #else
                                   turnOn(resource);
        #endif
      #endif
    }
  #endif   


New or modified code:

Code: Select all

 
void turnOn(uint8_t resource){
  if (resource == 1) {
    if (!resourceIsOn[1]) {
      BUZZERPIN_ON;
      resourceIsOn[1] = 1;
    }
  }else if (resource == 0) {
    if (!resourceIsOn[0]) {
      resourceIsOn[0] = 1;
      LEDPIN_ON;
    }
  }else if (resource == 2) {
    if (!resourceIsOn[2]) {
      resourceIsOn[2] = 1;
      #if defined (PILOTLAMP)
        PilotLamp(PL_GRN_ON);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_GREEN_ON;
   #endif
  }else if (resource == 3) {
    if (!resourceIsOn[3]) {
      resourceIsOn[3] = 1;
      #if defined (PILOTLAMP)
        PilotLamp(PL_BLU_ON);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_BLUE_ON;
   #endif
  }else if (resource == 4) {
    if (!resourceIsOn[4]) {
      resourceIsOn[4] = 1;
      #if defined (PILOTLAMP)
        PilotLamp(PL_RED_ON);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_RED_ON;
   #endif
  }else if (resource == 5) {
    if (!resourceIsOn[5]) {
      resourceIsOn[5] = 1;
    }
   #if defined (LED_MONITOR)
     LED_ALL_ON;
   #endif
  }
}

void turnOff(uint8_t resource){
  if (resource == 1) {
    if (resourceIsOn[1]) {
      BUZZERPIN_OFF;
      resourceIsOn[1] = 0;
    }
  }else if (resource == 0) {
    if (resourceIsOn[0]) {
      resourceIsOn[0] = 0;
      LEDPIN_OFF;
    }
  }else if (resource == 2) {
    if (resourceIsOn[2]) {
      resourceIsOn[2] = 0;
      #if defined (PILOTLAMP)
        PilotLamp(PL_GRN_OFF);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_GREEN_OFF;
   #endif
  }else if (resource == 3) {
    if (resourceIsOn[3]) {
      resourceIsOn[3] = 0;
      #if defined (PILOTLAMP)
        PilotLamp(PL_BLU_OFF);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_BLUE_OFF;
   #endif
  }else if (resource == 4) {
    if (resourceIsOn[4]) {
      resourceIsOn[4] = 0;
      #if defined (PILOTLAMP)
        PilotLamp(PL_RED_OFF);
      #endif
    }
   #if defined (LED_MONITOR)
     LED_RED_OFF;
   #endif
  }else if (resource == 5) {
    if (resourceIsOn[5]) {
      resourceIsOn[5] = 0;
    }
   #if defined (LED_MONITOR)
     LED_ALL_OFF;
   #endif
  }
}

 void toggleResource(uint8_t resource, uint8_t activate){
     switch(resource) {     
        #if defined (BUZZER)   
          case 1:
            if (activate == 1) {BUZZERPIN_ON;}
            else BUZZERPIN_OFF;
            break;
        #endif
        #if defined (PILOTLAMP)
          case 2:
            if (activate == 1) PilotLamp(PL_GRN_ON);
            else PilotLamp(PL_GRN_OFF);
            break;
          case 3:
            if (activate == 1) PilotLamp(PL_BLU_ON);
            else PilotLamp(PL_BLU_OFF);
            break;
          case 4:
            if (activate == 1) PilotLamp(PL_RED_ON);
            else PilotLamp(PL_RED_OFF);
            break;
        #endif
         #if defined (LED_MONITOR)
          case 2:
            if (activate == 1) {LED_GREEN_ON;}
            else {LED_GREEN_OFF;}
            break;
          case 3:
            if (activate == 1) {LED_BLUE_ON;}
            else {LED_BLUE_OFF;}
            break;
          case 4:
            if (activate == 1) {LED_RED_ON;}
            else {LED_RED_OFF;}
            break;
          case 5:
            if (activate == 1) {LED_ALL_ON;}
            else {LED_ALL_OFF;}
            break;
        #endif
       case 0:
        default:
          if (activate == 1) {LEDPIN_ON;}
          else LEDPIN_OFF;
          break;
      }
      return;
  }


3 pins are required which need a mega, the related definitions have been changed in def.h

Code: Select all

#if defined(MEGA)
  #if defined(LED_MONITOR)
    #undef GPS_LED_INDICATOR
    #undef PILOTLAMP
    #define LEDPIN_PINMODE             pinMode (13, OUTPUT);
    #define LEDPIN_TOGGLE              PINB  |= (1 << 7);
    #define LEDPIN_ON                  PORTB |= (1 << 7);
    #define LEDPIN_OFF                 PORTB &= ~(1 << 7);
    #define LED_GREEN_PINMODE          pinMode (30, OUTPUT);
    #define LED_BLUE_PINMODE           pinMode (31, OUTPUT);
    #define LED_RED_PINMODE            pinMode (33, OUTPUT);
    #if defined(LED_MON_REV)
      #define LED_GREEN_ON               PORTC &= ~(1 << 7);
      #define LED_GREEN_OFF              PORTC |= (1 << 7);
      #define LED_BLUE_ON                PORTC &= ~(1 << 6);
      #define LED_BLUE_OFF               PORTC |= (1 << 6);
      #define LED_RED_ON                 PORTC &= ~(1 << 4);
      #define LED_RED_OFF                PORTC |= (1 << 4);
      #define LED_ALL_ON                 PORTC &= ~(1 << 4); PORTC &= ~(1 << 6); PORTC &= ~(1 << 7);
      #define LED_ALL_OFF                PORTC |= (1 << 4);  PORTC |= (1 << 6);  PORTC |= (1 << 7);
    #else
      #define LED_GREEN_ON               PORTC |= (1 << 7);
      #define LED_GREEN_OFF              PORTC &= ~(1 << 7);
      #define LED_BLUE_ON                PORTC |= (1 << 6);
      #define LED_BLUE_OFF               PORTC &= ~(1 << 6);
      #define LED_RED_ON                 PORTC |= (1 << 4);
      #define LED_RED_OFF                PORTC &= ~(1 << 4);
      #define LED_ALL_ON                 PORTC |= (1 << 4);  PORTC |= (1 << 6);  PORTC |= (1 << 7);
      #define LED_ALL_OFF                PORTC &= ~(1 << 4); PORTC &= ~(1 << 6); PORTC &= ~(1 << 7);
    #endif
  #else
    #define LEDPIN_PINMODE             pinMode (13, OUTPUT);pinMode (30, OUTPUT);
    #define LEDPIN_TOGGLE              PINB  |= (1<<7); PINC  |= (1<<7);
    #define LEDPIN_ON                  PORTB |= (1<<7); PORTC |= (1<<7);
    #define LEDPIN_OFF                 PORTB &= ~(1<<7);PORTC &= ~(1<<7);
    #define STABLEPIN_PINMODE          pinMode (31, OUTPUT);
    #define STABLEPIN_ON               PORTC |= (1 << 6);
    #define STABLEPIN_OFF              PORTC &= ~(1 << 6);
  #endif
  #define BUZZERPIN_PINMODE            pinMode (32, OUTPUT);
  #if defined PILOTLAMP
    #define    PL_PIN_ON               PORTC |= 1<<5;
    #define    PL_PIN_OFF              PORTC &= ~(1<<5);
  #else
    #define BUZZERPIN_ON               PORTC |= 1<<5;
    #define BUZZERPIN_OFF              PORTC &= ~(1<<5);
  #endif


Maybe that is too much for the purpose, but, it could act as an example how to implement the desired non-blocking function.

sgomare
Posts: 1
Joined: Mon Oct 13, 2014 11:34 am

Re: Connecting LEDs and stuff

Post by sgomare »

..
Last edited by sgomare on Sat Oct 25, 2014 1:35 pm, edited 1 time in total.

jeana1gilhousen
Posts: 1
Joined: Fri Oct 17, 2014 4:49 am

Re: Connecting LEDs and stuff

Post by jeana1gilhousen »

and may there is also an another connection between me and my choicing cheap blonde wig,this is different about yours.

hbelanger
Posts: 1
Joined: Fri Sep 13, 2013 12:22 am

Re: Connecting LEDs and stuff

Post by hbelanger »

Hi all
I'm in the middle of a conversion of a Walkera QR-X350 to Crius AIO and multiwii 2.3. I want to make use of the two rear external LED I've already figured out how to get the F/C Status to pin D10, but how do I get GPS Status to let's say D11 ???

Thanks in advance and keep up the good work

Post Reply