Go Down

Topic: Constraining a Value with PPM Analog Input (Read 1 time) previous topic - next topic

QAMember

Hello, I am experiencing trouble with some code I have written:

#include <Servo.h>

Servo myservo;    //create servo object to control a servo

int analogPin = 0;
int val = 0;
int c = 0;
int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup() {
  Serial.begin(9600);
  for (int count=0;count<13;count++) {   
    pinMode(pinArray[count], OUTPUT);
  }
  pinMode(A0, INPUT);
 
  int eraser = 7;    //111
  TCCR0B &= ~eraser;    //sets timers to 000
  TCCR1B &= ~eraser;
  TCCR2B &= ~eraser;
  TCCR3B &= ~eraser;
  TCCR4B &= ~eraser;
  int prescaler = 1;    //1 being highest frequency
  TCCR0B |= prescaler;    //sets timers to 1
  TCCR1B |= prescaler;
  TCCR2B |= prescaler;
  TCCR3B |= prescaler;
  TCCR4B |= prescaler;
 
  myservo.attach(13);
  myservo.write(90); //half-throttle 
}


void loop() {
  float a = 128.0;
  float b = 128.0;
  float d = 2.0;
  float e = 18.3;    //offset
{
  val = analogRead(0);
  int c = val / 1023;    //percentage of input
  c = constrain(c, 8000, 800);

{
  int value = a + b * sin((millis()/c) * d * PI + (2 * e));
  analogWrite(3,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (3 * e));
  analogWrite(4,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (4 * e));
  analogWrite(5,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (5 * e));
  analogWrite(6,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (6 * e));
  analogWrite(7,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (7 * e));
  analogWrite(8,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (8 * e));
  analogWrite(9,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (9 * e));
  analogWrite(10,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (10 * e));
  analogWrite(11,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (11 * e));
  analogWrite(12,value);
}
}
}

The C value is used to control the wave frequency of 10 lights flashing. Pin 13 on the output is used to simulate the throttle output of a receiver, however I cannot figure out how to constrain the C value between a minimum and maximum. When the throttle toggle on my Tx is all the way down at "0", I need the analogInput to read 8000. When the Tx toggle is centered at "90", the analogInput should read halfway between 8000 and 800 [4400]. When the Tx toggle is all the way up at "180" servo angle/position, I need C value to be 800, and every linear value in between. How do I code to get the analogInput pin 0 to recognize the PPM range coming from the receiver? The servo attach was simply meant to simulate the operation before I actually connect the radio system to the Arduino. Thanks.

retrolefty

Quote
How do I code to get the analogInput pin 0 to recognize the PPM range coming from the receiver?


You can't use a analogRead() statement to read a PPM channel from a R/C receiver if that is what you are trying to do. The PPM signal is digital in nature and you have to determine the pulse width of the signal to gain the information it is sending. Usually using a pulseIn() command to a digital input pin is one method of obtaining the R/C channel information.

Lefty


Magician

http://playground.arduino.cc//Code/ReadReceiver     
more:
http://playground.arduino.cc//Main/InterfacingWithHardware#PPM

Normal servo has 1000-2000 usec pulse, if you need to re-scale range look at this function:

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

QAMember

Code: [Select]
#include <Servo.h>

Servo myservo;    //create servo object to control a servo

int analogPin = 0;
int val = 0;
int c = 0;
int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

void setup() {
  Serial.begin(9600);
  for (int count=0;count<13;count++) {   
    pinMode(pinArray[count], OUTPUT);
  }
  pinMode(A0, INPUT);
 
  int eraser = 7;    //111
  TCCR0B &= ~eraser;    //sets timers to 000
  TCCR1B &= ~eraser;
  TCCR2B &= ~eraser;
  TCCR3B &= ~eraser;
  TCCR4B &= ~eraser;
  int prescaler = 1;    //1 being highest frequency
  TCCR0B |= prescaler;    //sets timers to 1
  TCCR1B |= prescaler;
  TCCR2B |= prescaler;
  TCCR3B |= prescaler;
  TCCR4B |= prescaler;
 
  myservo.attach(13);
  myservo.write(90); //half-throttle 
}


void loop() {
  float a = 128.0;
  float b = 128.0;
  float d = 2.0;
  float e = 18.3;    //offset
{
  val = analogRead(0);
  int c = val / 1023;    //percentage of input
  c = constrain(c, 8000, 800);

{
  int value = a + b * sin((millis()/c) * d * PI + (2 * e));
  analogWrite(3,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (3 * e));
  analogWrite(4,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (4 * e));
  analogWrite(5,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (5 * e));
  analogWrite(6,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (6 * e));
  analogWrite(7,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (7 * e));
  analogWrite(8,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (8 * e));
  analogWrite(9,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (9 * e));
  analogWrite(10,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (10 * e));
  analogWrite(11,value);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (11 * e));
  analogWrite(12,value);
}
}
}

Go Up