Go Down

Topic: Multiple Serial and LED Control (Read 694 times) previous topic - next topic

corndog

I've been working on this code now for days and it does not seem to behave.

I have 2 separate sketches that do what I need them too, however it's when I bring them together into the same sketch that the problems start.

I have a serial rotary encoder (linked below) that outputs lovely serial values (seriously, it's just delightful) and my code successfully passes that serial back out to the computer (max/msp) via bluetooth.https://www.tindie.com/products/madworm/the-knob/

Code for WORKING sketch 1.

Quote

#include <SotwareSerial.h>
SoftwareSerial out(10, 11);
 
void setup()
{
  out.begin(9600);
  Serial.begin(9600);

}
 
void loop()
{

  if(out.available())
  {

    Serial.print((uint8_t)out.read());
  }
  if(Serial.available())
  {
    out.print((uint8_t)Serial.read());
  }
}



Now, I also have found some great code for interfacing with an RGB led via serial. This is also working code.

Quote

/*
  Reading a serial ASCII-encoded string.
 
 This sketch demonstrates the Serial parseInt() function.
 It looks for an ASCII string of comma-separated values.
 It parses them into ints, and uses those to fade an RGB LED.
 
 Circuit: Common-anode RGB LED wired like so:
 * Red cathode: digital pin 3
 * Green cathode: digital pin 5
 * blue cathode: digital pin 6
 * anode: +5V
 
 created 13 Apr 2012
 by Tom Igoe
 
 EDIT!!
 
 White 255,135,255
 
 This example code is in the public domain.
 */

// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // make the pins outputs:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt();
    // do it again:
    int green = Serial.parseInt();
    // do it again:
    int blue = Serial.parseInt();

    // look for the newline. That's the end of your
    // sentence:
    if (Serial.read() == '\n') {
      // constrain the values to 0 - 255 and invert
      // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
      red = 255 - constrain(red, 0, 255);
      green = 255 - constrain(green, 0, 255);
      blue = 255 - constrain(blue, 0, 255);

      // fade the red, green, and blue legs of the LED:
      analogWrite(redPin, red);
      analogWrite(greenPin, green);
      analogWrite(bluePin, blue);

      // print the three numbers in one string as hexadecimal:
      Serial.print(red, HEX);
      Serial.print(green, HEX);
      Serial.println(blue, HEX);
    }
  }
}



However, this is where I'm facing problems. I've brought the code together, or tried to, but it only seems to 'see' the serial knob code (first code snippet above) and ignore the RGB led code.
My most recent attempt is in the post to follow this one. (due to forum character limit)


I'd appreciate any feedback, I'm more of a code hacker/designer then programmer and this isn't really my territory, but I'm learning.

Thanks for your help.

corndog

#1
May 06, 2013, 03:25 am Last Edit: May 06, 2013, 04:49 am by corndog Reason: 1
Below is the above 2 codes 'combined' but not working. All I'm getting in my serial monitor is output from the serial knob.

Quote

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
// pins for the LEDs:
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;

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

void setup()
{
 mySerial.begin(9600);
 Serial.begin(9600);
 // initialize serial:
 Serial.begin(9600);
 // make the pins outputs:
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);
 
 //flash all LEDS as a test
 digitalWrite(redPin, LOW);   // turn the LED on (HIGH is the voltage level)
 delay(100);            // wait for a second
 digitalWrite(redPin, HIGH);    // turn the LED off by making the voltage LOW
 delay(100);               // wait for a second
 
 digitalWrite(greenPin, LOW);   // turn the LED on (HIGH is the voltage level)
 delay(100);            // wait for a second
 digitalWrite(greenPin, HIGH);    // turn the LED off by making the voltage LOW
 delay(100);               // wait for a second
 
 digitalWrite(bluePin, LOW);   // turn the LED on (HIGH is the voltage level)
 delay(100);            // wait for a second
 digitalWrite(bluePin, HIGH);    // turn the LED off by making the voltage LOW
 delay(100);               // wait for a second
 
}

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


void loop()
{
       
 if(mySerial.available())
 {
       //    digitalWrite(13, 1);
   Serial.print((uint8_t)mySerial.read());
   Serial.println();
 }
 if(Serial.available())
 {
       //    digitalWrite(13, 1);
   mySerial.print((uint8_t)Serial.read());
 }
 

// if there's any serial available, read it:
 while (Serial.available() > 0) {

   // look for the next valid integer in the incoming serial stream:
   int red = Serial.parseInt();
   // do it again:
   int green = Serial.parseInt();
   // do it again:
   int blue = Serial.parseInt();

   // look for the newline. That's the end of your
   // sentence:
   if (Serial.read() == '\n') {
     // constrain the values to 0 - 255 and invert
     // if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
     red = 255 - constrain(red, 0, 255);
     green = 255 - constrain(green, 0, 255);
     blue = 255 - constrain(blue, 0, 255);

     // fade the red, green, and blue legs of the LED:
     analogWrite(redPin, red);
     analogWrite(greenPin, green);
     analogWrite(bluePin, blue);

     // print the three numbers in one string as hexadecimal:
     Serial.print(red, HEX);
     Serial.print(green, HEX);
     Serial.println(blue, HEX);
   }
 }
}



To sum up, I'm trying to merge the code snippets in the first post together.

The intended outcome is:

Serial Knob > Arduinno > Computer (via Bluetooth)
Serial Characters from Computer (via same Bluetooth connection)> Ardunio >Triggers RGB LED

Both sketches work perfectly independently. There just seems to be conflict when merged together, this is where I'm going wrong.

PaulS

Code: [Select]
  Serial.begin(9600);
  // initialize serial:
  Serial.begin(9600);

Initialize the Serial instance. Do it again, just in case the first one didn't work. Maybe you need to do it 7 or 8 more times, just in case.

Code: [Select]
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

How come you do these only once?

Code: [Select]
  if(Serial.available())
  {
        //    digitalWrite(13, 1);
    mySerial.print((uint8_t)Serial.read());
  }
 

// if there's any serial available, read it:
  while (Serial.available() > 0) {

You mean "If there is any MORE serial data available, read it".

Do you really have a mySerial (whatever that is) connected to pins 10 and 11? A meaningful name for the instance would be useful. Then, we'd have some clue why that device needs to know about the data coming in on the hardware serial port.

Quote
I have a serial rotary encoder (linked below) that outputs lovely serial values (seriously, it's just delightful) and my code successfully passes that serial back out to the computer (max/msp) via bluetooth.https://www.tindie.com/products/madworm/the-knob/

Does the rotary encoder really expect you to talk TO it? Is it what is connected to the pins with the instance named out?

corndog

Ok, I had a chuckle, you got me, I was trying to keep my noob credentials hidden but you have successfully exposed them, thanks :)

The repeat Serial.begin(9600); is a start, I'll will take that out. Days of looking at the same lines of code...well one can miss a thing or two.

As for the mySerial, this is the software serial receiving integers from the serial knob. Only the RX (pin 10) is wired up. It didn't present an issues for me in the working sketch (the first "working" sketch I posted) so I left it.

The two sketches don't need to communicate together, they just need to do their job and keep out of the way of the other. Before I posted this I was using 2 Arduinos but due to space limitations I now can only use one.

I have been testing the 'merged' code on a Leonardo.


PaulS

Quote
As for the mySerial, this is the software serial receiving integers from the serial knob. Only the RX (pin 10) is wired up. It didn't present an issues for me in the working sketch (the first "working" sketch I posted) so I left it.

I really have no idea what a "serial knob" is, but, certainly using a name like knob for the instance would be a lot more meaningful than using mySerial, wouldn't it?

It would also be good to use something like -1 for the TX pin, to make it clear that the device is not capable of receiving data.

Also, it would be good to NOT include code that tries to write to it.

Go Up