Offline
Jr. Member
Karma: 0
Posts: 54
|
 |
« on: February 25, 2013, 04:09:11 pm » |
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? 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
Offline
Edison Member
Karma: 29
Posts: 1571
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: February 25, 2013, 06:13:58 pm » |
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
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Portland, Oregon, USA
Offline
Newbie
Karma: 0
Posts: 29
|
 |
« Reply #2 on: February 26, 2013, 12:51:53 am » |
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 .. 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 .. 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
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #3 on: February 26, 2013, 08:26:52 am » |
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
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #4 on: February 26, 2013, 11:15:01 am » |
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 .. 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 .. 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
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #5 on: February 26, 2013, 11:23:32 am » |
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
Edison Member
Karma: 41
Posts: 1869
|
 |
« Reply #6 on: February 26, 2013, 02:04:18 pm » |
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
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #7 on: February 26, 2013, 02:15:23 pm » |
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
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #8 on: February 27, 2013, 01:03:56 pm » |
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 ' if (BTSerial.read() == 'N'){ digitalWrite(whitepin, HIGH); } if (BTSerial.read() == 'F'){ digitalWrite(whitepin, LOW); }
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 24
Posts: 2351
|
 |
« Reply #9 on: February 27, 2013, 01:49:55 pm » |
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
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #10 on: February 27, 2013, 02:12:37 pm » |
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
Edison Member
Karma: 41
Posts: 1869
|
 |
« Reply #11 on: February 27, 2013, 02:44:38 pm » |
Can you give an example? I see what your saying, I can type any key before the F and it turns off the light
char c = Serial.read();
if (c == VAL1) { // do something } else if (c == VAL2) { // do something else }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #12 on: February 27, 2013, 07:17:32 pm » |
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
Offline
Edison Member
Karma: 29
Posts: 1571
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #13 on: February 27, 2013, 07:46:33 pm » |
Post your code
|
|
|
|
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 54
|
 |
« Reply #14 on: February 27, 2013, 08:10:49 pm » |
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
|
|
|
|
|
|