Help needed : PWM control using serial port

I am trying to control 2 LEDs through a serial port. Can someone suggest a code i can use to control each LED individually?
This is what the code should do :
let the user control the intensity of LED1 from 0 to 255 independent of LED2 and vice versa.

Thanks

What have you tried?

Have a look at the examples in Serial Input Basics - especially the 3rd example.

…R

Hello Septillion,

Thanks for replying. I have used the standard code with for loop to increment the led brightness from 0 to 255 and decrement it. I am new to coding. Therefore, all i have been able to accomplish is this:

case 1 : LED 1 and LED 2 increase and decrease in brightness together.

case 2 : LED 1 goes to 255 while LED 2 goes to 0.

case 3 : the for loop seems to predetermine the time for which the LEDs undergo pwm.

I would like to write a code where the user will have two sliders to work with, one for LED 1 and another for LED2. when he/she manipulates the slider for LED1, LED 2 remains untouched by the code. and when the slider for LED 2 is manipulated, LED 1 remains untouched.

I hope this helps. I have been trying to come up with something for a month now. Thanks again for helping!

I have been trying to come up with something for a month now

So, post what you've got, and we can work on it with you.

I am trying to control 2 LEDs through a serial port.

I would like to write a code where the user will have two sliders to work with, one for LED 1 and another for LED2.

These are two very different approaches. Sending serial data from the serial monitor like "LED1 175" is very different from using analog slide potentiometers to control the levels of the two leds.

You may even be talking about devising a PC program with two sliders on a touch screen and sending the values to the Arduino.

The first thing to do on any project is to accurately specify what you are trying to do.

Yes, so I tried controlling the Arduino using Visual Basic and it worked for simpler operations.

For this particular one, I will be am trying to use a GUI with sliders and there will be individual sliders for each LED. I am trying to manipulate the sliders and they should in turn send the corresponding intensity values to a digital potentiometer and ultimately to the corresponding LED.

Science_girl:
I will be am trying to use a GUI with sliders and there will be individual sliders for each LED.

Any idea what form the data generated (?) takes? What's the resulting GUI serial output when the sliders get slid?

I used "while" statements to control my eeprom updating via serial port. Below is an example.

void eeprom1()    //enter eeprom read/write mode via serial comm
{
  while (Serial.available() > 0) {
    char inByte = Serial.read();

    while(inByte == 'm')
    {
      serialIn();
      break;
    }
    while(inByte == 'r')
    {
      eepromr();
      break;
    }
    while(inByte == 'w')
    {
      eepromwr();
      break;
    }
  }
}

You can use whatever characters you want this is just how I chose to do it.

Say in you VB program you send the letter "m" to the Arduino, in my sketch you will see that it invokes the function serialIn();. You can do something similar by building specific functions to do certain things when a certain character comes in on the serial port. It works quite well with the exception that you have to also include an "exit" scheme from each special function or you will be stuck there until the board is reset. Hope this helps.

Bill

Science_girl:
Yes, so I tried controlling the Arduino using Visual Basic and it worked for simpler operations.

For this particular one, I will be am trying to use a GUI with sliders and there will be individual sliders for each LED. I am trying to manipulate the sliders and they should in turn send the corresponding intensity values to a digital potentiometer and ultimately to the corresponding LED.

First you need to define a protocol. It can take the form of e.g. 'X:n\n' where 'X' is the number of the LED and 'n' is the intensity; '\n' acts as a terminator so your Arduino code can check if a message is complete. I'm not quite sure how the digital potentiometer fits in; maybe you can explain.

Next implement that protocol at the Arduino side and use Serial Monitor to debug and test. Once it's working, you can concentrate on the VB side of things.

Science_girl:
Thanks for replying. I have used the standard code with for loop to increment the led brightness from 0 to 255 and decrement it. I am new to coding. Therefore, all i have been able to accomplish is this:

case 1 : LED 1 and LED 2 increase and decrease in brightness together.

case 2 : LED 1 goes to 255 while LED 2 goes to 0.

case 3 : the for loop seems to predetermine the time for which the LEDs undergo pwm.

Are those case Arduino code or VB code? What is standard code with for loop to increment the led brightness from 0 to 255 and decrement it? Maybe post the code so we know what you have been trying.

Science_girl:
For this particular one, I will be am trying to use a GUI with sliders and there will be individual sliders for each LED. I am trying to manipulate the sliders and they should in turn send the corresponding intensity values to a digital potentiometer and ultimately to the corresponding LED.

The simplest way is to send both slider values every time even if only one, or none, changes. Perhaps send the values 10 times per second.

The simplest way to send the values IMHO is like “<123, 45>” where the first value is for LED1 and the second value is for LED2. That way there is no need for extra data to identify which is which.

The 3rd example in the link I gave you in Reply #2 is designed to deal with data in that format.

After your Arduino receives the data it should just update the relevant values for each LED and a separate LED function can use those values to update the brightness.

…R

I am trying to control 2 LEDs through a serial port. Can someone suggest a code i can use to control each LED individually?

I use the below to send numeric commands to control servos. You might do something similar to control the LEDs.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}