Go Down

Topic: I'd like to add control of an additional light to my sketch... (Read 1 time) previous topic - next topic

AllenI

Currently my sketch controls two 12V RGB strips. I would like to add a 12V white led strip and be able to turn it on/off with an Android app I already created. The app sends a 'N' for ON, 'F' for OFF via Bluetooth. Where Im unsure is how to add it to my sketch. Whether or not to add it to the void loop or create its own function. Here is the void loop, you can see where I've started my attempt to add this function to the loop. Also, Im assuming the ground wire goes to the I/O pin rather than ground on my breadboard, correct?

Code: [Select]
void loop() {
  //App Inventor sends three bytes (RGB, 0 to 255).
  if ( BTSerial.available() > 3){
    //make sure it's 3 bytes
    if ( BTSerial.available() == 4){
      //read each of the 3 bytes for brightness into the variables
      redvalue=BTSerial.read();
      greenvalue=BTSerial.read();
      bluevalue=BTSerial.read();
      //flush the buffer
      BTSerial.flush();
    }
    else {
      //if the data is not 3 bytes treat it as invalid and flush the buffer.
      if (byte(BTSerial.read() == 'A'));{
      redvalue2=BTSerial.read();
      greenvalue2=BTSerial.read();
      bluevalue2=BTSerial.read();
    }
       
      BTSerial.flush();
    }
  }
  //write the current values to the pwm pins.
  analogWrite(redpin, redvalue);
  analogWrite(greenpin, greenvalue);
  analogWrite(bluepin, bluevalue);
  analogWrite(redpin2, redvalue2);
  analogWrite(greenpin2, greenvalue2);
  analogWrite(bluepin2, bluevalue2);
 
  //Add control for 12V light strip

  //String readString;
 
  if (readString.length() > 0){
    if (readString == 'N'){
    digitalWrite(whitepin, HIGH);
    }
  }
 
}



Right now, Im getting an error: "readString was not declared in this scope" When I take off the comment // for String readString I get more errors

HazardsMind

#1
Feb 26, 2013, 12:13 am Last Edit: Feb 26, 2013, 12:19 am by HazardsMind Reason: 1
Your reading data from an Android, how? Bluetooth, wifi, or direct connection?
If your using Bluetooth, use Serial.read(), to get the incoming data. There is a big debate about the use of Strings, so use them at you own risk. Try the serial monitor first.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Riccarr

Hi Allen,

I see some confusing code formatting that may be contributing to your problems.

For example, not sure what your trying to do on this line ..

Code: [Select]
if (byte(BTSerial.read() == 'A'));{

but I think its just plain wrong. Unless your aware your going for some advanced coding, you have the conditional check of "BTSerial.read() == 'A'" inside the cast of byte(). This would mean you want to case the true/false value to the byte cast function; but then your not even using the byte() cast in the if() ... its just thrown away. If you just trying to compare the read() to character 'A', then you just need ..

Code: [Select]
if(BTserial.read() == 'a') {

Then further on that line you have a semicolon ; between the end of the if() and the open curly bracket {. That is an odd place for a semicolon and I assume its misplaced.

There's nothing wrong with the String declaration, it complies just fine "String readString;". Any errors your getting must be related to some misplaced bracketing above that.

I would start by comment out most of that code until you get to a clean compile. Then start adding back in a few lines at a time and keep verify/checking to see what starts giving you problems.

PaulS

Code: [Select]
  if ( BTSerial.available() > 3){
    //make sure it's 3 bytes
    if ( BTSerial.available() == 4){

3 != 4. What are you trying to do? Why does it matter that there might be two packets to read? If you wait for 4, but there are 5, you'll never read anything.

AllenI


Hi Allen,

I see some confusing code formatting that may be contributing to your problems.

For example, not sure what your trying to do on this line ..

Code: [Select]
if (byte(BTSerial.read() == 'A'));{

but I think its just plain wrong. Unless your aware your going for some advanced coding, you have the conditional check of "BTSerial.read() == 'A'" inside the cast of byte(). This would mean you want to case the true/false value to the byte cast function; but then your not even using the byte() cast in the if() ... its just thrown away. If you just trying to compare the read() to character 'A', then you just need ..

Code: [Select]
if(BTserial.read() == 'a') {

Then further on that line you have a semicolon ; between the end of the if() and the open curly bracket {. That is an odd place for a semicolon and I assume its misplaced.

There's nothing wrong with the String declaration, it complies just fine "String readString;". Any errors your getting must be related to some misplaced bracketing above that.

I would start by comment out most of that code until you get to a clean compile. Then start adding back in a few lines at a time and keep verify/checking to see what starts giving you problems.



Yeah I know its not the cleanest code but it works(kinda buggy though). The way my app works is, I have control over the 1st strip, then when I press a button on the app, its sends an 'A' which then transfers the RGB values over to the 2nd strip. Thats how Im controlling both strips right now. Im not sure why I used byte, I was just messing with this for hours and I finally got it to work so I left it. But I'll make the changes see how it works, b/c though it works, like I said it is kinda buggy changing the 2nd strips color.

I get a clean compile for everything above //Add control for 12V light strip   comment. This program is a few months old, just adding to it.

Go Up