View Single Post
  #2  
Old 10-13-2018, 11:20 AM
Wombii's Avatar
Wombii Wombii is offline
Green Horn
 
Join Date: Feb 2018
Location: Norway
Posts: 231
Wombii is on a distinguished road
Default Re: Remote LED switching with Arduino examples.

Example 3: If you've made it here, please don't be intimidated. This is the same as example 2, but with 2 extra functions. 1 is a simple on/off. 2 is a simple flash/off. Function 3 is the fun one with a 3 position switch.
Position 1: marker.
Position 2: marker + headlights.
Position 3: marker + wig wag flashing headlights.

Hopefully with some tinkering, you can see how these functions can form the basics of a light kit.

Here you need to connect 3 channels from the receiver to the Arduino pins A0, A1, A2. You'll also need LEDs connected from pins 2, 3, 4, 5, 6 with appropriate resistors to GND. 1000 ohm should work fine for bright blues without blinding you or smoking anything.
You can also skip the LEDs and just watch the Serial monitor output.

Note that this setup is only for testing. If you connect anything else to this circuit like a servo, more LEDs or power the receiver from a battery or BEC, make sure you understand how much power goes where, and remove the wire from receiver to the +5v pin on the Arduino when connecting it to USB.

Code:
//Wombii 2018
/*
read channels
simple switching
simple flash timing
output
*/


/***********************************************************************
* If you're comfortable with arrays and for loops:
*   Replace the individual assignments of pin names with an array:

const byte LEDpin[6] = {2,3,4,5,6,7};

*   Replace LEDpin state variables with an array:

byte LEDstate[6] = {0,0,0,0,0,0};

*   Use a for loop to set pinModes:

for(int i=0; i<sizeof(LEDpin) ; i++)
{
    pinMode(LEDstate[i],OUTPUT);
}

*   Use a for loop to switch I/O pins:

for(int i=0; i<sizeof(LEDstate) ; i++)
{
    digitalWrite(LEDpin[i],LEDstate[i]);
}

*   Use array position to read or write a variable:

LEDstate[2] = 1; instead of LEDpin2State = 1:

***********************************************************************/


//**map your Arduino output pins to alias names:**
// This is completely optional, but makes the program easier to read for humans.
// I'm using generic terms like LEDpin1 here, but you could replace that with specifics like headlight or turnSignal.
// Just remember to use the same name in the whole program, and you should probably rename the variables too.
//
// Pins available on most Arduinos. "*" means the pin can be used as a PWM pin for dimming LEDs. A0-A5 can also be called 14-19.
// RX  TX  02  03* 04  05* 06* 07 08  09* 10* 11* 12  13 A0  A1  A2  A3  A4  A5

const byte LEDpin1 = 2;
const byte LEDpin2 = 3;
const byte LEDpin3 = 4;
const byte LEDpin4 = 5;
const byte LEDpin5 = 6;
const byte LEDpin6 = 7;

// Radio pin aliases:
const byte channel1Pin = A0;
const byte channel2Pin = A1;
const byte channel3Pin = A2;



//**set up variables:**
//This is not optional. Variables are little boxes to store numbers in.

//Radio channel variables:
int channel1Value = 1500; //1.5ms (center) as a default value.
int channel2Value = 1500;
int channel3Value = 1500;

//Light function switch flags:
byte lightFunction1Switch = 0; //off as default value.
byte lightFunction2Switch = 0;
byte lightFunction3Switch = 0;

//Make variables for LEDpins:
byte LEDpin1State = 0; //defaults to off
byte LEDpin2State = 0; //defaults to off
byte LEDpin3State = 0; //defaults to off
byte LEDpin4State = 0; //defaults to off
byte LEDpin5State = 0; //defaults to off
byte LEDpin6State = 0; //defaults to off







//This runs once at power on.
void setup()
{
    //set LEDpins to switch mode HIGH/LOW
    pinMode(LEDpin1,OUTPUT);
    pinMode(LEDpin2,OUTPUT);
    pinMode(LEDpin3,OUTPUT);
    pinMode(LEDpin4,OUTPUT);
    pinMode(LEDpin5,OUTPUT);
    pinMode(LEDpin6,OUTPUT);
    
    //all pins default to LOW, set HIGH if necessary:
    digitalWrite(LEDpin1,LEDpin1State);
    digitalWrite(LEDpin2,LEDpin2State);
    digitalWrite(LEDpin3,LEDpin3State);
    digitalWrite(LEDpin4,LEDpin4State);
    digitalWrite(LEDpin5,LEDpin5State);
    digitalWrite(LEDpin6,LEDpin6State);

    //Start the serial port to enable serial communication with a pc.
    Serial.begin(115200);

}

//This keeps looping.
void loop()
{

    // --- Read channels --- 

    //This is the easiest way to read channels, but it's slow and prone to failure for more than 2 channels.
    //(One loop will take minimum 10ms, possibly 60+ms with 3 channels depending on receiver and the order you plug the channels in.)
        channel1Value = pulseIn(channel1Pin,HIGH);
        channel2Value = pulseIn(channel2Pin,HIGH);
        channel3Value = pulseIn(channel3Pin,HIGH);

        //Send info to a pc to test the receiver connection. Open "serial plotter" in the Arduino IDE and select 115200 as baud rate.
        //The received channel pulse lengths should show up in a nice chart.

        Serial.print(channel1Value);
        Serial.print('\t');
        Serial.print(channel2Value);
        Serial.print('\t');
        Serial.print(channel3Value);
        Serial.print('\t');

    // --- Convert channel values to logical switches --- 
    // Channel values should come in as a value in the range of 800-2200 with center value of 1500 microseconds.
    // This part could be skipped, but makes it easier to troubleshoot.

    //Channel1 with 2 positions
        if (channel1Value < 1300)
        {
            //The switch is off
            lightFunction1Switch = 0;
        }
        else
        {
            //The switch is on
            lightFunction1Switch = 1;
        }

    //Channel2 with 2 positions
        if (channel2Value < 1300)
        {
            //The switch is off
            lightFunction2Switch = 0;
        }
        else
        {
            //The switch is on
            lightFunction2Switch = 1;
        }
    
    //Channel3 with 3 positions: ( <1300, 1300-1700, 1700> )
        if (channel3Value < 1300)
        {
            //The switch is bottom
            lightFunction3Switch = 0;
        }
        else if (channel3Value > 1700)
        {
            //The switch is top
            lightFunction3Switch = 2;
        }
        else
        {
            //The switch is middle
            lightFunction3Switch = 1;
        }

    


    // --- Logic stuff for the switches. ---
    // Note that this part doesn't actually write to the ports, but determines what should be written to the ports.

        //Light function 1 is a simple on/off switch.
        if (lightFunction1Switch == 1)
        {
            //Turn on LEDpin 1 and 2
            LEDpin1State = 1;
            LEDpin2State = 1;
        }
        else
        {
            //Turn off LEDpin 1 and 2
            LEDpin1State = 0;
            LEDpin2State = 0;
        }


        //Light function 2 is a simple on/off switch for flasher.

        static unsigned long LEDpin3Time = 0; //Make a variable to store timing value for pin 3. 

        if (lightFunction2Switch == 1) //if switch is on
        {
            //Make light function 2 flash LEDpin3 on/off
            if (millis() - LEDpin3Time > 200) //If [current time] - [last time pin1 switched] is more than 200 ms
            {
                LEDpin3State = !LEDpin3State; //! means NOT or opposite. Set state LOW if it's HIGH and vice versa.
                LEDpin3Time = millis(); //store [current time] as [last time pin1 switched].
            }

        }
        else //if switch is off
        {
            //Turn LEDpin3 off
            LEDpin3State = 0;
        }


        //Light function 3: 0 = marker on, headlight off / 1 = marker + headlights on / 2 = marker + wig wag flash headlights
        
        static unsigned long LEDpin5Time = 0; //Make a variable to store timing value for pin 5

        if (lightFunction3Switch == 2) //if switch is on
        {
            LEDpin4State = 1;
            //Flash LEDpin5 on/off
            if (millis() - LEDpin5Time > 150) //If [current time] - [last time pin1 switched] is more than 200 ms
            {
                LEDpin5State = !LEDpin5State; //! means NOT or opposite. Set state LOW if it's HIGH and vice versa.
                LEDpin5Time = millis(); //store [current time] as [last time pin1 switched].
            }
            
            LEDpin6State = !LEDpin5State; //pin 6 should be the opposite state of pin 5.

        }
        else if (lightFunction3Switch == 1)
        {
            //Turn pin 4,5,6 on
            LEDpin4State = 1;
            LEDpin5State = 1;
            LEDpin6State = 1;
        }
        else //if switch is off
        {
            //Turn pin 4 on, 5 and 6 off.
            LEDpin4State = 1;
            LEDpin5State = 0;
            LEDpin6State = 0;
        }

    
    /// --- Switch the Arduino I/O pins ---
    // digitalWrite(pin,state) where state can be HIGH/LOW or a value. 0 means LOW and anything else means HIGH.
    // analogWrite(pin,value) where value is a value between 0 and 255 can be used to PWM dim an LED on certain pins.

    digitalWrite(LEDpin1,LEDpin1State);
    digitalWrite(LEDpin2,LEDpin2State);
    digitalWrite(LEDpin3,LEDpin3State);
    digitalWrite(LEDpin4,LEDpin4State);
    digitalWrite(LEDpin5,LEDpin5State);
    digitalWrite(LEDpin6,LEDpin6State);

    
    //This section will send the current state of the LED pins to the "serial monitor" in the Arduino IDE.
    Serial.print(LEDpin1State);
    Serial.print('\t');
    Serial.print(LEDpin2State);
    Serial.print('\t');
    Serial.print(LEDpin3State);
    Serial.print('\t');
    Serial.print(LEDpin4State);
    Serial.print('\t');
    Serial.print(LEDpin5State);
    Serial.print('\t');
    Serial.println(LEDpin6State);
    //*/

    
} //End of loop
Reply With Quote