serial.read() help

i would like to send "y" in vb so that arduino would turn on led

but i dont know how arduino interprets the received "y" from vb

{note: i had confirmed that vb is sending string only using MSComm1.output="y" }

but my arduino is not interpreting it correctly please help

this is the code i used

int i;
char rec[7];
char str3 = "Y";
void establishContact();
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.flush();
  pinMode(13, OUTPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds 
           
 }


void loop()
{
  // if we get a valid byte, read analog ins:
   for (i=0;i<Serial.available();i++)
    {
    rec[i]=Serial.read();
     }
  delay(10);
        if (rec==str3)
     {
        Serial.print("firs");
        digitalWrite(13,HIGH);
     }
    
}

void establishContact()
{
  //while (Serial.available() <= 0) 
  //{
    Serial.print("SEND"); 
    //Serial.println(byte(26));
    // send an initial string
    delay(300);
    
  //}
}

please help guys

if (rec==str3)

You can't directly compare character arrays - use "strcmp".
Make sure your received string is terminated first.

(also "Y" != "y")

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294723649
Please don't cross-post.

Please don't cross-post.

sorry for that . i wont do that again.

that was quite helpful and i used a strcmp function but still it doesn't work

this is my code

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int ledpin =13;
void establishContact();
void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  Serial.flush();
  pinMode(ledpin, OUTPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds 
           
 }


void loop()
{
  // if we get a valid byte, read analog ins:
     while(Serial.available() > 0) // Don't read unless
                                                  // there you know there is data
   {
        digitalWrite(ledpin,HIGH);
            if(index < 19) // One less than the size of the array
       {
           
             inChar = Serial.read(); // Read a character
           inData[index] = inChar; // Store it
           index++; // Increment where to write next
           inData[index] = '\0'; // Null terminate the string
       }
   }
    if (strcmp(inData,"124")==0)
     {
        Serial.print("firs");
        digitalWrite(ledpin,HIGH);
     }
         
}

void establishContact()
{
  //while (Serial.available() <= 0) 
  //{
    Serial.print("1234"); 
    //Serial.println(byte(26));
    // send an initial string
    delay(300);
    
  //}
}

thank you

note:

and by the way in strcmp function i used "124" because i sent MScomm1.output="124"

in visual basic.

I recently worked on a project where I needed to send Values to Arduino from my PC over serial.

I adapted some code from the EARTHSHINE DESIGN Starter Kit Manual (great stuff)

I share what I did so maybe you can extract some reference examples you can use. (full code is in exhibition section)

//----[ MAIN LOOP ]------------------------------------------------------
void loop() {

// Mike McRoberts serial input command routines
// from the "Serial Controlled Mood Lamp" example
// in Arduino Starter Kit Manual from Earthshine Design

if (Serial.available() > 0) {
int index=0;
  delay(10); // let the buffer fill up
  int numChar = Serial.available();
   if (numChar>buffsize) {
       numChar=buffsize;
      }
   while (numChar--) {
          buffer[index++] = Serial.read();
         }
 splitString(buffer); // Process Serial Packet
 }
}

//----[ SubRoutines ]----------------------------------------------------

void splitString(char* data) {
// also from "Serial Controlled Mood Lamp" example

if (debug) {
 Serial.print("Data entered: ");
 Serial.println(data);
}

// Sequentially De-Tokenize the Serial Commands received
 char* parameter;
       parameter = strtok (data, " ,");
   while (parameter != NULL) {
          // Pass result to parseCMD for each Command received
          parseCMD(parameter);
          // remove processed commands from the list
          parameter = strtok (NULL, " ,");
   }

   // Clear the text and serial buffers
   for (int x=0; x<buffsize; x++) {
        buffer[x]='\0';
   }
  Serial.flush();
}

//=======================================================================
void parseCMD(char* data) {
// Flexible, easily expanded Command Parser
// based on "Serial Controlled Mood Lamp" example
// *** Marvelous coding by Mike MCRoberts

//--[gear]---------------------------------------
if ((data[0] == 'G') || (data[0] == 'g')) {
 // Have command, now get Argument "value" while removing whitespace
 int ArgVal = strtol(data+1, NULL, 10);
 // then limit the results to what we expect for this command
     ArgVal = constrain(ArgVal,0,8);
     sevenSegWrite(ArgVal);

     if (debug) {
         Serial.print("Gear is set to: ");
         Serial.println(ArgVal);
     }
}

//--[shift light]--------------------------------
if ((data[0] == 'S') || (data[0] == 's')) {
 int ArgVal = strtol(data+1, NULL, 10);
     ArgVal = constrain(ArgVal,0,1);
     digitalWrite(shiftlight,ArgVal);

     if (debug) {
         Serial.print("SHIFT is set to: ");
         Serial.println(ArgVal);
     }
}

//--[reset]--------------------------------------
 if ((data[0] == 'C') || (data[0] == 'c')) {
   int ArgVal = strtol(data+1, NULL, 10);
       ArgVal = constrain(ArgVal,0,1);

       sevenSegWrite(8);
       shiftWrite(0x00);

     if (debug) {
         Serial.print("Clear Outputs");
     }
 }

 //--[rpm bar graph]-----------------------------
 if ((data[0] == 'R') || (data[0] == 'r')) {
   int ArgVal = strtol(data+1, NULL, 10);
       ArgVal = constrain(ArgVal,0,8);

       shiftWrite(bargraph[ArgVal]);

       if (debug) {
           Serial.print("RPM is set to: ");
           Serial.println(ArgVal);
       }
 }

} // End parseCMD Loop

This worked really well for me...

I could send C0 (or any other) by itself or send "C0,R2,s1,G3" and it deals with any combo, upper or lower case... it works nicely.