This is my code:
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
char input = 0;
char string[50];
void setup () {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
int temp = 0;
int temp2 = 0;
void loop () {
if(Serial.available() > 0) {
input = Serial.read();
string[temp] = input;
temp++;
} else {
if (!sizeof(string) > 5 || !sizeof(string) == 2 || !sizeof(string) == 1) {
if (sizeof(string) == 3) {
if (string[0] == 'r' && string[1] == 'e' && string[2] == 'd') {
setColor(255, 0, 0);
} else if (sizeof(string) == 5){
if (string[0] == 'g' && string[1] == 'r' && string[2] == 'e' && string[3] == 'e' && string[4] == 'n') {
setColor(0, 255, 0);
} else if (string[0] == 'b' && string[1] == 'l' && string[2] == 'u' && string[3] == 'e') {
setColor(0, 0, 255);
}
}
}
}
}
}
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
Basically what I'm trying to do, is when I type red, the led should turn red, when I type blue, it should turn blue, same for green. Everything works, and is compiled, it just does nothing when I type anything in the terminal.
I really hope someone can help me.
-Alvin
Here is an example to show how to do what (I think) that you want using a method from Robin2's serial input basics tutorial to reliably receive the serial input and the strcmp() function to compare the input strings to decide which LED to light. Make sure that line endings in serial monitor is set to Newline.
// modified serial input basics tutorial (thanks Robin2) example 2 code by c goulding aka groundfungus
int redPin = 6;
int greenPin = 5;
int bluePin = 3;
const byte numChars = 10;
char receivedChars[numChars];
boolean newData = false;
void setup()
{
Serial.begin(115200);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop()
{
recvWithEndMarker();
if (newData == true)
{
Serial.print("received ");
Serial.println(receivedChars);
if (strcmp(receivedChars, "red") == 0)
{
setColor(255, 0, 0);
}
if (strcmp(receivedChars, "blue") == 0)
{
setColor(0, 255, 0);
}
if (strcmp(receivedChars, "green") == 0)
{
setColor(0, 0, 255);
}
newData = false;
}
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void setColor(int redValue, int greenValue, int blueValue)
{
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
sizeof(string) will always return the allocated memory (which is 50 bytes in your code). The function that you should be looking at instead is strlen() which returns the number of characters in a nul-terminated character array; e.g. strlen("hello") will return 5.
Further I agree with above advise to check out Robin's tutorial.
Thank you for taking the time to answer my question. But unfortunately it didn't fix it, I still get no response from the Arduino. The light on the board flickers when I send something, just nothing happens. Here's another code I wrote, that still doesn't work, but should:
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (String(String((char)Serial.read())).equals("red")) {
_ledColor(255, 0, 0);
}
if (String(String((char)Serial.read())).equals("green")) {
_ledColor(0, 255, 0);
}
if (String(String((char)Serial.read())).equals("blue")) {
_ledColor(0, 0, 255);
}
}
void _ledColor (double _redValue, double _greenValue, double _blueValue) {
analogWrite(3, _redValue);
analogWrite(5, _greenValue);
analogWrite(6, _blueValue);
}
Where do you capture the characters from the serial buffer? Below is simple serial echo coee that might be of use.
//zoomkat 6-29-14 Simple serial echo test
//type or paste text in serial monitor and send
String readString;
void setup() {
Serial.begin(9600);
Serial.println("Simple serial echo test"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(3); //slow looping to allow buffer to fill with next character
}
if (readString.length() > 0) {
Serial.println(readString); //so you can see the captured String
readString = "";
}
}
I finally got it to work!!!!
Here's the final code:
void setup() {
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(9600);
}
String readString;
void loop() {
while (Serial.available()) {
char c = Serial.read();
readString += c;
delay(3);
}
if (readString.length() > 0) {
if (readString.equals("red")) {
_ledColor(255, 0, 0);
Serial.println("Color changed to red.");
}
if (readString.equals("green")) {
_ledColor(0, 255, 0);
Serial.println("Color changed to green.");
}
if (readString.equals("blue")) {
_ledColor(0, 0, 255);
Serial.println("Color changed to blue.");
}
}
readString = "";
}
void _ledColor (double _redValue, double _greenValue, double _blueValue) {
analogWrite(6, _redValue);
analogWrite(5, _greenValue);
analogWrite(3, _blueValue);
}
Thank you to EVRYONE that helped!!!
I didn't sleep the whole night to figure this out, it is currently 8am. ;_D
Have a nice day.
The use of String (with capital S) is discouraged in microcontrollers with limited memory.
Extensive use of specifically concatenations can result in gaping holes in memory and eventually in unexpected behaviour at run time.
For your simple code it's OK. Try to learn to use so-called c-strings (as you started with).
I tested, successfully the code that I posted, with my Uno and 3 LEDs so do not know why it will not work for you. Did you set the serial monitor line endings to Newline? Did you set the serial monitor baud rate to 115200?