Continous reading of multiple char's from a string through serial-connection

Hi all.

A total Noob at both programming and arduino here.

I'm working on a project where I want to hook up a some different LED's to the breadboard. I have watched a lot of tutorials and read a lot of guides and have gotten a basic understanding of most of what I need.

But I've hit a wall now. What I want to do is to assign a specific character to a specific LED, have a few of them next to each other and have them blink whenever their characters' are typed into to the Serial Monitor.

I.e. LED 1 blinks when a is received, LED 2 blinks when b is received etc... I've already been able to do that when I type in the characters one by one using the "if" and "serial.read" commands. But what I want to do is to be able to type in "abcdefghij" all at once, hit enter and then see LED 1 through 10 blink once. (not all at once but in succession of each other)

I am perfectly prepared to do my own research and to read about the methods on my own. But I am not sure which way to go here?

Should I read about "get.bytes"? "String to char array?" Etc... Which way would be the best to go here?

Thank you in advance for your replies.

Read the chars into a char array. You know you're done when you receive an end of line character. Then you can process the array.

You probably could just put a delay in the code you are currently using.

The examples in serial inout basics show how to receive the data and save it in a char array. The second example may be what you need.

When the data is in the array just iterate through it get your effects.

I suspect that some simple maths could convert your received characters (e.g. a or b) into pin numbers for your LEDs so that you don't need any if statements.

Suppose that ledA is on pin2 and ledB is on pin3. 'A' is 65, 'B' is 66. So 'A' - 63 gives 2 and 'B' - 63 gives 3 Assuming the letter is stored in a variable called inChar then you can light the appropriate LED with digitalWrite(inChar - 63, HIGH);

...R

Edit to add Link - apologies for the oversight

Ok. I finally managed to code something that actually worked (for now at least)

As usual I tried to make it way more difficult than it had to be.

Here is the code as it is now:

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;
int ledPin9 = 10;
int ledPin10 = 11;

void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
}

void loop()
{
while (Serial.available() == 0);

int val = Serial.read() -‘0’;
if (val == 1)
{
Serial.println(“On”);
digitalWrite(ledPin1, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
}
else if (val == 2)
{
Serial.println(“Off”);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
}
else if (val == 3)
{
Serial.println(“Off”);
digitalWrite(ledPin3, HIGH);
delay(500);
digitalWrite(ledPin3, LOW);
}
else if (val == 4)
{
Serial.println(“Off”);
digitalWrite(ledPin4, HIGH);
delay(500);
digitalWrite(ledPin4, LOW);
}
else if (val == 5)
{
Serial.println(“Off”);
digitalWrite(ledPin5, HIGH);
delay(500);
digitalWrite(ledPin5, LOW);
}
else if (val == 6)
{
Serial.println(“Off”);
digitalWrite(ledPin6, HIGH);
delay(500);
digitalWrite(ledPin6, LOW);
}
else if (val == 7)
{
Serial.println(“Off”);
digitalWrite(ledPin7, HIGH);
delay(500);
digitalWrite(ledPin7, LOW);
}
else if (val == 8)
{
Serial.println(“Off”);
digitalWrite(ledPin8, HIGH);
delay(500);
digitalWrite(ledPin8, LOW);
}
else if (val == 9)
{
Serial.println(“Off”);
digitalWrite(ledPin9, HIGH);
delay(500);
digitalWrite(ledPin9, LOW);
}
else if (val == 10)
{
Serial.println(“Off”);
digitalWrite(ledPin10, HIGH);
delay(500);
digitalWrite(ledPin10, LOW);
}
else
{
Serial.println(“Invalid”);
}
}

It works well enough…

If I type in 1234, the led on pin 2, 3, 4 and 5 light up for half a second. but if I write 10, then the led on pin2 lights op and the serial monitor writes On and Invalid.

I understand why it does this but at the moment I don’t know how to fix it.

For now I will try to experiment with replacing all the integer variables with Strings and then assign letters to each LED instead.

 if (val == 10)

Look at the computer keyboard in front of you. Locate the key marked "10".

AWOL: Look at the computer keyboard in front of you. Located the key marked "10".

I sense you are making fun of me. But to that I can only refer to the very first thing that I wrote in this post: that I am not only inexperienced but completely new to programming.

So if what you mean is that, what I wrote in the code only can refer to a specific number key and not the number, I'd have no way of knowing that.

The only way you could make the condition x==10 true would be if you input the key with the ASCII code '0'+10, which is 0x3a. Looking at your ASCII chart, you'll see this the colon character ':'

Please remember to use code tags when posting code.

but if I write 10, then the led on pin2 lights op and the serial monitor writes On and Invalid.

The arduino only reads one byte at a time. If you want to evaluate 10 which is sent as two bytes (1 and 0), then you will need to change your code.

I just realized I forgot to put the link to serial input basics in Reply #3 - apologies.

Have a look at the examples in that link. And my suggestion for some simplifying maths in reply #3

...R