Pages: [1]   Go Down
Author Topic: PWM inputs from Rx for Atmega 328  (Read 518 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

good evening everyone

I've spent a few hours researching the Arduino programming language and I'm beginning to get a handle on it however I'm a bit stumped on a basic input concept.  I'm attempting to use an Atmega 328 based board to sense switch positions from an RC Tx (via the Rx/channel outputs) so as to control the illumination patterns of LED lights.  Ideally I'd like to have two PWM inputs (two channels from the Rx into the 328) controlling up to four strings of LED's.  I'm using a jdrones ioBoard for the project.

My basic question is do I have to utilize a routine like ServoDriver2 to have the 328 be able to interpret the PWM signal from the Rx?  The thread I've found on ServoDriver2 is from 2008, does that matter? 

The condition will a simple test of three possible values (low, middle, high) to correspond to a three position switch.  I understand the concept of calculating the pulse width using start and end times but am i at least looking in the right place to get this sketch started?

thanks much, looking forward to your responses.

Bart
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16520
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well in context of the arduino world, PWM means outputs from the board. As you are trying to read the value of a PWM signal(s) from a R/C receiver you are needing an input function to calculate the value of the two receiver servo channels.

 There are 3rd party library codes around designed to do this for you, but assuming you might want to give it a shot on your own you could simply wire the two receiver servo channels you are interested in to two arduino digital input pins and then use the PulseIn() command to read the pulse width of each pin.

http://arduino.cc/en/Reference/PulseIn

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lefty,
thanks for the quick reply.  i've been going through the reference library all morning and haven't gotten to that folder but at first glance it looks like it will be good enough for me to measure the pulse width so as to use it in the control process of the LED outputs.

I appreciate the help.
Bart
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16520
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lefty,
thanks for the quick reply.  i've been going through the reference library all morning and haven't gotten to that folder but at first glance it looks like it will be good enough for me to measure the pulse width so as to use it in the control process of the LED outputs.

I appreciate the help.
Bart

Your welcome and be sure to share your results when you get it all working as you wish, that is the "Arduino way".  smiley-wink

Lefty
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's where it's at.  When compiling it's getting hung up a the pulseIn command, go figure.



/*
 LED control using jDrones ioBoard and inputs from
 RC receiver (PWM)
 PWM signal pulses are measured using 'PulseIn' statement
 Conditional statement looks at Pulse value and determines
 illumination pattern of LED outputs
 illumination to be alternating left/right indicates manual control
kinda-sorta heartbeat indicates GPS position hold
 rapid flashing indicates come-home mode active

 Created 26 March, 2012
 By Bart Cocchiola with help from the Arduino community. smiley


 
 */


void setup() {
  // pin numbers for inputs and outputs
  int rxgpsmode = 12;     // digital input pin using GPS mode channel from Rx
  int ledPin1 = 02;       // pin for left LED output, PWM enabled
  int ledPin2 = 03;       // pin for right LED output, PWM enabled
  unsigned int duration;
  int ledCounter;     // counter variable used to loop LED patterns
  int ledBrightness;  // brightness variable for heartbeat pattern
  int fadeAmount = 25;    // factor used to build light intensity in Heartbeat pattern
  pinMode(rxgpsmode, INPUT);  // initialize Rx connection pin as in INPUT
  pinMode(ledPin1, OUTPUT);   // initialize the LED pins as outputs
  pinMode(ledPin2, OUTPUT);
}

void loop()
{
  duration = pulseIn(rxgpsmode, HIGH)    // read the value of the incoming pulse length

  if (duration < 100){
  // alternating arms flash slowly----->GPS off
  digitalWrite(ledpin1, HIGH);   // turn the ledpin1 ON (HIGH is the voltage level)
  delay(1000);                   // wait for a second
  digitalWrite(ledpin1, LOW);    // turn the ledpin1 OFF by making the voltage LOW
  delay(100);                    // brief delay, to make blinking appear uniform throughout subroutine
  digitalWrite(ledpin2, HIGH);   // turn ledpin2 ON
  delay(1000);                   // wait for a second
  digitalWrite(ledpin2, LOW);    // turn ledpin2 OFF
}

else if (duration > 1200)

{
  // LEDs flash quickly three times then exit loop to recheck conditional
  // GPS come home mode active
  //
  for (int ledcounter = 0; ledCounter < 3; ledCounter = ledCounter +1) {
  ledpin2 = ledpin1;             // ledpin2 set equal to ledpin1 to coordinate illumination pattern
  digitalWrite(ledpin1, HIGH);   // both LED pins turn on
  delay(300);                    // wait 1/3 second
  digitalWrite(ledpin1, LOW);    // both LED pins turn off
  delay(100);                    // wait 1/3 second          
}
}

else

{
  // heartbeat pattern, GPS PH active
  ledpin2 == ledpin1;
  for (int ledBrightness = 0; ledBrightness <= 250; ledBrightness = ledBrightness + fadeAmount) {
  analogWrite(ledpin1, ledBRightness);
}
  analogWrite(ledpin1, 250);
  delay(250);
  digitalWrite(ledpin1, LOW);
  delay(250);
  digitalWrite(ledpin1, HIGH);
  delay(500);
  digitalwrite(ledpin1, LOW);
}
« Last Edit: March 26, 2013, 08:33:12 pm by barchiola » Logged

Pages: [1]   Go Up
Jump to: