USB question

32 views
Skip to first unread message

Justin L

unread,
Jul 18, 2012, 12:57:37 PM7/18/12
to toasted-circu...@googlegroups.com
Hi Andrew,

I have a question about the USB on the lightuino. I've read through Beginning Arduino and the Arduino Cookbook, and have read pretty much every post (it seems) on Arduino and serial comm. My angle of attack on this project is to send character strings and pars them, in order to send multiple variables to the lightuino at once (obviously its still serial). Heres my patch that works great on the uno (using Serial.xxxx class), but it won't work on the lightuino. When i switch the Serial.xxxx class to Usb.xxxx class for the lightuino it will only print the first character of the string then stops working at that point. Is there something different about the Usb.xxxx and using SPI that I need to account for? I'm not going to be controlling one rgb led like in this example, but the concept of parsing strings is what I'm after because I can translate that into my project. Thanks so much for your help! Heres the code- Cheers! Justin


#include <lightuino5.h>
LightuinoSink sinks;   
LightuinoSourceDriver sources;
FlickerBrightness pwm(sinks);




// Project 10 - Serial controlled mood lamp///// This is the project out of Beginning Arduino that Im studying
char buffer[18];
char val[0];
int red, green, blue; /////// these parts of the code aren't involved with what I'm doing
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;
void setup()
{
{pwm.StartAutoLoop(4096);}
  
  Serial.begin(9600); ///or Usb.begin(); for the lightuino

}
void loop()
{
if (Serial.available() > 0) {  ///on lightuino Usb.available() etc. throughout the code
        int index=0;
        delay(100); // let the buffer fill up
        int numChar = Serial.available();
        if (numChar>15) {
                numChar=15;
         
       }
        while (numChar--) {
                buffer[index++] = Serial.read();
        }
        splitString(buffer);
}
void splitString(char* data) {
        Serial.print("Data entered: ");
        Serial.println(data);
        char* parameter;
        parameter = strtok (data, " ,");
        while (parameter != NULL) {
          

        setLED(parameter);
        delay(30);
        parameter = strtok (NULL, " ,");
        }
 // Clear the text and serial buffers
for (int x=0; x<16; x++) {
        buffer[x]='\0';
 }
Serial.flush();
}
void setLED(char* data) {
        if ((data[0] == 'r') || (data[0] == 'R')) {
                int Ans = strtol(data+1, NULL, 10);
                
                Ans = constrain(Ans,0,255);
               
                 analogWrite(GreenPin, Ans); ////on the lightuino I'd use something like  pwm.brightness[variable 1 from string] = (variable 2 from string)
                Serial.print("Red is set to: ");
                Serial.println(Ans);
        }
        if ((data[0] == 'g') || (data[0] == 'G')) {
                int Ans = strtol(data+1, NULL, 10);
                Ans = constrain(Ans,0,255);
                analogWrite(GreenPin, Ans);
                Serial.print("Green is set to: ");
                Serial.println(Ans);
        }
        if ((data[0] == 'b') || (data[0] == 'B')) {
          int Ans = strtol(data+1, NULL, 10);
          Ans = constrain(Ans,0,255);
          analogWrite(BluePin, Ans);
          Serial.print("Blue is set to: ");
          Serial.println(Ans);

Andrew Stone

unread,
Jul 18, 2012, 1:37:40 PM7/18/12
to toasted-circu...@googlegroups.com
Hi Justin,

Send me your Lightuino sketch as an attachment (the bottom got cut off).  Please send it with the Lightuino serial and LED calls, not the Arduino ones so I can debug the exact sketch that is not working for you.

Cheers!
Andrew

Justin L

unread,
Jul 22, 2012, 11:10:05 PM7/22/12
to toasted-circu...@googlegroups.com
Hi Andrew,

Thanks for your reply. Here's the attached sketch.

Justin
mood_lamp_lightuino.ino

Andrew Stone

unread,
Jul 28, 2012, 5:16:43 PM7/28/12
to toasted-circu...@googlegroups.com
Ahh... I found it.  Sorry that it took me awhile to find the time to dig into this.  I have a very busy week and will do better next time!  Anyway, use this loop() code instead.  The problem is that the Lightuino is returning true/false for "available", where Arduino Serial returns the number of bytes available.  I'll change mine to match but for now use what's below.  Anyway, note that this code might be considered a little "better" anyway because it allows you to grab characters that come in while within the loop.

I gave it: r220,g150,b100 and got:
Data entered: r200,g150,b100
Red is set to: 200
Green is set to: 150
Blue is set to: 100


Cheers!
Andrew

void loop()

{

if (Usb.available() > 0) {


        int index=0;

        delay(100); // let the buffer fill up

        while (Usb.available() && index<15) {

                buffer[index++] = Usb.read();

        }

        splitString(buffer);

Justin L

unread,
Jul 30, 2012, 11:58:18 PM7/30/12
to toasted-circu...@googlegroups.com
Andrew,

Thank you!! I can't wait to get back to work on this project! I was kinda stuck there on that one. I really appreciate you taking the time to help out on this. Don't worry about it taking a little time to get back.

Cheers!
Justin
Reply all
Reply to author
Forward
0 new messages