Reading Serial Port

Hello, I am connecting my Arduino to my Raspberry Pi via USB. I got one thing to work, where the LED on the Uno would blink the amount of times depending on what I typed into a program on Raspberry Pi.

The Arduino code was this:

const int ledPin = 13;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (Serial.available())  {
     blink(Serial.read() - '0');
  }
  delay(500);
}

void blink(int numberOfTimes){
  for (int i = 0; i < numberOfTimes; i++)  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}

I would type the following into the Raspberry Pi program:

>>> import serial
>>> ser = serial.Serial('/dev/ttyACM0', 9600)

And then:

>>> ser.write('3')

And then the Arduino would blink three times.

So then, I thought about turning a certain LED on/off depending on what was said in the Pi program.

So I made this code:

int red = 3;
int green = 5;
int yellow = 6;

void setup() {

  Serial.begin(9600);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  
}

void loop() {

  digitalWrite((Serial.read()), HIGH);
 
}

My idea was that the arduino would read the serial port, and if I typed 'red', then it would turn the red LED on, but I get no response...

What could I do to make this work?

There's a whole thread on serial basics.
Hint: most of the time, Serial.read will return -1.
You have no pin -1.

AWOL:
There's a whole thread on serial basics.
Hint: most of the time, Serial.read will return -1.
You have no pin -1.

hmm... So would I do something like:

digitalWrite((Serial.read() + 1), HIGH);

Thanks, and I will dig for that thread!

No, you wouldn't do that either, mostly because that would screw up the serial interface.

Okay, I think I found the thread. How does this look?

void loop() {

  if (Serial.available() > 0) {

    digitalWrite((Serial.read()), HIGH);

  }

}

Let's say you hit the '1' key.
You probably don't have a pin 49 either, unless you're on a Mega.

Ah...

void loop() {

  if (14 > Serial.available() > 0) {

    digitalWrite((Serial.read()), HIGH);

  }

}

Edit: Still no luck.

Now you're just making stuff up.
Slow down.
Read the whole of the serial basics post.
I mean all of it.

AWOL:
Now you're just making stuff up.
Slow down.
Read the whole of the serial basics post.
I mean all of it.

Okay, I tend to get a little ahead of myself sometimes(a lot). :blush:

Okay, so from the tutorial and this code:

char receivedChar;
boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");

 pinMode(3, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);

}

void loop() {
 recvOneChar();
 showNewData();
}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 newData = false;
 }
}

When I typed "ser.write(' **insert character here ** ')", on the Raspberry end, the character appeared in the Serial Monitor on the Arduino end.

Then, I tried modifying it as so:

char receivedChar;
boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvOneChar();
 showNewData();
}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 digitalWrite((receivedChar), HIGH);
 newData = false;
 }
}

I still get a response from the Serial Monitor, but none from the LEDs.

You are still not grasping the difference between an ASCII char '1' = 49, '2' = 50 '3' = 51. and the actual numbers 1,2,3.

You can get back to the numbers with

 digitalWrite((receivedChar-48), HIGH);

or alternatively if it makes more sense to you

 digitalWrite((receivedChar - '0'), HIGH);

cattledog:
You are still not grasping the difference between an ASCII char '1' = 49, '2' = 50 '3' = 51. and the actual numbers 1,2,3.

You can get back to the numbers with

 digitalWrite((receivedChar-48), HIGH);

or alternatively if it makes more sense to you

 digitalWrite((receivedChar - '0'), HIGH);

Aha! I just needed to put two and two together. Now it works like a charm. Thank you both for your assistance and guidance!