SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By swilkerson
#200403
Help Please The following code returns !B11 fro iPhone button push. I'm just not sure what that is. It's declared as an integer and I'd like to use an if then statement to see which button is pushed. Can someone help me.

/*
My Example 9/17/2018
*/
#include <SoftwareSerial.h>
int Str1, Str2;
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial mySerial(bluetoothRx, bluetoothTx); // Set up serial port to pins 2 TX and 3 RX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
}
}
void loop() { // run over and over
if (mySerial.available()) {
Str1 = mySerial.read();
Serial.write(Str1);
//Serial.write('\n'); // Carriage Return
}

}
By swilkerson
#200414
More simply if I have a string

Str1 = 'ABCD';

how do I reference B for example? For the life of me I can not figure this out.
By lyndon
#200416
Normally, Str1 would be a char array
Code: Select all
char Str1[20]
or similar. And you would reference the second element as
Code: Select all
 char c = Str1[1]
. But in your example Str1 & Str2 are int.
By swilkerson
#200428
* More Specifically (I'm reading button pushes from my iPhone)
* I'm using the BlueFruit App
The code I'm attaching sends back !B11:!B10; The !B11 is the button push and the !B10 is the button release.
When ever I try and put this into a string or store it so I can access each individual Byte I get nothing. You'll see one of my attempts commented out in the code.
I've been stuck on this for 2 days and spent countless hours trying to get it to work, I think i'm just too stupid to figure it out and need help
Please!


/*
My Example 9/18/2018
*/
#include <SoftwareSerial.h>

char c;
String readString[10];

int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial mySerial(bluetoothRx, bluetoothTx); // Set up serial port to pins 2 TX and 3 RX

void setup() {
Serial.begin(9600);
mySerial.begin(57600); // This was the speed it liked.
}

void loop() {
int i = 0;
if (mySerial.available()) {
delay(2);
c = mySerial.read();
Serial.print(c); // This information is fine c contains the current item from the buffer when done it stops
//readString = String(c);
i = i+1;
}
//Serial.println(readString[2,3]); // when included I get nothing....
//delay(2000);
}
By swilkerson
#200429
Also each button sends a different group of characters on byte at a time stored in the buffer. So for example I want to move forward when !B11 is sent and stop when !B10 or the button is released. Then Let's say another Button sends !B41 is sent when that button is pushed and !B40 when released. Therefore having the latest button sequence stored allows me to compare each term and assign movements to robots.
Thank you to anyone how can help me....
By swilkerson
#200430
I think I've figured it out. It's something to do with the difference in me using an if statement rather than a while statement. In other words in the above I used
if (mySerial.available()) {
.
.
.
}
If I change that to:
while (mySerial.available()) {
.
.
.
}
the variables do not change. Maybe someone could be kind enough to explain that one to me.
By swilkerson
#200431
Yes I just ran my test and that is the solution. I will post my final code tomorrow when I wrap it up.
By teprojects1
#200458
Good to know that you sort it out. Yeah share the final code here , it will help others having similar issues. :)
By swilkerson
#200532
Ok I have it working pretty good. I have a video too here: https://www.youtube.com/watch?v=YBieM-_nFbw
here is the code I used, very simple. Perfect for kids. I'm working on doing this with a Balboa now.

/*
My Example 9/17/2018
*/
#include <SoftwareSerial.h>
#include <Servo.h>

Servo myservo1, myservo2;

char c;
String readString[10];
int MyInt1, State1, State2;

int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial mySerial(bluetoothRx, bluetoothTx); // Set up serial port to pins 2 TX and 3 RX

void setup() {
myservo1.attach(9); // Continuous tern servos
myservo2.attach(10); // Continuous tern servos
Serial.begin(9600);
State1 = 0;
digitalWrite(13,LOW); // Lights out
while (!Serial) {
}

mySerial.begin(57600); // This was the speed it liked.
}

void loop() { // run over and over to get the button presses
int i = 0;
while (mySerial.available()) {
delay(2);
c = mySerial.read();
if (c != '\n'){
readString[ i ] = String(c);
i = i + 1;
}
}
// Now make the robot move in accordance with the button presses
// Full Stop
if (readString[3] == String('0')){
digitalWrite(13,LOW);
//Serial.write("S");
State1 = 1;
myservo1.writeMicroseconds(1500); // Make Servo Stop Moving
myservo2.writeMicroseconds(1500); // Make Servo Stop Moving
}
// Full Forward
else if(readString[2] == String('5')){
digitalWrite(13,HIGH);
//Serial.write("F");
myservo1.writeMicroseconds(2000); // Make Servo Move
myservo2.writeMicroseconds(1000); // Make Servo Move
}
// Right Turn
else if(readString[2] == String('8')){
digitalWrite(13,HIGH);
//Serial.write("R");
myservo1.writeMicroseconds(2200); // Make Servo Move
myservo2.writeMicroseconds(1700); // Make Servo Move
}
// Left Turn
else if(readString[2] == String('7')){
digitalWrite(13,HIGH);
//Serial.write("L");
myservo1.writeMicroseconds(1300); // Make Servo Move
myservo2.writeMicroseconds(800); // Make Servo Move
}
// Backwards
else if(readString[2] == String('6')){
digitalWrite(13,HIGH);
//Serial.write("B");
myservo1.writeMicroseconds(1000); // Make Servo Move
myservo2.writeMicroseconds(2000); // Make Servo Move
}
}
// An that's it