Hi,
This is frustrating me to death and I'm sure it's something simple. I am trying to send a command via the serial port that will change the brightness of an LED. For example, if I type ledfade-127 in the Serial Monitor and click Send. I would expect the LED to be at 1/2 brightness. I can use the FADE sketch and it works just fine (it's in a loop). Mine is based on a command - not in a loop. Here's my code. Can anyone shed some light on my problem? My goal is to be able to pass "command" from mono/C# to the arduino.
Thanks,
Frank
#include <Servo.h>
int ledPin = 9; //digital pin 9
int servoPin = 10; //digital pin 10
String inputStr = "";
static float f_val = 0.0;
static char outstr[15];
Servo myServo;
void setup() {
Serial.begin(9600);
//pinMode(ledPin, OUTPUT);
myServo.attach(servoPin);
inputStr.reserve(200);
}
void loop() {
if (Serial.available() )
{
inputStr = Serial.readString();
Serial.flush();
Serial.println(inputStr);
String temp = inputStr.substring(0, 7);
//Serial.println(temp);
if (temp.equalsIgnoreCase("ledfade"))
{
digitalWrite(ledPin, LOW);
//Serial.println("here");
int len = inputStr.length();
temp = inputStr.substring(8, len);
//Serial.println(temp);
int brightness = temp.toInt();
Serial.println(brightness);
analogWrite(ledPin, brightness);
}
if (inputStr == "ledon")
{
digitalWrite(ledPin, HIGH);
}
if (inputStr == "ledoff")
{
digitalWrite(ledPin, LOW);
}
if (inputStr == "servocw")
{
myServo.write(0);
}
if (inputStr == "servoccw")
{
myServo.write(180);
}
if (inputStr == "servoctr")
{
myServo.write(90);
}
inputStr = "";
}
}
//void serialEvent() {
//Serial.flush();
//}
If you are just sending numbers, then you can use Serial.parseInt();
Say you send -127, well using parseInt(), it will take that string and convert it to an actual value of -127.
Im not sure if you need to wait to see if a certain amount of characters have been entered or if it will keep reading the buffer until it finds a NULL character. I want to say it will keep reading, but you can confirm that yourself.
I'm getting the numbers just fine. Parsing the strings just fine. The only time the LED will even come on is if I pass it a ledfade-255 (so the AnalogWrite is getting 255). If I pass any other number - the LED will go off.
I hope someone can verify this.
btw - Using IDE 1.0.1 on Rasbian and an RPi 2.
Frank
Try this. It should work much better than what you have now.
http://cpp.sh/546h
It said server timed out.
After stripping the code to almost nothing the problem was the line:
myServo.attach(servoPin); //int servoPin = 10; before Setup()
Once I commented that line out - everything worked as it was suppose to.
What a pain in the a$$ figuring that out was!!
Well then it could be the servo and pin 9 are using the same timer, in which case, fading the LED on pin 9 would not work.
To get both working, you will need to change one of the pins. But the LED needs to be connected to a pin that has the ~ next to it.
Also Strings use too much memory. However changing your code to use char pointers (char*) would mean you would need to rewrite the code. But if all you are doing is moving a servo and changing the brightness of an LED, then you can keep the code as is. If not and this is a small part of a larger project, then I would advise you use char pointers.
This should be better and much smaller code than what you have.
To run this, just type in something along the lines of LED_ON(your number). <- the period is the delimiter, unless changed.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(9, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
static char buffer[20]; // make it big enough for your string to fit + NULL character
static byte i = 0;
if (Serial.available() > 0)
{
char input = Serial.read();
delay(1); // 1 ms is needed for it to actually work correctly. I don't know why, but it needs it.
if (input != '.') // look for . or the END delimiter
{
buffer[i] = input;
buffer[i + 1] = 0; // add NULL character
i++;
}
else
{
if (FindMyString(buffer, "LED_ON"))
Serial.println("FOUND IT!");
Serial.println(buffer); // show the buffer
analogWrite(9, findInt(buffer)); // adjust the leds brightness by the inputted value
memset(buffer, 0, sizeof(buffer)); // clear the buffer
}
}
else
i = 0;
}
long findInt(char* S)
{
long result = 0;
short i = 0;
bool neg = false;
while (*(S + i) != NULL)
{
if ((*(S + i) >= '0' && *(S + i) <= '9') || *(S + i) == '-')
{
if (*(S + i) == '-') neg = true;
else
result = result * 10 + *(S + i) - '0';
}
i++;
}
if (neg) result *= -1;
return result;
}
byte FindMyString(char* buf, char* string)
{
byte _Size = strlen(string);
byte i = 0, idx = 0;
while (*(buf + i) != NULL) // *(buf + i) is the same as buf[i]
{
if (buf[i] == string[idx]) // go through the buffer and compare the characters
idx++; // if a buffer character matches a character in your string, go to the next character in your string
else
idx = 0; // no match found, reset idx
i++;
if (idx == _Size) // if all the characters in your string were found, return true.
return true;
}
if (idx < _Size) // missing characters
return false;
}