LeoStick and RC receiver controlling LED working

A shrunk down Leonardo-compatible board, thumb drive sized with native USB support. [Product info]
Post Reply
dtbaker
Posts: 26
Joined: Sat Jun 02, 2012 12:52 pm

LeoStick and RC receiver controlling LED working

Post by dtbaker » Sun Dec 02, 2012 1:57 pm

Hey guys,

Just unpacked my LeoStick and got it up and running in Ubuntu rather easily. Still displays some random errors upon upload but the sketch uploads and works fine so I'm not too fussed.

I connected the LeoStick up to a spare channel on my RC receiver, and with a bit of code I can control the onboard RGB LED using my RC transmitter.

The code is very simple. It does a pulseIn() check on my receiver channel. The value of this is just below 16900 when the RC knob is all the way left, and the value is just above 17500 when the knob is all the way to the right. The code splits the knob up into 8 positions: MIN, 1, 2, 3, 4, 5, 6 and MAX. The LeoStick can perform different operations depending on the position of the knob. At the moment it just sets different RGB combinations. Here's the code:

Code: Select all

#define CHANNEL 6
#define CHANNELMIN 16900
#define CHANNELMAX 17500
#define MODECOUNT 6

#define LEDPINR 13 // RED
#define LEDPING 9 // GREEN
#define LEDPINB 10 // BLUE

int modewidth = (CHANNELMAX - CHANNELMIN) / MODECOUNT;
int currentmode = 0;

void setup() {
  Serial.begin(9600);
  pinMode(CHANNEL, INPUT);     //set the pin to input
  digitalWrite(CHANNEL, HIGH); //
  pinMode(LEDPINR, OUTPUT); 
  pinMode(LEDPING, OUTPUT); 
  pinMode(LEDPINB, OUTPUT); 
  Serial.println("Starting");
}

void loop() {
  int i = pulseIn(CHANNEL, LOW);
  if(i<=CHANNELMIN){
    // off
    //Serial.println("Min");
    currentmode = 0;
  }else if(i>=CHANNELMAX){
    // all way on.
    //Serial.println("Max");
    currentmode = MODECOUNT+1;
  }else{
    // one of the "modes" in between.
    currentmode = ((i - CHANNELMIN) / modewidth)+1;
  }
  Serial.println(currentmode);
  switch(currentmode){
    case 0:
      digitalWrite(LEDPINR, LOW); //
      digitalWrite(LEDPING, LOW); //
      digitalWrite(LEDPINB, LOW); //
    break;
    case 1:
      digitalWrite(LEDPINR, HIGH); //
      digitalWrite(LEDPING, LOW); //
      digitalWrite(LEDPINB, LOW); //
    break;
    case 2:
      digitalWrite(LEDPINR, LOW); //
      digitalWrite(LEDPING, HIGH); //
      digitalWrite(LEDPINB, LOW); //
    break;
    case 3:
      digitalWrite(LEDPINR, LOW); //
      digitalWrite(LEDPING, LOW); //
      digitalWrite(LEDPINB, HIGH); //
    break;
    case 4:
      digitalWrite(LEDPINR, HIGH); //
      digitalWrite(LEDPING, HIGH); //
      digitalWrite(LEDPINB, LOW); //
    break;
    case 5:
      digitalWrite(LEDPINR, HIGH); //
      digitalWrite(LEDPING, LOW); //
      digitalWrite(LEDPINB, HIGH); //
    break;
    case 6:
      digitalWrite(LEDPINR, LOW); //
      digitalWrite(LEDPING, HIGH); //
      digitalWrite(LEDPINB, HIGH); //
    break;
    case 7:
      digitalWrite(LEDPINR, HIGH); //
      digitalWrite(LEDPING, HIGH); //
      digitalWrite(LEDPINB, HIGH); //
    break;
  }
  
}
Here's the video showing it in action:

http://www.youtube.com/watch?v=IWf3IBD2c-4

Hopefully I figure out how to get this powered up on my hexacopter shortly and can control some LED lights via a relay using my transmitter.

Image

yay!

Post Reply