Serial.read Problem

I searched and couldn't find an answer to this - When I program my arduino, it prints serial just fine. It also reads and prints the information it received to the computer. BUT when I program the arduino to read a 1 or 2 from the computer to turn on or off an led, all that happens is the RX led flashes, receiving the information, but not turning on the led. :-?

Any Ideas???

Hi MLCC;

Can you post your code? It is easier to answer that question with something tasty to look at and digest.

Without the code I couldn't say. My best guess would be that you're sending, from the PC, an ASCII '1' such as you'd get by pressing the '1' key on your keyboard but on the arduino you're listening for a binary-coded-decimal 1, such as you'd get by int i = 1;

Sorry. I have two computers, one that can't run internet, and one that can't program... Here you go:

/*
Date: 1/90/991
Name: Serial_Test_debug
Ver: 1.85
Created By: Michael
Notes:
  Not Working for some reason...
  
  
*/

// Variables
int AByte = 0;
int ledpin = 13;

// Startup
void setup () {
  Serial.begin(9600);
  pinMode(ledpin,OUTPUT);
}

// Main
void loop () {

  while (Serial.available() == 0) {}    //Wait for information before continuing
  
  //read serial  
  AByte = Serial.read();
  
  // 1=LED ON  2=LED OFF  
  if (AByte == 1) {
      digitalWrite(ledpin,HIGH);
  }
  if (AByte == 2) {
      digitalWrite(ledpin,LOW);
  }
}

If it helps, I'm using a Arduino Duemilanove.

Fjornir: Didn't notice that you had just posted... That sounds right, but how would I fix this?

  if (AByte == 1) {

You're comparing an ASCII byte read off the serial port to a binary coded decimal '1' ...

  if (AByte == '1') {

is probably what you want.

Thanks! 8-) It's working now! ;D ;D

i have an identical problem

i send to the arduino from the pc a demical number

in this format

AXXX

XXXis the three digit number

search = Serial.read(); if (search == 'A') { rpm100 = Serial.read(); rpm10 = Serial.read(); rpm1 = Serial.read();

rpm = (100*rpm100) + (10*rpm10) + rpm1;

position = map(rpm,127,255,40,0);

my problem is that the output is not a number

and i can continue

i also have the choice to send the serial command in binary and hexademical format

thanks a lot

You are reading a character from the serial port, like the letter '4', and treating it as though you read the number 4. To get the number 4 from the letter '4', subtract '0' from the letter:

int num = Serial.read() - '0'; // num will be 4 if '4' was read

i tried that also

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1274183719/0#10

What values end up in rpm100, rpm10, and rpm1?

in the other post i have more info

it sends

A195A245A200A458 etc

search = Serial.read();
  if (search == 'A')  {
    rpm100 = Serial.read() - '0';
    rpm10 = Serial.read() - '0';
    rpm1 = Serial.read() - '0';

    rpm = (100*rpm100) + (10*rpm10) + rpm1;

    position = map(rpm,127,255,40,0);

    // debug
    Serial.println(rpm);
    Serial.println(position);

  }