Im so lost, First time with Arduino

Hello,

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

nvxwax:
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.

You have most of the answer right there. Note how the tests start at the top and work down.

if (pwm > 1700) {
   digitalWrite(13, HIGH);
   digitalWrite(12, LOW);
}
else if (pwm > 1300) {
   digitalWrite(13, LOW);
   digitalWrite(12, LOW);
}
else {
   digitalWrite(13, LOW);
   digitalWrite(12, HIGH);
}

...R

Thanks

I see what i had wrong, I also had to change rcPin to ch1 to get it to look at the correct value

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.

(ch1 >= 1490 && ch1 <= 1510)

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 :wink: ).

Another approach is to use hysteresis. Here is a thread that discusses it and has some sample code: http://forum.arduino.cc/index.php?topic=190305.5

tylernt:
(though with probably not quite so many as 1,000 samples :wink: ).

I think in that version I was looking to see if it made a huge difference over 10 or 100, and wondering how long it took to pop out with a value.

Yeah, 10-20 I guess would do the trick.