Hi all, I have an android app (that I have made myself) In it I have a slider that sends thumb position data (0-180 in whole numbers) over Bluetooth to my arduino nano. The code below comes from zoomkat I believe and it is working great! It controls my micro servo on pin D9 perfectly. I also have two buttons in the android app that send "on" and "off" to control an LED (eventually a motor) This does not work at all?
// type servo position 0 to 180 in serial monitor
String readString;
#include <Servo.h>
Servo myservo; // create servo object
int led = 13; // Pin 13
void setup() {
pinMode(led, OUTPUT); // Set pin 13 as digital out
Serial.begin(9600);
myservo.writeMicroseconds(0); //set initial servo position
myservo.attach(9); //the pin for the servo control
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
int n = readString.toInt(); //convert readString into a number
myservo.write(n);
// }
readString=""; //empty for next input
String input = "";
if (input == "on")
{
digitalWrite(led, HIGH); // on
}
else if (input == "off")
{
digitalWrite(led, LOW); // off
}
}
}
been at it for a few hours now and cannot figure it out, google has helped me to this point, however all i can get it to do is either work good with the servo and not the led, or work the led but not the servo?
This create an empty String and check if it's equal to "on".. That will never be true!
You have to do something like this instead:
if ( readString.length() >0 )
{
if ( readString == "on" )
{
digitalWrite( led, HIGH ); // on
}
else if ( readString == "off" )
{
digitalWrite( led, LOW ); // off
}
else
{
int n = readString.toInt(); //convert readString into a number
myservo.write( n );
}
readString = "";
}
Thank you a bunch guix, that worked great! Now that you have helped me get it working, I can examine how and why it works and learn where I went wrong. I had it working last night only partially. The led would light then all of a sudden the servo slider would not work and the led buttons would control the servo :o Just before I posted the first post I was thinking I should have the led code above the servo, and I see the "else" works good. I had a brain fart that I couldn't use it more than once It took me too many hours just to get where I am at, would you happen to personally recommend any arduino specif coding books?, just the coding.
The arduino reference is helpful, but I need a little more help (I am a visual learner)
I can't recommend a book, but arduino code is C/C++ code, so find some tutorials about C/C++ basics (because arduino doesn't support most of advanced stuff)
Here is a modified (and untested) version of your code, I highly recommend to not use the String class or delays!
const size_t INPUT_SIZE = 20;
void loop()
{
if ( Serial.available() > 0 ) // if there is something in the serial buffer
{
static char input[ INPUT_SIZE ];
static uint8_t i;
char c = Serial.read(); // read one char
if ( c != '\n' && i < INPUT_SIZE - 1 ) // if the char isn't a newline, and there is room in the input buffer
input[ i++ ] = c; // store the char in the input buffer
else
{
input[ i ] = '\0'; // add a null terminator to make it a valid C string
i = 0; // reset index for next command
if ( !strcmp( input, "on" ) )
{
digitalWrite( led, HIGH );
}
else if ( !strcmp( input, "off" ) )
{
digitalWrite( led, LOW );
}
else if ( isNumeric( input ) )
{
myservo.write( atoi( input ) );
}
}
}
}
bool isNumeric( const char *s )
{
for ( size_t i = 0, j = strlen(s); i < j; i++ )
if ( s[i] < '0' || s[i] > '9' )
return false;
return true;
}
You will have to modify your android program to send a '\n' at the end of each command.
The Arduino code in this demo illustrates a reasonably comprehensive way to receive any sort of data. It works fine with the Serial Monitor if you want to try it out without the Python program.