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.
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);
}
}