Go Down

Topic: So I'm trying to read the PWM signal from a 4 ch rc receiver, but.... (Read 510 times) previous topic - next topic

RubenD056

I connect the receiver to arduino pins 3,4,5, and 6 respectively, this code is trying to replace the pulseIn function to a more efficient one. The values should be read with a +-50 microsecond accuracy and then printed onto the 16x2 LCD, the LCD code has been tested and works, well the code should explain itself, although it's kind of complicated.

Thanks in advance to anyone willing to help me!!! Appreciate it;

Here it is, I've spent about the hole day trying to do this and while it works well when I tested it with one input, it doesn't work well with the four inputs and gives me strange values as output in the LCD:



#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);


const int ch1Pin = 3, ch2Pin = 4, ch3Pin = 5, ch4Pin = 6;
int timer1=0,timer2=0,timer3=0,timer4=0;
int lVal1=LOW,lVal2=LOW,lVal3=LOW,lVal4=LOW;


void setup() {
  lcd.init();                     
  lcd.backlight();
 
  for (int i=3;i<=6;i++){pinMode(i,INPUT);}
 
 

}

void loop() {
  // put your main code here, to run repeatedly:
  int ch1,ch2,ch3,ch4,  lCh1,lCh2,lCh3,lCh4,  val1,val2,val3,val4;
 
  val1 = getReceiver(ch1Pin);
  val2 = getReceiver(ch2Pin);
  val3 = getReceiver(ch3Pin);
  val4 = getReceiver(ch4Pin);
 
 
  if (val1>0){ch1 = val1;}
  if (val2>0){ch2 = val1;}
  if (val3>0){ch3 = val1;}
  if (val4>0){ch4 = val1;}
 
 
  if (ch1!=lCh1&& ch2!=lCh2&& ch3!=lCh3&& ch4!=lCh4)
  {
      lcd.setCursor(0,0);
      lcd.print(ch1);
      lcd.setCursor(9,0);
      lcd.print(ch2);
      lcd.setCursor(0,1);
      lcd.print(ch3);
      lcd.setCursor(9,1);
      lcd.print(ch4);
      delay(100);
     
      lCh1 = ch1;
      lCh2 = ch2;
      lCh3 = ch3;
      lCh4 = ch4;
   
  }
 
}






int getReceiver (int channel)
{
 
  switch (channel){
    case 3:
     
      if (lVal1 == LOW && digitalRead(channel)==LOW){}
      else if (lVal1 == LOW && digitalRead(channel)==HIGH)
      {
        timer1=micros();
      }
      else if (lVal1 == HIGH && digitalRead(channel)==HIGH){}
      else if (lVal1 == HIGH && digitalRead(channel)==LOW)
      {
       int result1 = micros()-timer1;
       lVal1 = digitalRead(channel);
       return result1;
      }

      lVal1 = digitalRead(channel);
      return 0;
     
    case 4:

      if (lVal2 == LOW && digitalRead(channel)==LOW){}
      else if (lVal2 == LOW && digitalRead(channel)==HIGH)
      {
        timer2=micros();
      }
      else if (lVal2 == HIGH && digitalRead(channel)==HIGH){}
      else if (lVal2 == HIGH && digitalRead(channel)==LOW)
      {
       int result2 = micros()-timer2;
       lVal2 = digitalRead(channel);
       return result2;
      }

      lVal2 = digitalRead(channel);
      return 0;

    case 5:

      if (lVal3 == LOW && digitalRead(channel)==LOW){}
      else if (lVal3 == LOW && digitalRead(channel)==HIGH)
      {
        timer3=micros();
      }
      else if (lVal3 == HIGH && digitalRead(channel)==HIGH){}
      else if (lVal3 == HIGH && digitalRead(channel)==LOW)
      {
       int result3 = micros()-timer3;
       lVal3 = digitalRead(channel);
       return result3;
      }

      lVal3 = digitalRead(channel);
      return 0;

    case 6:

      if (lVal4 == LOW && digitalRead(channel)==LOW){}
      else if (lVal4 == LOW && digitalRead(channel)==HIGH)
      {
        timer4=micros();
      }
      else if (lVal4 == HIGH && digitalRead(channel)==HIGH){}
      else if (lVal4 == HIGH && digitalRead(channel)==LOW)
      {
       int result4 = micros()-timer4;
       lVal4 = digitalRead(channel);
       return result4;
      }

      lVal4 = digitalRead(channel);
      return 0;
  }
}


Go Up