a question about Serial communication


i'm new arduino , try learn basics. code locks when enter other syntax written in "loop" section.
i couldn't find way whenever different written says "ooopss" , wait syntax.... in advance.

code: [select]


char indata[20]; // allocate space string
char inchar=-1; // store character read
byte index = 0; // index array; store character

void setup() {
    serial.begin(9600);
    serial.write("power on");
}

char comp(char* this) {
   
  while (serial.available() > 0) // don't read unless
                                   // there know there data
    {
        if(index < 19) // 1 less size of array
        {
            inchar = serial.read(); // read character
            indata[index] = inchar; // store it
            index++; // increment write next
            indata[index] = '\0'; // null terminate string
        }
    }

    if (strcmp(indata,this)  == 0) {
       
      (int i=0;i<19;i++) {
            indata[i]=0;
        }
        index=0;
        return 0;
    }
    else { 
          return 1;
    }
}

void loop()
{
    if (comp("green")==0) {
      serial.println("\n green led"); 
     analogwrite(9, 255);
     analogwrite(3, 0);
     // serial.write("\nmotor 1 -> online\n");
    }
   if (comp("red")==0) {
      serial.println("\nred led"); 
      analogwrite(3, 255);
      analogwrite(9, 0);
     // serial.write("motor 1 -> offline\n");
    }
   if (comp("turnoff")==0)
    {
      analogwrite(9, 0);
      analogwrite(3, 0);
      serial.println("\nleds off");
    }
   
   
}

i don't understand question. code post work?  if so, post code not.  or, try explain better issue is.


Arduino Forum > Using Arduino > Programming Questions > a question about Serial communication


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial