I am a newbie here in the forums. I need help on a code to use pwm inputs from my Taranis radio to manipulate functions I have programmed.
I have searched around the net but all I found are connections what I am looking for are the codes to be used. Any help will be appreciated. Sorry if the questions is too basic I am a noob.
How you intercept these inputs might depend on how many of them you need to process. Why don't you describe the number of inputs, and the details of the PWM signal they put out. Otherwise, all anybody can give you is a generic explanation.
Can you please explain what you are trying to do ? Are you trying to take the outputs from an RC receiver and use them to control something or are you wanting to connect something directly to the Taranis transmitter, for instance ?
The project basically is plane installed with ws2812 equipped LED's I have used codes to make 5 LED "Dance patterns" which I want to manipulate remotely through my radio system. It will be controlled by an arduino mini and my taranis radio.
I particularly want to use my switches to change the LED patterns remotely.
OK, now we have a description of the requirement we can give help.
I used this program to control front and back LEDs on a quadcopter. The flash patterns are activated when the system is in a particular state and move on to the next state when the pin connected to a receiver output pin is toggled. The state of the input pin is determined using the pulseIn() function which is what you need to do. Obviously the LED patterns that you use are up to you.
#define ON HIGH
#define OFF LOW
#define DOUBLE_FLASH 0
#define ALL_OFF 1
#define LEFT_RIGHT 2
#define IN_OUT 3
#define ALL_ON 4
#define RANDOM 5
#define LEDS_ON_1 0
#define LEDS_OFF_1 1
#define LEDS_ON_2 2
#define LEDS_OFF_2 3
#define WAIT_1 1
byte receiverPin = 11;
unsigned long duration;
byte inputState = LOW;
byte prevInputState = LOW;
int pattern = 1;
unsigned long rearLedStateInterval;
unsigned long frontLedStateInterval;
int frontLedState = ON;
byte rearLedPins[] = {
A0, A1, A2, A3};
byte frontLedPins[] = {
8, 9};
void setup()
{
Serial.begin(115200);
for (int pin = 0; pin < 4; pin++)
{
pinMode(rearLedPins[pin], OUTPUT);
}
for (int pin = 0; pin < 2; pin++)
{
pinMode(frontLedPins[pin], OUTPUT);
}
delay(1000);
}
void loop()
{
duration = pulseIn(receiverPin, HIGH);
duration = map(duration, 1080, 1870, 0, 100);
if (duration > 50)
{
inputState = LOW;
}
else
{
inputState = HIGH;
}
if (inputState != prevInputState)
{
rearAll(OFF);
pattern++;
if (pattern > RANDOM)
{
pattern = DOUBLE_FLASH;
}
}
prevInputState = inputState;
switch (pattern)
{
case DOUBLE_FLASH:
{
static byte state = LEDS_ON_1;
switch(state)
{
case LEDS_ON_1: //all ON wait
rearAll(ON);
static unsigned long stateStart = millis();
rearLedStateInterval = 100;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_OFF_1;
stateStart = millis();
}
break; //end of LEDS_ON_1 state
case LEDS_OFF_1:
rearAll(OFF);
rearLedStateInterval = 200;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_2;
stateStart = millis();
}
break; //end of LEDS_OFF_1 state
case LEDS_ON_2:
rearAll(ON);
rearLedStateInterval = 100;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_OFF_2;
stateStart = millis();
}
break; //end of LEDS_ON_2 state
case LEDS_OFF_2: //all OFF wait longer after second flash
rearAll(OFF);
rearLedStateInterval = 500;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_1;
stateStart = millis();
}
break; //end of LEDS_OFF_2 state
}
break; //end of DOUBLE_FLASH
}
case ALL_OFF:
{
rearAll(OFF);
break;
}
case LEFT_RIGHT:
{
static byte state = LEDS_ON_1;
switch(state)
{
case LEDS_ON_1:
left(ON);
right(OFF);
static unsigned long stateStart = millis();
rearLedStateInterval = 300;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_2;
stateStart = millis();
}
break; //end of LEDS_ON_1 state
case LEDS_ON_2:
left(OFF);
right(ON);
rearLedStateInterval = 300;
if (millis() - stateStart >= rearLedStateInterval)
{
state = LEDS_ON_1;
stateStart = millis();
}
break; //end of LEDS_ON_2 state
}
break; //end of LEFT/RIGHT states
}
case IN_OUT:
{
static byte rearLedState = LEDS_ON_1;
switch(rearLedState)
{
case LEDS_ON_1:
outers(ON);
inners(OFF);
static unsigned long rearLedStateStart = millis();
rearLedStateInterval = 300;
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_2;
rearLedStateStart = millis();
}
break; //end of LEDS_ON_1 rearLedState
case LEDS_ON_2:
outers(OFF);
inners(ON);
rearLedStateInterval = 300;
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_1;
rearLedStateStart = millis();
}
break; //end of LEDS_ON_2 rearLedState
} //end of IN/OUT rearLedStates
break; //end of IN/OUT case
}
case ALL_ON:
{
rearAll(ON);
break;
}
case RANDOM:
{
static byte rearLedState = LEDS_ON_1;
switch(rearLedState)
{
case LEDS_ON_1:
digitalWrite(rearLedPins[random(0, 4)], random(0, 2));
static unsigned long rearLedStateStart = millis();
rearLedState = WAIT_1;
rearLedStateInterval = 50;
break;
case WAIT_1:
if (millis() - rearLedStateStart >= rearLedStateInterval)
{
rearLedState = LEDS_ON_1;
rearLedStateStart = millis();
}
break;
} //end of RANDOM rearLedStates
break; //end of RANDOM case
}
} //end of rear leds patterns switch
switch (frontLedState)
{
case ON:
front(ON);
frontLedStateInterval = 80;
static unsigned long frontLedStateStart = millis();
if (millis() - frontLedStateStart >= frontLedStateInterval)
{
frontLedState = OFF;
frontLedStateStart = millis();
}
break; //end of front leds on
case OFF:
front(OFF);
frontLedStateInterval = 500;
if (millis() - frontLedStateStart >= frontLedStateInterval)
{
frontLedState = ON;
frontLedStateStart = millis();
}
break; //end of front leds off
} //end of front leds switch
} //end of loop()
void rearAll(byte ledState)
{
outers(ledState);
inners(ledState);
}
void left(byte ledState)
{
digitalWrite(rearLedPins[2], ledState);
digitalWrite(rearLedPins[3], ledState);
}
void right(byte ledState)
{
digitalWrite(rearLedPins[0], ledState);
digitalWrite(rearLedPins[1], ledState);
}
void inners(byte ledState)
{
digitalWrite(rearLedPins[1], ledState);
digitalWrite(rearLedPins[2], ledState);
}
void outers(byte ledState)
{
digitalWrite(rearLedPins[0], ledState);
digitalWrite(rearLedPins[3], ledState);
}
void front(byte ledState)
{
digitalWrite(frontLedPins[0], ledState);
digitalWrite(frontLedPins[1], ledState);
}
I wanted to expand the range to make the different timing regions easier to select but it was not strictly necessary as I could have used the raw data and selected on that.