Show Posts
Pages: [1] 2
1  Using Arduino / Sensors / Interfacing Rx & Arduino Mega 2560 To Control Offset LED Array Frequency on: April 09, 2013, 09:19:29 pm
Over the last several days, I have been encountering trouble interfacing my receiver to my Arduino Mega's analog input in order to control the sine wave function frequency of an offset array of 12 leds. First of all, I do not know how to interface the PPM output of the receiver's throttle pin to operate as a sensor at the analog input and generate values between 0 and 180, typical of the servo library. Secondly, the Arduino leds progressively enter glitch after uploading when attempting to assign the variable c = map(a,b,a1,a2). Now, I am purposed to believe that this code is requiring more processing than the board can handle, running the 12 PWM output pins at maximum modulation frequency and all. However, little change results when the 10 pins drop to standard 488 Hz and ~1000 Hz for the two other pins. I have mainly conducted these experiments with a 50k or 100k linear potentiometer, so I know what I am doing as far as the hardware is concerned. If anyone is gracious enough to help me out on this one, they are very welcome. Setting this up seemed basic at first thought, however the results have been shabby and extensively tested with fair results, though the PWM output pins still struggle maintaining the proper offset cadence over long periods. A possibly better idea of mine is to assign multiple frequency value settings, such as 20 discrete points along the overall frequency range, which may be activated when the PPM input is within:

servoRead(angle)

(0, 9) = 18000;
(10, 19) = 17000;
(20, 29) = 1600;

All the way to angle 180. I am not sure exactly how this would be coded, so help is greatly appreciated.

Code:
int c;
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
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);
  }
  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; 
}




void loop() {
  float a = 128.0;
  float b = 128.0;
  float d = 2.0;
  float e = 18.3;    //offset
 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and                    1023)
  val = map(val, 0, 1023, 0, 7200);     // Minimum = 800 || Maximum = 8000 || We can reverse the Tx throttle channel if needed
  val = c + 800;                  // sets the servo position according to the scaled value 


{
  int value = a + b * sin((millis()/c) * d * PI + (e));
  analogWrite(2,value);
}
{
  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);
}
{
  int value = a + b * sin((millis()/c) * d * PI + (12 * e));
  analogWrite(13,value);
}
}
2  Using Arduino / Sensors / Re: Constraining a Value with PPM Analog Input on: April 07, 2013, 07:59:32 pm
Code:
#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);
}
}
}
3  Using Arduino / Sensors / Re: Constraining a Value with PPM Analog Input on: April 07, 2013, 07:34:05 pm
How do I go about the code?
4  Using Arduino / Sensors / Constraining a Value with PPM Analog Input on: April 07, 2013, 04:45:00 pm
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.
5  Using Arduino / Audio / Re: Fully Rectified Sine Wave With PWM on: April 05, 2013, 01:24:31 pm
Semicolo, thank you, very much, for the code.
6  Using Arduino / Sensors / Re: Receiver Output to Control PWM Value on: April 05, 2013, 12:37:33 pm
I am using a 2560 Mega rev 3.
7  Using Arduino / Sensors / Receiver Output to Control PWM Value on: April 05, 2013, 07:30:10 am
I need the throttle pin of an R/C receiver to interface with the Arduino and constrain, I believe, between two values. The following code:

int pinArray[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int count = 0;
void setup() {
  Serial.begin(9600);
  for (count=0;count<13;count++) {
    pinMode(pinArray[count], OUTPUT);
  }
  pinMode(0, 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;
}

int x = analogRead(0) / 1023;    //percentage of input
int c = x * 10000 + 8000;    //range defined

void loop() {
  float a = 128.0;
  float b = 128.0;
  float c;
  float d = 2.0;
  float e = 18.3;    //offset

{
  int value = a - b * abs(sin(d*PI*millis()/c));
  analogWrite(2,value);

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

The analog input pin should be zero, while the PWM outputs are from 2 to 13. Float C is the variable value. Is this:
"
int x = analogRead(0) / 1023;    //percentage of input
int c = x * 10000 + 8000;    //range defined
"
-all that is required for one channel PPM control?
8  Using Arduino / Audio / Fully Rectified Sine Wave With PWM on: April 03, 2013, 08:41:24 am
I need to create a sine wave function through the PWM pins where the sine wave is pointy at the crests and 2 times wide at the bottom. I'm guessing a function which emulates the full rectification of a sine wave would be the way to go. Here is some of the current code I am using:

void loop() {
  float a = 128.0;
  float b = 128.0;
  float c = 28000.0;
  float d = 2.0;
  float e = 18.3;    //offset
{
  int value = a + b * sin((millis()/c) * d * PI );
  analogWrite(2,value);
}

9  Using Arduino / Programming Questions / Re: 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:45:38 pm
Specifically, how do I go about setting the initial function brightness in terms of phase degrees?
10  Using Arduino / Programming Questions / Re: 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:44:35 pm
Thank you, very much, PeterH. I will see what I can do. Any code structure examples of how to set this up would be highly gratified. Thanks, everyone.
11  Using Arduino / Programming Questions / Re: 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:41:34 pm
That base code represents the function for each pwm pin 3 through 13. There will be 10 of these function segments. Each "led" lights up and fades out in a sinusoidal fashion. All 10 pins must operate these functions simultaneously but in an offset 1/10 of each other. Thus, if the PWM function period, or trigonometric sine period is 1000 millis, then each successive pin after 3 should add a delay of 100 milliseconds. Get it?
12  Using Arduino / Programming Questions / Re: 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:30:26 pm
The board I am using is a 2560 Mega. As for the code, each 10 PWM pins run on the same individual code shown above.
13  Using Arduino / Programming Questions / Re: 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:28:00 pm
I am what you would call a newbie. Could you write up how this analogWrite() callback is supposed to be setup?
14  Using Arduino / Programming Questions / 10 Simultaneous PWM Signals in Offset on: March 18, 2013, 01:11:50 pm
I am currently having trouble figuring out how to run 10 leds simultaneously in 1/10 timing offset of each other using this individual sine function code base:

int Pin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, INPUT);
}

void loop()
{
  float something = millis()/1000.0;
  int value = 128.0 + 128 * sin( something * 2.0 * PI  );
  analogWrite(Pin,value);
}

- so as to generate a linear propagating/graduating sinusoidal wave flash with the 10 leds. First, I thought of using a specified delay with each led. The first led would have a delay of zero while each of the following 9 pins would be assigned a delay of 100 + n.
Pin() = Delay(t)
1 = 0
2 = 100
3 = 200
4 = 300
5 = 400
6 = 500
7 = 600
8 = 700
9 = 800
10 = 900

Of course, I learned that the delay command does not facilitate this setup as only one delay can be used at at time with nothing else running. Currently, I am toggling the Timer1 library with no success. May someone please assist me from here?
15  Using Arduino / Motors, Mechanics, and Power / Re: Continuous Rot Servo Sine Sweeping on: February 11, 2013, 01:27:51 pm
Zoomkat, if the Arduino is externally powered, the servo should work properly, correct?
Pages: [1] 2