Im sure this is a simple project but i cant get it to do what i want.
Im taking the signal (PWM) from an RC radio receiver and want it to control two other pins (LED Lights).
I got some code that gets the PWM from pin 10 and lets you see the the result in the serial Monitor, The values go from 1000 (left), 1500 (center), 2000 (right).
I need to turn pin 12 HIGH when the PWM is <1300 and Pin 13 HIGH when the PWM is >1700, between 1301 -1699 i need pins 12 and 13 LOW.
ANybody got any ideas how to get this working? Thanks
int duration;
int rcPin = 10; // PWM signal arduino pin
int ch1 = 0; // Receiver channel 1 pwm value
void setup() {
pinMode(rcPin, INPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read in the length of the signal in microseconds
ch1 = pulseIn(rcPin, HIGH, 25000);
Serial.print("Channel #1: ");
Serial.println(ch1);
if (rcPin < 1300)
{
digitalWrite(12, HIGH);
}
else if (rcPin > 1700)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
}
Ok i dot my sketch working but have another question,
Is there anyway to average the result of pulsein so it does not swing so much and fast? I have the argument below controlling a LED, It samples so fast it makes the LED blink very fast because the pulsein signal wanders out of that range (i need a tight range). I'm using for centering a servo.
nvxwax:
Is there anyway to average the result of pulsein
This snippet does an average of 1000 temperature readings. I use a for to go 1000 times, each time adding the new reading to a running total. Then when the for is done, divide by 1000
You can do the same.....
// read and calc the temp
temptotal=0;
for(int x = 0; x < 1000; x++) {
rawVal=analogRead(5);//Connect LM35 on Analog 5
temp=(500 * rawVal) /1024;
temptotal = temptotal + temp;
}
temp=temptotal/1000;
Serial.println(temp);
Jimbo's averaging solution is completely valid, and entirely appropriate for this application (though with probably not quite so many as 1,000 samples ).