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.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

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.

AllenI


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.


This was my shotty attempt at adding control for the 2nd strip. I was trying to send 'A',R,G,B. I was trying to send an 'A' with the RGB values to the arduino so it knows to write to the 2nd strip rather than the 1st.  I will try Serial.read like suggested above.

However from my original question, to add on/off control for a white led strip, should I add the code to the void loop() how I have it now or should I make it its own function?

Arrch

#6
Feb 26, 2013, 08:04 pm Last Edit: Feb 26, 2013, 08:06 pm by Arrch Reason: 1

However from my original question, to add on/off control for a white led strip, should I add the code to the void loop() how I have it now or should I make it its own function?


What makes you think it needs it's own function?

There are two primary reasons to put code inside a function:
- Re-usability
- Break up larger code sections in to smaller, easier to follow functions

Does putting what you're trying to accomplish in a function achieve one of these two?

AllenI

I wasn't sure thats why I was asking. But from your reply it seems Im ok keeping it inside the loop for what Im trying to do. Thanks

AllenI

Ok Im alittle further along. I'm able to turn the light on using the serial monitor typing the character N. However to turn it off I have to type in 'F rather than F to work properly. Can someone explain why this is? I'd like to just enter the character F alone w/o the '

Code: [Select]
  if (BTSerial.read() == 'N'){
    digitalWrite(whitepin, HIGH);
   
  }
  if (BTSerial.read() == 'F'){
  digitalWrite(whitepin, LOW);
  }
 

wildbill

It's because you read from BTSerial each time you test to see what the char is. If you type N, the first read correctly picks it up. If you type F, the first read grabs it, compares it to N and does nothing. Then your F testing if has missed it. If you put any character in front of the F, it will be acted on.

You need to do the read once before you check what it is. Store it in a variable and them test that against as many values as you like.

AllenI

Can you give an example? I see what your saying, I can type any key before the F and it turns off the light

Arrch


Can you give an example? I see what your saying, I can type any key before the F and it turns off the light


Code: [Select]
char c = Serial.read();

if (c == VAL1)
{
  // do something
}
else if (c == VAL2)
{
  // do something else
}

AllenI

OK I used a switch case to get my white light strip to turn on/off. However now the rest of my code in the void loop does not work. wtf? it never ends

HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

AllenI

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);
 

 
  switch (BTSerial.read()){
 
  case 1:
   digitalWrite(whitepin, HIGH);
   break;
  case 2:
   digitalWrite(whitepin, LOW);
   break;

  }
}


The if statements and the switch cases work individually but not as one whole program

Go Up