Maker Pro
Maker Pro

ardino code for Two color LED to PIC18F45K80

Hi,

I am looking help to understand following code because I do not understand programming for Arduino

https://tkkrlab.nl/wiki/Arduino_KY-029_Yin_Yi_2-color_LED_module_3MM

I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30

Code:
// Arduino test code:
int redpin = 11; // select the pin for the red LED
int bluepin = 10; // select the pin for the blueLED
int val;
 
void setup () {
  pinMode (redpin, OUTPUT);
  pinMode (bluepin, OUTPUT);
  Serial.begin (9600);
}
 
void loop () {
 for(val=255;val>0;val--) {
   analogWrite (11, val);
   analogWrite (10, 255 - val);
   delay(5);
 }
 for(val=255;val<255;val++) {
    analogWrite (11, val);
    analogWrite (10, 255 - val);
   delay(5);
 }
}

There are two for loop in code. I do not understand What's happening inside For loop for Two color LED's?

Thanks for any help
 
See here:
analogWrite()
delay()

Arduino code is a dialect of C++, easy to understand.

I don't understand what's happening in inside loop for LED controlling. LED can give red or green color at a time. I am trying to understand what's the logic needed to get different color from LED

simple I want a blue light on /off for 2 seconds and red light on /off for 2 seconds repeat process. Can you tell me logic for this ?

my plan for different color

BLUE_Pin = ON
RED_Pin = OFF
Wait for 2 seconds
BLUE_Pin = OFF
RED_Pin = ON
Wait for 2 seconds
Repeat process ..

Is my plan okay if its then I will write code ?
 

Harald Kapp

Moderator
Moderator
AnalogWrite() can output an analog value between 0 V (data = 0) and 5 V (data = 255) any value in between will make the LED shine more or less bright. If you simply need on/off, replace analogWrite by a digital output of 0 or 1 to the output pin the LED is connected.
Can you tell me logic for this ?
Your list shows the logic.

Why do you keep looking up Arduino Code if you don't understand it and can't use it? Search for PIC code instead, that'll help you much more.
 
What is a "current limiting register"?
Do you mean current limiting resistor? Yes, you do. See here.

Here is my code Big LED doesn't show any light only small led is blinking
Code:
#define _XTAL_FREQ 8000000
#include <xc.h>
// PIC18F45K80 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)
// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)
// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

#define RED            LATBbits.LATB0
#define BLUE            LATBbits.LATB1

void Port_Initialized (void);

void main(void)
{
 
    Port_Initialized ();
 
    while (1)
    {
         LATBbits.LATB0 = 0;  LATBbits.LATB1 = 1;
          __delay_ms(2000);
         LATBbits.LATB0 = 1;  LATBbits.LATB1 = 0;
          __delay_ms(2000);
      
    }
}

void Port_Initialized (void)
{
    LATA = LATB = LATC = LATD = LATE =  0;
    TRISA = 0b0000000;// all are output, Unused
    TRISB = 0b0000000;//
    TRISC = 0b0000000;// all are output, Unused
    TRISD = 0b0000000;//
    TRISE = 0b0000000;// All are output, Unused
 
    ANCON0 = 0; // digital port
    ANCON1 = 0; // digital port
    CM1CON = 0; // Comparator off
    CM2CON = 0; // Comparator off
    ADCON0 = 0; // A/D conversion Disabled
}

IMG_20201009_220358.jpg
 

bertus

Moderator
Hello,

Where are the leads going to?
I only see the module and not the other connections.
To test the module, connect the left pin to ground and the middle pin to +5 Volts.
The red led should be lit.
The other test is the left pin to ground and the right pin to + 5 Volts.
The green led should be lit.
When the middle and right pins are both connected to + 5 Volts, both leds will be on and the led will shine an orange like color.

Bertus
 
Hello,

Where are the leads going to?
I only see the module and not the other connections.
To test the module, connect the left pin to ground and the middle pin to +5 Volts.
The red led should be lit.
The other test is the left pin to ground and the right pin to + 5 Volts.
The green led should be lit.
When the middle and right pins are both connected to + 5 Volts, both leds will be on and the led will shine an orange like color.

Bertus

I have done as you said but no luck

Please look at attached screenshot. Yellow connected to ground
 

Attachments

  • IMG_20201011_231743.jpg
    IMG_20201011_231743.jpg
    279.9 KB · Views: 6
  • IMG_20201011_231839.jpg
    IMG_20201011_231839.jpg
    297.1 KB · Views: 6
Hello,

That is strange.
It could be the module is damaged.
Wich module do you have?
The KY-011 or the KY-029?
https://arduinomodules.info/ky-011-two-color-led-module-3mm/
https://arduinomodules.info/ky-029-dual-color-led-module/

Bertus
Its difficult to find which one I have, because nothing is written on the board

Back to post #1 I think I should try the example code of Arduino But I do not understand the Arduino program. I have searched the sample code for the pic which I am not getting on internet.

If someone can tell me the logic of Arduino program, then I can write program for pic
 

bertus

Moderator
Hello,

The arduino code is fading from red to green and from green to red and so on.
The delay between each fade step is 5 mSec.
The total cyclus is 2 times 256 steps of 5 mSec.
So the total cyclus is about 2.5 seconds.

Bertus
 

bertus

Moderator
Hello,

I am thinking, perhaps you module uses an other kind of led and does not follow the connections of the arduino modules.

Bicolor_LED_cross_section_three_pin_version 600px.jpg

Try the middle pin to ground and the left or right via a 220 Ohms resistor to the + 5 Volts.

Bertus
 
Hello,

I am thinking, perhaps you module uses an other kind of led and does not follow the connections of the arduino modules.

View attachment 49455

Try the middle pin to ground and the left of right via a 220 Ohms resistor to the + 5 Volts.

Bertus
I did the connection as you said. Red light is turning on.
 

Attachments

  • IMG_20201012_010350.jpg
    IMG_20201012_010350.jpg
    287.9 KB · Views: 2
Top