Jump to content
deptowicz

Control LED Dimming

Recommended Posts

Hello,

 

I've just started learning the basics of launchpad programming thru the samples in energia.

 

As an exercise, I'm trying to control the green LED intensity from the PC keyboard using code from the PhysicalPixel and Fade samples. I'm sending keyboard input via the Serial Monitor window of energia.

 

The code successfully uploads to the msp430g2231 but I'm not getting the result I'm expecting. That is press 'H' and 'L' to increase and decrease the LED intensity.

 

Here's the code:

 

int brightness = 128;
int incomingByte;      // a variable to read incoming serial data into
 
void setup() {
  Serial.begin(4800);     // initialize serial communication:
  pinMode(GREEN_LED, OUTPUT);        // initialize the LED pin as an output:
}
 
void loop() {
  if (Serial.available() > 0) {                // see if there's incoming serial data:
    incomingByte = Serial.read();        // read the oldest byte in the serial buffer:

 

    if (incomingByte == 'H') {
      brightness = brightness + 5;
      if (brightness >= 255) { brightness = 255; }
      analogWrite(GREEN_LED, brightness);
    }
    
    if (incomingByte == 'L') {
      brightness = brightness - 5;
      if (brightness <= 0) { brightness = 0; }
      analogWrite(GREEN_LED, brightness);
    }
  }
}

 

 

The Communication>Dimmer sample also does not work for me. I'm under the impression that the cause of the problem is similar.

Share this post


Link to post
Share on other sites

i am not a energia user but for similar serial control do something projects, i always try to make sure the comm is ok.

 

u can jumper the tx / rx line (join them together, crisscrossed, instead of jumper them onto the mcu device) on the launchpad and use a terminal to see if what u type actually echos back.

Share this post


Link to post
Share on other sites

Hi,

 

I'm pretty sure the comm is working because if I rewrite the code without the brightness settings, that is,

 

 

if (incomingByte == 'H') {
      analogWrite(GREEN_LED, 255);
 }
    
if (incomingByte == 'L') {
      analogWrite(GREEN_LED, 0);
 }

 

the code acts exactly the way I want it. Sending 'H' switches the LED ON(255) while sending 'L' switches the LED OFF(0). It is only when I try to control the brightness values that it fails to respond.

 

Out of curiosity, how do I use terminal to see that what I type echoes back?

Share this post


Link to post
Share on other sites

you need a serial terminal program. i don't know the capability of the comm windows on energia but it is likely it can receive chars back from the LP.

 

i use minicom on linux and putty on windows.

 

on the 5x2 jumper block on the LP. look for the RX and TX, pull out the jumpers for these 2. and tie the TX and RX (PC side, or USB side) together w/ a jumper cable or something. this will route what u type from the PC side immediately back to the PC. by doing so it will confirm the comm driver on the PC side (and the LP side) works properly.

 

many times when i had comm problems, it was caused by comm drivers, etc (especially under linux when the LP was new).

 

i can't help u w/ the pwm as i don't use energia.

Share this post


Link to post
Share on other sites

oh, it probably doesn't matter, but

 

 

int brightness = 128;
int incomingByte;      // a variable to read incoming serial data into

 

would usually be written as

 

 

 

uint8_t brightness = 128;
uint8_t incomingByte;      // a variable to read incoming serial data into
 

(or may be unsigned char)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×