I 'm from Brazil I'm developing a master's research in innovation and technology that have to control a prosthetic hand Bionic Android interface. My question is how to differentiate progress bars on the Arduino code. I'm the Android angle and by sending an identification , for example " 180a " or " 100b " or 60a ....
I managed to differentiate the bars and off an LED, but my problem is how to control the Servant , because the angle is not being interpreted . Below the code I'm using , if someone wants to contribute to the research thanks :
#include <SoftwareSerial.h> #include <Servo.h>
Servo servo1;
Servo servo2;
SoftwareSerial bluetooth(10, 11); // RX, TX
char readBluetooth;
int lamp=13;
char buffer[4];
int received;
String teste;
if(c=='a'){
int numero = atoi(buffer);
received=0;
digitalWrite(lamp,HIGH);
servo1.write(numero);
}else if(c=='b'){
int numero = atoi(buffer);
if(numero>=60){
servo2.write(numero);
received=0;
}
digitalWrite(lamp,LOW);
received=0;
Servo code that might be close to what you are looking for. You can test with the serial monitor.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
Servo servo1;//Variavel meuservoT do tipo Servo
Servo servo2;
SoftwareSerial bluetooth(10, 11); // RX, TX
char readBluetooth;//Variável que irá receber o valor enviado do celular para o bluetooth
int lamp=13;//LED da porta 13
char buffer[4];
int received;
String teste;
void setup(){
bluetooth.begin(9600);//Inicia Comunicação com Bluetooth
servo1.attach(3);// porta 3 para o Motor
servo2.attach(4);
pinMode(lamp,OUTPUT);//Definindo o pino 13 como saÃda
// servo1.write(0);
received=0;
}
void loop(){
if(bluetooth.available()){
char c = (char)bluetooth.read();
if(c=='a'){
String valor = buffer;//recebe Valor do buffer como String
String angulo = valor.substring(1,1);//Cria uma variavel com o angulo por exemplo: 38a, captura somente 38
int angulo2 = angulo.toInt();//converte o angulo para inteiro
servo1.write(angulo2);
received=0;
digitalWrite(lamp,HIGH);
}else if(c=='b'){
int numero = atoi(buffer);
if(numero>=60){
// servo2.write(numero);
received=0;
}
digitalWrite(lamp,LOW);
received=0;
The "string" that you think you are receiving is NOT a string. A string is a NULL terminated array of chars. Not a NULL terminator in sight in your code. Do NOT pass things that are not strings to functions that expect strings.