Pages: [1] 2   Go Down
Author Topic: I'd like to add control of an additional light to my sketch...  (Read 1181 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3412
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 25, 2013, 06:19:29 pm by HazardsMind » Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Portland, Oregon, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3357
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: February 26, 2013, 02:06:12 pm by Arrch » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  if (BTSerial.read() == 'N'){
    digitalWrite(whitepin, HIGH);
   
  }
  if (BTSerial.read() == 'F'){
  digitalWrite(whitepin, LOW);
  }
 
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3357
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char c = Serial.read();

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 84
Posts: 3412
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

Pages: [1] 2   Go Up
Jump to: